Java Code Examples for com.j256.ormlite.support.DatabaseConnection#close()

The following examples show how to use com.j256.ormlite.support.DatabaseConnection#close() . 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: Database.java    From PacketProxy with Apache License 2.0 6 votes vote down vote up
public void saveWithoutLog(String path) throws Exception
{
	setChanged();
	notifyObservers(DatabaseMessage.PAUSE);
	clearChanged();

	Path src = databasePath;
	Path dest = FileSystems.getDefault().getPath(path);
	Files.copy(src,  dest, StandardCopyOption.REPLACE_EXISTING);
	JdbcConnectionSource new_db = new JdbcConnectionSource("jdbc:sqlite:"+dest);
	DatabaseConnection conn = new_db.getReadWriteConnection();
	conn.executeStatement("delete from packets", DatabaseConnection.DEFAULT_RESULT_FLAGS);
	conn.close();
	new_db.close();

	setChanged();
	notifyObservers(DatabaseMessage.RESUME);
	clearChanged();
}
 
Example 2
Source File: DataSourceConnectionSourceTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testDscsSetUrl() throws Exception {
	DataSource dataSource = createMock(DataSource.class);
	Connection conn = createMock(Connection.class);
	conn.setAutoCommit(true);
	conn.setAutoCommit(true);
	conn.setAutoCommit(false);
	expect(conn.getAutoCommit()).andReturn(false);
	conn.setAutoCommit(true);
	conn.close();
	conn.close();
	conn.close();
	expect(dataSource.getConnection()).andReturn(conn);
	expect(dataSource.getConnection()).andReturn(conn);
	expect(dataSource.getConnection()).andReturn(conn);
	replay(dataSource, conn);
	DataSourceConnectionSource dcs = new DataSourceConnectionSource();
	dcs.setDataSource(dataSource);
	dcs.setDatabaseUrl(DEFAULT_DATABASE_URL);
	dcs.initialize();
	DatabaseConnection jdbcConn = dcs.getReadOnlyConnection(TABLE_NAME);
	jdbcConn.close();
	dcs.close();
	verify(dataSource, conn);
}
 
Example 3
Source File: JdbcPooledConnectionSourceTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testTransaction() throws Exception {
	JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL);
	try {
		DatabaseConnection conn1 = pooled.getReadOnlyConnection(null);
		DatabaseConnection conn2 = pooled.getReadWriteConnection(null);
		pooled.saveSpecialConnection(conn2);
		DatabaseConnection conn3 = pooled.getReadWriteConnection(null);
		assertSame(conn2, conn3);
		pooled.releaseConnection(conn3);
		pooled.releaseConnection(conn1);
		DatabaseConnection conn4 = pooled.getReadWriteConnection(null);
		assertSame(conn2, conn4);
		pooled.releaseConnection(conn4);
		pooled.clearSpecialConnection(conn2);
		DatabaseConnection conn5 = pooled.getReadWriteConnection(null);
		assertSame(conn1, conn5);
		conn1.close();
		conn2.close();
		conn3.close();
		conn4.close();
		conn5.close();
	} finally {
		pooled.close();
	}
}
 
Example 4
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = SQLException.class)
public void testQueryForFirstThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	int equal = 21313;
	foo.equal = equal;
	assertEquals(1, dao.create(foo));

	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.queryForFirst(dao.queryBuilder().prepare());
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 5
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = SQLException.class)
public void testQueryForIdThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	int equal = 21313;
	foo.equal = equal;
	assertEquals(1, dao.create(foo));

	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.queryForId(foo.id);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 6
Source File: JdbcPooledConnectionSourceTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testTestClosedConnectionWithTesting() throws Exception {
	JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL);
	String pingStatement = pooled.getDatabaseType().getPingStatement();
	pooled.setTestBeforeGet(true);
	try {
		DatabaseConnection conn1 = pooled.getReadWriteConnection(null);
		conn1.queryForLong(pingStatement);
		pooled.releaseConnection(conn1);
		// close it behind the pool's back, bad dog
		conn1.close();
		DatabaseConnection conn2 = pooled.getReadWriteConnection(null);
		assertNotSame(conn1, conn2);
		conn2.queryForLong(pingStatement);
		pooled.releaseConnection(conn2);
		DatabaseConnection conn3 = pooled.getReadWriteConnection(null);
		assertSame(conn2, conn3);
		conn3.queryForLong(pingStatement);
		pooled.releaseConnection(conn3);
	} finally {
		pooled.close();
	}
}
 
Example 7
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testDeleteThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.delete(foo);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 8
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testDeleteIdsThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		List<Integer> foos = new ArrayList<Integer>();
		foos.add(foo.id);
		dao.deleteIds(foos);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 9
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testUpdatePreparedThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		UpdateBuilder<Foo, Integer> ub = dao.updateBuilder();
		ub.updateColumnValue(Foo.EQUAL_COLUMN_NAME, 1);
		dao.update(ub.prepare());
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 10
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testUpdateIdThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		// close connection
		conn.close();
		dao.updateId(foo, foo.id + 1);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 11
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testCreateThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.create(foo);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 12
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testDeleteByIdThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.deleteById(foo.id);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 13
Source File: CloseableIteratorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testIteratorThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.iterator();
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 14
Source File: JdbcPooledConnectionSourceTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testAuthTestConnection() throws Exception {
	JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL);
	long delay = 100;
	pooled.setCheckConnectionsEveryMillis(delay);
	String pingStatement = pooled.getDatabaseType().getPingStatement();
	try {
		DatabaseConnection conn1 = pooled.getReadWriteConnection(null);
		conn1.queryForLong(pingStatement);
		pooled.releaseConnection(conn1);
		// make it test ok once
		Thread.sleep(delay + 50);
		DatabaseConnection conn2 = pooled.getReadWriteConnection(null);
		assertSame(conn1, conn2);
		conn2.queryForLong(pingStatement);
		pooled.releaseConnection(conn2);
		// close it behind the pool's back, bad dog
		conn2.close();
		// now it should find out that the connection is bad and pull it
		Thread.sleep(delay + 50);
		DatabaseConnection conn3 = pooled.getReadWriteConnection(null);
		assertNotSame(conn2, conn3);
		// this should work
		conn3.queryForLong(pingStatement);
		pooled.releaseConnection(conn3);
	} finally {
		pooled.close();
	}
}
 
Example 15
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testQueryRawObjectsThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.queryRaw("SELECT * FROM FOO", new DataType[0]);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 16
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testUpdateRawThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.updateRaw("DELETE FROM FOO");
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 17
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testQueryRawStringsThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.queryRaw("SELECT * FROM FOO");
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 18
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testRefreshThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		dao.refresh(foo);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 19
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testDeleteCollectionThrow() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
	try {
		conn.close();
		List<Foo> foos = new ArrayList<Foo>();
		foos.add(foo);
		dao.delete(foos);
	} finally {
		connectionSource.releaseConnection(conn);
	}
}
 
Example 20
Source File: DataSourceConnectionSourceTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testDscsGetReadWrite() throws Exception {
	DataSource dataSource = createMock(DataSource.class);
	Connection conn = createMock(Connection.class);
	conn.close();
	expect(dataSource.getConnection()).andReturn(null);
	expect(dataSource.getConnection()).andReturn(null);
	expect(dataSource.getConnection()).andReturn(conn);
	replay(dataSource, conn);
	DataSourceConnectionSource dcs = new DataSourceConnectionSource(dataSource, DEFAULT_DATABASE_URL);
	DatabaseConnection jdbcConn = dcs.getReadWriteConnection(TABLE_NAME);
	jdbcConn.close();
	dcs.close();
	verify(dataSource, conn);
}