]> source.dussan.org Git - iciql.git/commitdiff
Allows to gain full control of transactions
authorbartolomiew <bart_olomiew@yahoo.fr>
Sun, 28 Oct 2012 07:41:27 +0000 (08:41 +0100)
committerJames Moger <james.moger@gmail.com>
Fri, 2 Nov 2012 18:33:26 +0000 (14:33 -0400)
src/com/iciql/Db.java
src/com/iciql/TableDefinition.java
tests/com/iciql/test/ForeignKeyTest.java
tests/com/iciql/test/IciqlSuite.java
tests/com/iciql/test/TransactionTest.java [new file with mode: 0644]

index caec637c3a7c39ff83cb29d287dec79d611199a9..32b7c6a186175988d9bcac08a35d1416ce64e3c2 100644 (file)
@@ -1,6 +1,7 @@
 /*\r
  * Copyright 2004-2011 H2 Group.\r
  * Copyright 2011 James Moger.\r
+ * Copyright 2012 Frederic Gaillard.\r
  *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -68,6 +69,9 @@ public class Db {
        private DbUpgrader dbUpgrader = new DefaultDbUpgrader();\r
        private final Set<Class<?>> upgradeChecked = Collections.synchronizedSet(new HashSet<Class<?>>());\r
 \r
+       private boolean skipCreate;\r
+       private boolean autoSavePoint = true;\r
+       \r
        static {\r
                TOKENS = Collections.synchronizedMap(new WeakIdentityHashMap<Object, Token>());\r
                DIALECTS = Collections.synchronizedMap(new HashMap<String, Class<? extends SQLDialect>>());\r
@@ -563,6 +567,11 @@ public class Db {
        }\r
        \r
        Savepoint prepareSavepoint() {\r
+               // don't change auto-commit mode.\r
+               // don't create save point.\r
+               if (!autoSavePoint) {\r
+                       return null;\r
+               }\r
                // create a savepoint\r
                Savepoint savepoint = null;\r
                try {\r
@@ -731,4 +740,33 @@ public class Db {
                        JdbcUtils.closeSilently(stat);\r
                }\r
        }\r
+\r
+       /**\r
+        * Allow to enable/disable globally createIfRequired in TableDefinition.\r
+        * For advanced user wanting to gain full control of transactions.\r
+        * Default value is false.\r
+        * @param skipCreate\r
+        */\r
+       public void setSkipCreate(boolean skipCreate) {\r
+               this.skipCreate = skipCreate;\r
+       }\r
+\r
+       public boolean getSkipCreate() {\r
+               return this.skipCreate;\r
+       }\r
+       \r
+       /**\r
+        * Allow to enable/disable usage of save point.\r
+        * For advanced user wanting to gain full control of transactions.\r
+        * Default value is false.\r
+        * @param autoSavePoint\r
+        */\r
+       public void setAutoSavePoint(boolean autoSavePoint) {\r
+               this.autoSavePoint = autoSavePoint;\r
+       }\r
+\r
+       public boolean getAutoSavePoint() {\r
+               return this.autoSavePoint;\r
+       }\r
+       \r
 }\r
