Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.j256.ormlite.jdbc;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertFalse;
  import static org.junit.Assert.assertNotSame;
  import static org.junit.Assert.assertSame;
  import static org.junit.Assert.assertTrue;
  
 
 import org.junit.Test;
 
 
 
 	private static final String DEFAULT_DATABASE_URL = "jdbc:h2:mem:ormlite";
 
 	@Test
 	public void testBasic() throws Exception {
 		try {
 			assertNotSame(conn1conn2);
 			pooled.releaseConnection(conn2);
 			assertNotSame(conn1conn3);
 			assertSame(conn2conn3);
 			assertNotSame(conn1conn4);
 			assertNotSame(conn3conn4);
 			pooled.releaseConnection(conn1);
 			pooled.releaseConnection(conn3);
 			assertSame(conn1conn5);
 			assertSame(conn3conn6);
 			conn1.close();
 			conn2.close();
 			conn3.close();
 			conn4.close();
 			conn5.close();
 			conn6.close();
 		} finally {
 			pooled.close();
 		}
 	}
 
 	@Test
 	public void testTransaction() throws Exception {
 		try {
 			pooled.saveSpecialConnection(conn2);
 			assertSame(conn2conn3);
 			pooled.releaseConnection(conn3);
 			pooled.releaseConnection(conn1);
 			assertSame(conn2conn4);
 			pooled.releaseConnection(conn4);
 			pooled.clearSpecialConnection(conn2);
 			assertSame(conn1conn5);
 			conn1.close();
 			conn2.close();
 			conn3.close();
 			conn4.close();
 			conn5.close();
 		} finally {
 			pooled.close();
 		}
 	}
 
 	@Test
 	public void testMaxFree() throws Exception {
 		try {
 			pooled.releaseConnection(conn1);
 			pooled.releaseConnection(conn2);
 			pooled.releaseConnection(conn3);
 			// not conn1 which should be closed
 			assertSame(conn2conn4);
 		} finally {
 			pooled.close();
 		}
 	}
 
 	@Test
	public void testReleaseAfterClose() throws Exception {
		try {
			conn1 = pooled.getReadOnlyConnection();
finally {
			pooled.close();
		}
		pooled.releaseConnection(conn1);
	}
	public void testMaxAge() throws Exception {
		try {
			long maxAge = 500;
			pooled.releaseConnection(conn1);
			Thread.sleep(maxAge + 1);
			assertNotSame(conn1conn2);
finally {
			pooled.close();
		}
	}
	public void testClosedConnction() throws Exception {
		try {
			pooled.releaseConnection(conn1);
			// wait a bit
			Thread.sleep(200);
			// should get the same connection
			assertSame(conn1conn2);
			pooled.releaseConnection(conn2);
			// close it behind the scenes
			conn2.close();
			// wait a bit
			Thread.sleep(200);
			// now it should be different
			assertTrue(conn3 != conn1);
			Thread.sleep(200);
			assertTrue("loop counter is " + pooled.getTestLoopCount(), pooled.getTestLoopCount() < 100);
finally {
			pooled.close();
		}
	}
	public void testMaxAgeMaxValue() throws Exception {
		try {
			pooled.releaseConnection(conn1);
finally {
			pooled.close();
		}
	}
	@Test(expected = SQLException.class)
	public void testCloseNoInit() throws Exception {
		pooled.close();
	}
	@Test(expected = SQLException.class)
	public void testGetReadWriteNoInit() throws Exception {
		try {
finally {
			pooled.close();
		}
	}
	@Test(expected = SQLException.class)
	public void testReleaseNoInit() throws Exception {
		try {
			pooled.releaseConnection(null);
finally {
			pooled.close();
		}
	}
	@Test(expected = SQLException.class)
	public void testSaveTransaction() throws Exception {
		try {
finally {
			pooled.close();
		}
	}
	@Test(expected = SQLException.class)
	public void testClearTransaction() throws Exception {
		try {
finally {
			pooled.close();
		}
	}
	public void testUsageCounts() throws Exception {
		try {
			pooled.releaseConnection(conn2);
			pooled.releaseConnection(conn1);
finally {
			pooled.close();
		}
	}
	public void testReleaseAlreadyClosed() throws Exception {
		try {
			conn1.close();
			pooled.releaseConnection(conn1);
finally {
			pooled.close();
		}
	}
	public void testReleaseUnknown() throws Exception {
		try {
			pooled.releaseConnection(conn1);
finally {
			pooled.close();
		}
	}
	public void testReleaseAlreadyClosedUnknown() throws Exception {
		try {
			conn1.close();
			pooled.releaseConnection(conn1);
finally {
			pooled.close();
		}
	}
	public void testConstructors() throws Exception {
		pooled = new JdbcPooledConnectionSource(nullnull);
		DatabaseType databaseType = DatabaseTypeUtils.createDatabaseType();
		pooled = new JdbcPooledConnectionSource(databaseType);
		assertSame(databaseTypepooled.getDatabaseType());
		pooled = new JdbcPooledConnectionSource(nullnulldatabaseType);
		assertSame(databaseTypepooled.getDatabaseType());
	}
	public void testNestedConnection() throws Exception {
		try {
			pooled.saveSpecialConnection(conn1);
			pooled.saveSpecialConnection(conn1);
			pooled.releaseConnection(conn1);
			pooled.releaseConnection(conn1);
finally {
			pooled.close();
		}
	}
	@Test(expected = SQLException.class)
	public void testSaveOtherConnection() throws Exception {
		try {
			pooled.saveSpecialConnection(conn1);
			pooled.saveSpecialConnection(conn2);
finally {
			pooled.close();
		}
	}
	public void testClearOtherConnection() throws Exception {
		try {
			pooled.saveSpecialConnection(conn1);
finally {
			pooled.close();
		}
	}
	@Test(expected = SQLException.class)
	public void testTestConnectionThatWasClosed() throws Exception {
		String pingStatement = pooled.getDatabaseType().getPingStatement();
		try {
			conn1.queryForLong(pingStatement);
			pooled.releaseConnection(conn1);
			// close it behind the pool's back, bad dog
			conn1.close();
			assertSame(conn1conn2);
			conn2.queryForLong(pingStatement);
finally {
			pooled.close();
		}
	}
	public void testAuthTestConnection() throws Exception {
		long delay = 100;
		String pingStatement = pooled.getDatabaseType().getPingStatement();
		try {
			conn1.queryForLong(pingStatement);
			pooled.releaseConnection(conn1);
			// make it test ok once
			Thread.sleep(delay * 2);
			assertSame(conn1conn2);
			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 * 2);
			assertNotSame(conn2conn3);
			// this should work
			conn3.queryForLong(pingStatement);
			pooled.releaseConnection(conn3);
finally {
			pooled.close();
		}
	}
	public void testAuthTestConnectionExpired() throws Exception {
		long delay = 100;
		String pingStatement = pooled.getDatabaseType().getPingStatement();
		try {
			conn1.queryForLong(pingStatement);
			pooled.releaseConnection(conn1);
			// make it test ok once
			Thread.sleep(delay * 2);
			assertNotSame(conn1conn2);
			conn2.queryForLong(pingStatement);
			pooled.releaseConnection(conn2);
finally {
			pooled.close();
		}
	}
		String pingStatement = pooled.getDatabaseType().getPingStatement();
		pooled.setTestBeforeGet(true);
		try {
			conn1.queryForLong(pingStatement);
			pooled.releaseConnection(conn1);
			// close it behind the pool's back, bad dog
			conn1.close();
			assertNotSame(conn1conn2);
			conn2.queryForLong(pingStatement);
			pooled.releaseConnection(conn2);
			assertSame(conn2conn3);
			conn3.queryForLong(pingStatement);
			pooled.releaseConnection(conn3);
finally {
			pooled.close();
		}
	}
	public void testIsOpen() throws Exception {
		pooled.setUrl("jdbc:h2:mem:baz");
		assertTrue(pooled.isOpen());
		pooled.releaseConnection(conn);
		assertTrue(pooled.isOpen());
		pooled.close();
	}
New to GrepCode? Check out our FAQ X