Java Code Examples for com.j256.ormlite.support.ConnectionSource#clearSpecialConnection()

The following examples show how to use com.j256.ormlite.support.ConnectionSource#clearSpecialConnection() . 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: TransactionManagerTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testTransactionManagerAutoCommitSupported() throws Exception {
	ConnectionSource connectionSource = createMock(ConnectionSource.class);
	DatabaseConnection conn = createMock(DatabaseConnection.class);
	expect(conn.isAutoCommitSupported()).andReturn(true);
	expect(conn.isAutoCommit()).andReturn(false);
	Savepoint savePoint = createMock(Savepoint.class);
	expect(savePoint.getSavepointName()).andReturn("name").anyTimes();
	expect(conn.setSavePoint(isA(String.class))).andReturn(savePoint);
	conn.commit(savePoint);
	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 2
Source File: StatementExecutor.java    From ormlite-core with ISC License 6 votes vote down vote up
private <CT> CT doCallBatchTasks(ConnectionSource connectionSource, Callable<CT> callable) throws SQLException {
	DatabaseConnection connection = connectionSource.getReadWriteConnection(tableInfo.getTableName());
	try {
		/*
		 * We are using a thread-local boolean to detect whether we are in the middle of running a number of
		 * changes. This disables the dao change notification for every batched call.
		 */
		localIsInBatchMode.set(true);
		/*
		 * We need to save the connection because we are going to be disabling auto-commit on it and we don't want
		 * pooled connection factories to give us another connection where auto-commit might still be enabled.
		 */
		boolean saved = connectionSource.saveSpecialConnection(connection);
		return doCallBatchTasks(connection, saved, callable);
	} finally {
		// even if the save-special returned false, we need to clear it to decrement the usage counter
		connectionSource.clearSpecialConnection(connection);
		connectionSource.releaseConnection(connection);
		localIsInBatchMode.set(false);
		if (dao != null) {
			// only at the end is the DAO notified of changes
			dao.notifyChanges();
		}
	}
}
 
Example 3
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@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);
}
 
Example 4
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testTransactionManagerTableName() throws Exception {
	ConnectionSource connectionSource = createMock(ConnectionSource.class);
	DatabaseConnection conn = createMock(DatabaseConnection.class);
	expect(conn.isAutoCommitSupported()).andReturn(false);
	Savepoint savePoint = createMock(Savepoint.class);
	expect(savePoint.getSavepointName()).andReturn("name").anyTimes();
	expect(conn.setSavePoint(isA(String.class))).andReturn(savePoint);
	conn.commit(savePoint);
	expect(connectionSource.getDatabaseType()).andReturn(databaseType);
	expect(connectionSource.getReadWriteConnection(FOO_TABLE_NAME)).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(FOO_TABLE_NAME, new Callable<Void>() {
		@Override
		public Void call() {
			return null;
		}
	});
	verify(connectionSource, conn, savePoint);
}
 
Example 5
Source File: OrmLiteDatabaseHelper.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
/**
 * 数据库降级
 */
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    ConnectionSource cs = getConnectionSource();
    DatabaseConnection conn = cs.getSpecialConnection(null);
    boolean clearSpecial = false;
    if (conn == null) {
        conn = new AndroidDatabaseConnection(db, true, this.cancelQueriesEnabled);
        try {
            cs.saveSpecialConnection(conn);
            clearSpecial = true;
        } catch (SQLException var11) {
            throw new IllegalStateException("Could not save special connection", var11);
        }
    }

    try {
        this.onDowngrade(cs, oldVersion, newVersion);
    } finally {
        if (clearSpecial) {
            cs.clearSpecialConnection(conn);
        }
    }
}
 
Example 6
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testTransactionManager() throws Exception {
	ConnectionSource connectionSource = createMock(ConnectionSource.class);
	DatabaseConnection conn = createMock(DatabaseConnection.class);
	expect(conn.isAutoCommitSupported()).andReturn(false);
	Savepoint savePoint = createMock(Savepoint.class);
	expect(savePoint.getSavepointName()).andReturn("name").anyTimes();
	expect(conn.setSavePoint(isA(String.class))).andReturn(savePoint);
	conn.commit(savePoint);
	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 7
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@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 8
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@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 9
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testTransactionManagerRollbackOtherException() throws Exception {
	ConnectionSource connectionSource = createMock(ConnectionSource.class);
	DatabaseConnection conn = createMock(DatabaseConnection.class);
	expect(conn.isAutoCommitSupported()).andReturn(false);
	Savepoint savePoint = createMock(Savepoint.class);
	expect(savePoint.getSavepointName()).andReturn("name").anyTimes();
	expect(conn.setSavePoint(isA(String.class))).andReturn(savePoint);
	conn.rollback(savePoint);
	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);
	try {
		tm.callInTransaction(new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				throw new Exception("you better roll back!!");
			}
		});
		fail("expected an exception");
	} catch (Exception e) {
		// expected
	}
	verify(connectionSource, conn, savePoint);
}
 
