/*\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
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
}\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
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
/*\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
}\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
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
/*\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
@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
--- /dev/null
+/*\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