public void closeDB() { try { if (dao != null && dao.getConnectionSource() != null) { DatabaseConnection conn = dao.getConnectionSource().getReadWriteConnection(); conn.executeStatement("shutdown compact", 0); } } catch (SQLException ex) { Logger.getLogger(TableRecordRepository.class).error("Error closing table_record repository - ", ex); } } }
DatabaseConnection conn = dao.startThreadConnection(); Savepoint savePoint = null; try { savePoint = conn.setSavePoint(null); doInserts(dao); } finally { // commit at the end conn.commit(savePoint); dao.endThreadConnection(conn); }
/** * @param connectionSource {@link ConnectionSource} for transaction. * @throws SQLException Connection error. */ public OrmliteTransaction(@Nonnull ConnectionSource connectionSource) throws SQLException { Validate.notNull(connectionSource, "connectionSource"); this.connectionSource = connectionSource; this.connection = this.connectionSource.getReadWriteConnection(); final boolean saved = this.connectionSource.saveSpecialConnection(connection); if (saved || connectionSource.getDatabaseType().isNestedSavePointsSupported()) { if (connection.isAutoCommitSupported()) { autoCommitAtStart = connection.isAutoCommit(); if (autoCommitAtStart) { connection.setAutoCommit(false); } } else { autoCommitAtStart = false; } savepoint = connection.setSavePoint(SAVE_POINT_PREFIX + savepointCounter.incrementAndGet()); } else { savepoint = null; autoCommitAtStart = false; } }
@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); }
@Test public void testTransactionManagerSavePointNull() throws Exception { ConnectionSource connectionSource = createMock(ConnectionSource.class); DatabaseConnection conn = createMock(DatabaseConnection.class); expect(conn.isAutoCommitSupported()).andReturn(false); expect(conn.setSavePoint(isA(String.class))).andReturn(null); conn.commit(null); 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); TransactionManager tm = new TransactionManager(connectionSource); tm.callInTransaction(new Callable<Void>() { @Override public Void call() { return null; } }); verify(connectionSource, conn); }
/** * Return true if the two connections seem to one one connection under the covers. */ protected boolean isSingleConnection(DatabaseConnection conn1, DatabaseConnection conn2) throws SQLException { // initialize the connections auto-commit flags conn1.setAutoCommit(true); conn2.setAutoCommit(true); try { // change conn1's auto-commit to be false conn1.setAutoCommit(false); if (conn2.isAutoCommit()) { // if the 2nd connection's auto-commit is still true then we have multiple connections return false; } else { // if the 2nd connection's auto-commit is also false then we have a single connection return true; } } finally { // restore its auto-commit conn1.setAutoCommit(true); } }
@Test public void testTransactionManagerRollbackNullSavePoint() throws Exception { ConnectionSource connectionSource = createMock(ConnectionSource.class); DatabaseConnection conn = createMock(DatabaseConnection.class); expect(conn.isAutoCommitSupported()).andReturn(false); expect(conn.setSavePoint(isA(String.class))).andReturn(null); conn.rollback(null); 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); TransactionManager tm = new TransactionManager(connectionSource); try { tm.callInTransaction(new Callable<Void>() { @Override public Void call() throws Exception { throw new SQLException("you better roll back!!"); } }); fail("expected an exception"); } catch (SQLException e) { // expected } verify(connectionSource, conn); }
@Override public CompiledStatement compileStatement(String statement, StatementType type, FieldType[] argFieldTypes, int resultFlags, boolean cacheStore) throws SQLException { if (proxy == null) { return null; } else { return proxy.compileStatement(statement, type, argFieldTypes, resultFlags, cacheStore); } }
@Override public void setAutoCommit(boolean autoCommit) throws SQLException { if (proxy != null) { proxy.setAutoCommit(autoCommit); } }
@Override public void commit(Savepoint savePoint) throws SQLException { if (proxy != null) { proxy.commit(savePoint); } }
@Override public int insert(String statement, Object[] args, FieldType[] argfieldTypes, GeneratedKeyHolder keyHolder) throws SQLException { if (proxy == null) { return 0; } else { return proxy.insert(statement, args, argfieldTypes, keyHolder); } }
@Override public boolean isAutoCommit() throws SQLException { if (proxy == null) { return false; } else { return proxy.isAutoCommit(); } }
@Test public void testExecuteStatement() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); String statement = "select foo from bar"; int result = 1312321; expect(conn.executeStatement(statement, 0)).andReturn(result); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); assertEquals(result, proxy.executeStatement(statement, 0)); proxy.close(); verify(conn); }
@Test public void testGeneratedIdSequenceLong() throws Exception { DatabaseType databaseType = new NeedsSequenceDatabaseType(); connectionSource.setDatabaseType(databaseType); Dao<GeneratedIdLong, Long> dao = createDao(GeneratedIdLong.class, false); StatementExecutor<GeneratedIdLong, Long> se = new StatementExecutor<GeneratedIdLong, Long>(databaseType, new TableInfo<GeneratedIdLong, Long>(databaseType, GeneratedIdLong.class), dao); DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L); expect(databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), (GeneratedKeyHolder) isNull())).andReturn(1); replay(databaseConnection); GeneratedIdLong genIdSeq = new GeneratedIdLong(); se.create(databaseConnection, genIdSeq, null); verify(databaseConnection); }
@Test public void testSetAutoCommit() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); boolean autoCommit = false; conn.setAutoCommit(autoCommit); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); proxy.setAutoCommit(autoCommit); proxy.close(); verify(conn); }
@Test public void testIsAutoCommit() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); boolean autoCommit = false; expect(conn.isAutoCommit()).andReturn(autoCommit); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); assertEquals(autoCommit, proxy.isAutoCommit()); proxy.close(); verify(conn); }
@Test public void testCallBatchTasksAutoCommitFalse() 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(false); 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); }
@Override public void close() throws IOException { if (proxy != null) { proxy.close(); } }
@Test public void testSetSavePoint() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); String name = "savepoint"; expect(conn.setSavePoint(name)).andReturn(null); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); proxy.setSavePoint(name); proxy.close(); verify(conn); }
@Test public void testCompileStatementStringStatementTypeFieldTypeArrayInt() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); String statement = "select foo from bar"; StatementType type = StatementType.DELETE; int flags = 11253123; expect(conn.compileStatement(statement, type, null, flags, false)).andReturn(null); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); proxy.compileStatement(statement, type, null, flags, false); proxy.close(); verify(conn); }