Java Code Examples for com.j256.ormlite.dao.Dao#startThreadConnection()

The following examples show how to use com.j256.ormlite.dao.Dao#startThreadConnection() . 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: JdbcPooledConnectionSourceTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testConnectionRollback() throws Exception {
	JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL);
	Dao<Foo, Integer> dao = null;
	DatabaseConnection conn = null;
	try {
		TableUtils.createTable(pooled, Foo.class);
		dao = DaoManager.createDao(pooled, Foo.class);
		conn = dao.startThreadConnection();
		dao.setAutoCommit(conn, false);
		Foo foo = new Foo();
		assertEquals(1, dao.create(foo));
		assertNotNull(dao.queryForId(foo.id));
		dao.endThreadConnection(conn);
		assertNull(dao.queryForId(foo.id));
	} finally {
		TableUtils.dropTable(pooled, Foo.class, true);
		if (dao != null) {
			dao.endThreadConnection(conn);
		}
		pooled.close();
	}
}
 
Example 2
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testCallBatchTasksCommitted() throws Exception {
	final Dao<Foo, Integer> dao = createDao(Foo.class, true);
	final Foo foo1 = new Foo();
	DatabaseConnection conn = dao.startThreadConnection();
	try {
		dao.callBatchTasks(new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				assertEquals(1, dao.create(foo1));
				assertNotNull(dao.queryForId(foo1.id));
				return null;
			}
		});
		dao.rollBack(conn);
		assertNotNull(dao.queryForId(foo1.id));
	} finally {
		dao.endThreadConnection(conn);
	}
}