Example 10
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@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);
}
 
Example 11
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testTransactionManagerRollback() throws Exception {
	ConnectionSource connectionSource = createMock(ConnectionSource.class);
	DatabaseConnection conn = createMock(DatabaseConnection.class);
	expect(conn.isAutoCommitSupported()).andReturn(false);
	Savepoint savePoint = createMock(Savepoint.class);
	expect(savePoint.getSavepointName()).andReturn("name").anyTimes();
	expect(conn.setSavePoint(isA(String.class))).andReturn(savePoint);
	conn.rollback(savePoint);
	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);
	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, savePoint);
}
 
Example 12
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@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 13
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@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 14
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@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);
}
 
Example 15
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCallBatchTasksNoAutoCommit() 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(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);
}
 
Example 16
Source File: TransactionManager.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Same as {@link #callInTransaction(ConnectionSource, Callable)} except this has a table-name.
 * 
 * <p>
 * WARNING: it is up to you to properly synchronize around this method if multiple threads are using a
 * connection-source which works gives out a 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(String tableName, final ConnectionSource connectionSource,
		final Callable<T> callable) throws SQLException {

	DatabaseConnection connection = connectionSource.getReadWriteConnection(tableName);
	try {
		boolean saved = connectionSource.saveSpecialConnection(connection);
		return callInTransaction(connection, saved, connectionSource.getDatabaseType(), callable);
	} finally {
		// we should clear aggressively
		connectionSource.clearSpecialConnection(connection);
		connectionSource.releaseConnection(connection);
	}
}
 
Example 17
Source File: OrmLiteSqliteOpenHelper.java    From ormlite-android with ISC License 5 votes vote down vote up
/**
 * Satisfies the {@link SQLiteOpenHelper#onUpgrade(SQLiteDatabase, int, int)} interface method.
 */
@Override
public final void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	ConnectionSource cs = getConnectionSource();
	/*
	 * The method is called by Android database helper's get-database calls when Android detects that we need to
	 * create or update the database. So we have to use the database argument and save a connection to it on the
	 * AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource().
	 */
	DatabaseConnection conn = cs.getSpecialConnection(null);
	boolean clearSpecial = false;
	if (conn == null) {
		conn = new AndroidDatabaseConnection(db, true, cancelQueriesEnabled);
		try {
			cs.saveSpecialConnection(conn);
			clearSpecial = true;
		} catch (SQLException e) {
			throw new IllegalStateException("Could not save special connection", e);
		}
	}
	try {
		onUpgrade(db, cs, oldVersion, newVersion);
	} finally {
		if (clearSpecial) {
			cs.clearSpecialConnection(conn);
		}
	}
}
 
Example 18
Source File: OrmLiteSqliteOpenHelper.java    From ormlite-android with ISC License 5 votes vote down vote up
/**
 * Satisfies the {@link SQLiteOpenHelper#onCreate(SQLiteDatabase)} interface method.
 */
@Override
public final void onCreate(SQLiteDatabase db) {
	ConnectionSource cs = getConnectionSource();
	/*
	 * The method is called by Android database helper's get-database calls when Android detects that we need to
	 * create or update the database. So we have to use the database argument and save a connection to it on the
	 * AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource().
	 */
	DatabaseConnection conn = cs.getSpecialConnection(null);
	boolean clearSpecial = false;
	if (conn == null) {
		conn = new AndroidDatabaseConnection(db, true, cancelQueriesEnabled);
		try {
			cs.saveSpecialConnection(conn);
			clearSpecial = true;
		} catch (SQLException e) {
			throw new IllegalStateException("Could not save special connection", e);
		}
	}
	try {
		onCreate(db, cs);
	} finally {
		if (clearSpecial) {
			cs.clearSpecialConnection(conn);
		}
	}
}