index 1ecf394546138f79cb1ff8b3e172177d6519c99b..e96b61c7164912b154ecccbd78feb92a608a6924 100644 (file)
@@ -1,7 +1,7 @@
 /*\r
  * Copyright 2004-2011 H2 Group.\r
  * Copyright 2011 James Moger.\r
- * Copyright 2012 Frédéric Gaillard.\r
+ * Copyright 2012 Frederic Gaillard.\r
  *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -839,6 +839,10 @@ public class TableDefinition<T> {
        }\r
 \r
        TableDefinition<T> createIfRequired(Db db) {\r
+               // globally enable/disable check of create if required\r
+               if (db.getSkipCreate()) {\r
+                       return this;\r
+               }\r
                if (!createIfRequired) {\r
                        // skip table and index creation\r
                        // but still check for upgrades\r
index d7894086484995e8f58b8db2b281e4556c3e96b7..ed85cbc87bc1b9b8e21ca3d67b07fb0e99cf9b2f 100644 (file)
@@ -28,6 +28,9 @@ import com.iciql.IciqlException;
 import com.iciql.test.models.CategoryAnnotationOnly;\r
 import com.iciql.test.models.ProductAnnotationOnlyWithForeignKey;\r
 \r
+/**\r
+ * Tests of Foreign Keys.\r
+ */\r
 public class ForeignKeyTest {\r
 \r
        /**\r
index 9181db7f2e6555b7ad342d232d57f9f135b2e1b9..1202d0ebd84877a4839269f56c219b7a1b0c87ff 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
  * Copyright 2011 James Moger.\r
- * Copyright 2012 Frédéric Gaillard.\r
+ * Copyright 2012 Frederic Gaillard.\r
  *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -93,7 +93,7 @@ import com.iciql.util.Utils;
 @SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class,\r
                ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class,\r
                RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UpgradesTest.class, JoinTest.class,\r
-               UUIDTest.class, ViewsTest.class, ForeignKeyTest.class })\r
+               UUIDTest.class, ViewsTest.class, ForeignKeyTest.class, TransactionTest.class })\r
 public class IciqlSuite {\r
 \r
        private static final TestDb[] TEST_DBS = {\r
diff --git a/tests/com/iciql/test/TransactionTest.java b/tests/com/iciql/test/TransactionTest.java
new file mode 100644 (file)
index 0000000..d1c6749
--- /dev/null
@@ -0,0 +1,119 @@
+/*\r
+ * Copyright 2012 Frederic Gaillard.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.iciql.test;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+\r
+import java.sql.SQLException;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+\r
+import com.iciql.Db;\r
+import com.iciql.IciqlException;\r
+import com.iciql.test.models.CategoryAnnotationOnly;\r
+import com.iciql.test.models.ProductAnnotationOnlyWithForeignKey;\r
+\r
+/**\r
+ * Tests of transactions.\r
+ */\r
+public class TransactionTest {\r
+\r
+       /**\r
+        * This object represents a database (actually a connection to the\r
+        * database).\r
+        */\r
+\r
+       private Db db;\r
+\r
+       @Before\r
+       public void setUp() {\r
+               db = IciqlSuite.openNewDb();\r
+               \r
+               // tables creation\r
+               db.from(new CategoryAnnotationOnly());\r
+               db.from(new ProductAnnotationOnlyWithForeignKey());\r
+               \r
+               startTransactionMode();\r
+       }\r
+\r
+       @After\r
+       public void tearDown() {\r
+               \r
+               endTransactionMode();\r
+               \r
+               db.dropTable(ProductAnnotationOnlyWithForeignKey.class);\r
+               db.dropTable(CategoryAnnotationOnly.class);\r
+               db.close();\r
+       }\r
+\r
+       @Test\r
+       public void testTransaction() {\r
+               \r
+               // insert in 2 tables inside a transaction\r
+               \r
+               // insertAll don't use save point in this transaction\r
+               db.insertAll(CategoryAnnotationOnly.getList());\r
+               db.insertAll(ProductAnnotationOnlyWithForeignKey.getList());\r
+\r
+               // don't commit changes\r
+               try {\r
+                       db.getConnection().rollback();\r
+               } catch (SQLException e) {\r
+                       throw new IciqlException(e, "Can't rollback");\r
+               }\r
+               \r
+               ProductAnnotationOnlyWithForeignKey p = new ProductAnnotationOnlyWithForeignKey();\r
+               long count1 = db.from(p).selectCount();\r
+               \r
+               CategoryAnnotationOnly c = new CategoryAnnotationOnly();\r
+               long count2 = db.from(c).selectCount();\r
+               \r
+               // verify changes aren't committed\r
+               assertEquals(count1, 0L);\r
+               assertEquals(count2, 0L);\r
+       }\r
+\r
+       /**\r
+        * Helper to set transaction mode\r
+        */\r
+       private void startTransactionMode() {\r
+               db.setSkipCreate(true);\r
+               db.setAutoSavePoint(false);\r
+               \r
+               try {\r
+                       db.getConnection().setAutoCommit(false);\r
+               } catch (SQLException e) {\r
+                       throw new IciqlException(e, "Could not change auto-commit mode");\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Helper to return to initial mode\r
+        */\r
+       private void endTransactionMode() {\r
+               try {\r
+                       db.getConnection().setAutoCommit(true);\r
+               } catch (SQLException e) {\r
+                       throw new IciqlException(e, "Could not change auto-commit mode");\r
+               }\r
+               // returns to initial states\r
+               db.setSkipCreate(false);\r
+               db.setAutoSavePoint(true);\r
+       }\r
+       \r
+}\r