Java Code Examples for com.j256.ormlite.support.DatabaseConnection#setAutoCommit()
The following examples show how to use
com.j256.ormlite.support.DatabaseConnection#setAutoCommit() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcConnectionSource.java From ormlite-jdbc with ISC License | 6 votes |
/** * Make a connection to the database. * * @param logger * This is here so we can use the right logger associated with the sub-class. */ @SuppressWarnings("resource") protected DatabaseConnection makeConnection(Logger logger) throws SQLException { Properties properties = new Properties(); if (username != null) { properties.setProperty("user", username); } if (password != null) { properties.setProperty("password", password); } DatabaseConnection connection = new JdbcDatabaseConnection(DriverManager.getConnection(url, properties)); // by default auto-commit is set to true connection.setAutoCommit(true); if (connectionProxyFactory != null) { connection = connectionProxyFactory.createProxy(connection); } logger.debug("opened connection to {} got #{}", url, connection.hashCode()); return connection; }
Example 2
Source File: JdbcPooledConnectionSourceTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testSetAutoCommit() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { DatabaseConnection conn1 = pooled.getReadOnlyConnection(null); conn1.setAutoCommit(false); pooled.releaseConnection(conn1); DatabaseConnection conn2 = pooled.getReadOnlyConnection(null); assertSame(conn1, conn2); assertTrue(conn2.isAutoCommit()); } finally { pooled.close(); } }
Example 3
Source File: StatementExecutorTest.java From ormlite-core with ISC License | 5 votes |
@Test public void testCallBatchTasksAutoCommitTrue() throws Exception { TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class); ConnectionSource connectionSource = createMock(ConnectionSource.class); DatabaseConnection connection = createMock(DatabaseConnection.class); expect(connectionSource.isSingleConnection("foo")).andReturn(false); expect(connectionSource.getReadWriteConnection("foo")).andReturn(connection); expect(connectionSource.saveSpecialConnection(connection)).andReturn(false); connectionSource.clearSpecialConnection(connection); connectionSource.releaseConnection(connection); expect(connection.isAutoCommitSupported()).andReturn(true); expect(connection.isAutoCommit()).andReturn(true); connection.setAutoCommit(false); connection.setAutoCommit(true); StatementExecutor<Foo, String> statementExec = new StatementExecutor<Foo, String>(databaseType, tableInfo, null); replay(connectionSource, connection); final AtomicBoolean called = new AtomicBoolean(false); statementExec.callBatchTasks(connectionSource, new Callable<Void>() { @Override public Void call() { called.set(true); return null; } }); assertTrue(called.get()); verify(connectionSource, connection); }
Example 4
Source File: StatementExecutorTest.java From ormlite-core with ISC License | 5 votes |
@Test public void testCallBatchTasksAutoCommitTrueSynchronized() throws Exception { TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class); ConnectionSource connectionSource = createMock(ConnectionSource.class); DatabaseConnection connection = createMock(DatabaseConnection.class); expect(connectionSource.isSingleConnection("foo")).andReturn(true); expect(connectionSource.getReadWriteConnection("foo")).andReturn(connection); expect(connectionSource.saveSpecialConnection(connection)).andReturn(false); connectionSource.clearSpecialConnection(connection); connectionSource.releaseConnection(connection); expect(connection.isAutoCommitSupported()).andReturn(true); expect(connection.isAutoCommit()).andReturn(true); connection.setAutoCommit(false); connection.setAutoCommit(true); StatementExecutor<Foo, String> statementExec = new StatementExecutor<Foo, String>(databaseType, tableInfo, null); replay(connectionSource, connection); final AtomicBoolean called = new AtomicBoolean(false); statementExec.callBatchTasks(connectionSource, new Callable<Void>() { @Override public Void call() { called.set(true); return null; } }); assertTrue(called.get()); verify(connectionSource, connection); }
Example 5
Source File: StatementExecutorTest.java From ormlite-core with ISC License | 5 votes |
@Test public void testCallBatchTasksAutoCommitTrueThrow() throws Exception { TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class); ConnectionSource connectionSource = createMock(ConnectionSource.class); DatabaseConnection connection = createMock(DatabaseConnection.class); expect(connectionSource.isSingleConnection("foo")).andReturn(false); expect(connectionSource.getReadWriteConnection("foo")).andReturn(connection); expect(connectionSource.saveSpecialConnection(connection)).andReturn(false); connectionSource.clearSpecialConnection(connection); connectionSource.releaseConnection(connection); expect(connection.isAutoCommitSupported()).andReturn(true); expect(connection.isAutoCommit()).andReturn(true); connection.setAutoCommit(false); connection.setAutoCommit(true); StatementExecutor<Foo, String> statementExec = new StatementExecutor<Foo, String>(databaseType, tableInfo, null); replay(connectionSource, connection); try { statementExec.callBatchTasks(connectionSource, new Callable<Void>() { @Override public Void call() throws Exception { throw new Exception("expected"); } }); fail("Should have thrown"); } catch (Exception e) { // expected } verify(connectionSource, connection); }
Example 6
Source File: BaseDaoImplTest.java From ormlite-core with ISC License | 5 votes |
@Test public void testConnectionMethods() throws Exception { Dao<Foo, Integer> dao = createDao(Foo.class, true); DatabaseConnection conn = null; try { conn = dao.startThreadConnection(); assertTrue(dao.isAutoCommit(conn)); dao.setAutoCommit(conn, false); assertFalse(dao.isAutoCommit(conn)); Foo foo = new Foo(); assertEquals(1, dao.create(foo)); assertNotNull(dao.queryForId(foo.id)); dao.rollBack(conn); assertNull(dao.queryForId(foo.id)); foo = new Foo(); assertEquals(1, dao.create(foo)); assertNotNull(dao.queryForId(foo.id)); dao.commit(conn); assertNotNull(dao.queryForId(foo.id)); dao.rollBack(conn); assertNotNull(dao.queryForId(foo.id)); } finally { if (conn != null) { conn.setAutoCommit(true); dao.endThreadConnection(conn); } } }
Example 7
Source File: TransactionManagerTest.java From ormlite-core with ISC License | 5 votes |
@Test public void testTransactionManagerAutoCommitOn() throws Exception { ConnectionSource connectionSource = createMock(ConnectionSource.class); DatabaseConnection conn = createMock(DatabaseConnection.class); expect(conn.isAutoCommitSupported()).andReturn(true); expect(conn.isAutoCommit()).andReturn(true); conn.setAutoCommit(false); Savepoint savePoint = createMock(Savepoint.class); expect(savePoint.getSavepointName()).andReturn("name").anyTimes(); expect(conn.setSavePoint(isA(String.class))).andReturn(savePoint); conn.commit(savePoint); conn.setAutoCommit(true); expect(connectionSource.getDatabaseType()).andReturn(databaseType); expect(connectionSource.getReadWriteConnection(null)).andReturn(conn); expect(connectionSource.saveSpecialConnection(conn)).andReturn(true); connectionSource.clearSpecialConnection(conn); connectionSource.releaseConnection(conn); replay(connectionSource, conn, savePoint); TransactionManager tm = new TransactionManager(connectionSource); tm.callInTransaction(new Callable<Void>() { @Override public Void call() { return null; } }); verify(connectionSource, conn, savePoint); }
Example 8
Source File: BaseDaoImpl.java From ormlite-core with ISC License | 4 votes |
@Override public void setAutoCommit(DatabaseConnection connection, boolean autoCommit) throws SQLException { connection.setAutoCommit(autoCommit); }
Example 9
Source File: TransactionManager.java From ormlite-core with ISC License | 4 votes |
/** * Same as {@link #callInTransaction(Callable)} except as a static method on a connection with database-type. * * <p> * WARNING: it is up to you to properly synchronize around this method if multiple threads are using the same * database connection or if your connection-source is single-connection. The reason why this is necessary is that * multiple operations are performed on the connection and race-conditions will exist with multiple threads working * on the same connection. * </p> */ public static <T> T callInTransaction(final DatabaseConnection connection, boolean saved, final DatabaseType databaseType, final Callable<T> callable) throws SQLException { boolean restoreAutoCommit = false; TransactionLevel levelCount = transactionLevelThreadLocal.get(); try { boolean hasSavePoint = false; Savepoint savePoint = null; if (saved || databaseType.isNestedSavePointsSupported()) { if (connection.isAutoCommitSupported()) { if (connection.isAutoCommit()) { // disable auto-commit mode if supported and enabled at start connection.setAutoCommit(false); restoreAutoCommit = true; logger.trace("had to set auto-commit to false"); } } savePoint = connection.setSavePoint(SAVE_POINT_PREFIX + savePointCounter.incrementAndGet()); if (savePoint == null) { logger.trace("started savePoint transaction"); } else { logger.trace("started savePoint transaction {}", savePoint.getSavepointName()); } hasSavePoint = true; } try { levelCount.incrementAndGet(); T result = callable.call(); int level = levelCount.decrementAndGet(); if (level <= 0) { transactionLevelThreadLocal.remove(); levelCount = null; } if (hasSavePoint) { // only commit if we have reached the end of our transaction stack if (level <= 0) { commit(connection, savePoint); } else { // otherwise we just release the savepoint release(connection, savePoint); } } return result; } catch (Exception e) { if (levelCount != null && levelCount.decrementAndGet() <= 0) { transactionLevelThreadLocal.remove(); } if (hasSavePoint) { try { rollBack(connection, savePoint); } catch (SQLException e2) { logger.error(e, "after commit exception, rolling back to save-point also threw exception"); // we continue to throw the commit exception } } if (e instanceof SQLException) { throw (SQLException) e; } else { throw SqlExceptionUtil.create("Transaction callable threw non-SQL exception", e); } } } finally { if (restoreAutoCommit) { // try to restore if we are in auto-commit mode connection.setAutoCommit(true); logger.trace("restored auto-commit to true"); } } }