@Override @Before public void before() throws Exception { if (connectionSource != null) { return; } super.before(); if (connectionSource != null) { connectionSource = new JdbcPooledConnectionSource(databaseUrl, userName, password); } }
@Test(expected = IOException.class) public void testCloseNoInit() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(); pooled.close(); }
@Test public void testReleaseUnknown() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { JdbcDatabaseConnection conn1 = new JdbcDatabaseConnection(DriverManager.getConnection(DEFAULT_DATABASE_URL)); pooled.releaseConnection(conn1); } finally { pooled.close(); } }
@Test(expected = SQLException.class) public void testGetReadWriteNoInit() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(); try { pooled.getReadWriteConnection(null); } finally { IOUtils.closeQuietly(pooled); } }
@Test(expected = SQLException.class) public void testReleaseNoInit() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(); try { pooled.releaseConnection(null); } finally { IOUtils.closeQuietly(pooled); } }
@Test public void testClearOtherConnection() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { JdbcDatabaseConnection conn1 = new JdbcDatabaseConnection(DriverManager.getConnection(DEFAULT_DATABASE_URL)); JdbcDatabaseConnection conn2 = new JdbcDatabaseConnection(DriverManager.getConnection(DEFAULT_DATABASE_URL)); pooled.saveSpecialConnection(conn1); pooled.clearSpecialConnection(conn2); } finally { pooled.close(); } }
@Test(expected = SQLException.class) public void testSaveOtherConnection() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { JdbcDatabaseConnection conn1 = new JdbcDatabaseConnection(DriverManager.getConnection(DEFAULT_DATABASE_URL)); JdbcDatabaseConnection conn2 = new JdbcDatabaseConnection(DriverManager.getConnection(DEFAULT_DATABASE_URL)); pooled.saveSpecialConnection(conn1); pooled.saveSpecialConnection(conn2); } finally { pooled.close(); } }
@Test public void testReleaseAfterClose() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); DatabaseConnection conn1; try { conn1 = pooled.getReadOnlyConnection(null); } finally { pooled.close(); } pooled.releaseConnection(conn1); }
@Test public void testReleaseAlreadyClosedUnknown() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { JdbcDatabaseConnection conn1 = new JdbcDatabaseConnection(DriverManager.getConnection(DEFAULT_DATABASE_URL)); conn1.close(); pooled.releaseConnection(conn1); } finally { pooled.close(); } }
@Test(expected = SQLException.class) public void testSaveTransaction() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(); pooled.initialize(); try { pooled.saveSpecialConnection(null); } finally { IOUtils.closeQuietly(pooled); } }
@Test(expected = SQLException.class) public void testClearTransaction() throws SQLException { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(); pooled.initialize(); try { pooled.clearSpecialConnection(null); } finally { IOUtils.closeQuietly(pooled); } }
@Test public void testMaxAgeMaxValue() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { pooled.setMaxConnectionAgeMillis(Long.MAX_VALUE); DatabaseConnection conn1 = pooled.getReadOnlyConnection(null); pooled.releaseConnection(conn1); } finally { pooled.close(); } }
@Test public void testReleaseAlreadyClosed() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { DatabaseConnection conn1 = pooled.getReadOnlyConnection(null); conn1.close(); pooled.releaseConnection(conn1); } finally { pooled.close(); } }
@Test public void testNestedConnection() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { JdbcDatabaseConnection conn1 = new JdbcDatabaseConnection(DriverManager.getConnection(DEFAULT_DATABASE_URL)); pooled.saveSpecialConnection(conn1); pooled.saveSpecialConnection(conn1); pooled.releaseConnection(conn1); pooled.releaseConnection(conn1); } finally { pooled.close(); } }
@Test public void testConstructors() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); assertEquals(DEFAULT_DATABASE_URL, pooled.getUrl()); pooled.close(); pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL, null, null); assertEquals(DEFAULT_DATABASE_URL, pooled.getUrl()); pooled.close(); DatabaseType databaseType = DatabaseTypeUtils.createDatabaseType(DEFAULT_DATABASE_URL); pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL, databaseType); assertEquals(DEFAULT_DATABASE_URL, pooled.getUrl()); assertSame(databaseType, pooled.getDatabaseType()); pooled.close(); pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL, null, null, databaseType); assertEquals(DEFAULT_DATABASE_URL, pooled.getUrl()); assertSame(databaseType, pooled.getDatabaseType()); pooled.close(); }
@Test public void testMaxAge() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { long maxAge = 500; pooled.setMaxConnectionAgeMillis(maxAge); DatabaseConnection conn1 = pooled.getReadOnlyConnection(null); pooled.releaseConnection(conn1); Thread.sleep(maxAge + 1); DatabaseConnection conn2 = pooled.getReadOnlyConnection(null); assertNotSame(conn1, conn2); } finally { pooled.close(); } }
@Test public void testSetAutoCommit() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { DatabaseConnection conn1 = pooled.getReadOnlyConnection(null); conn1.setAutoCommit(false); pooled.releaseConnection(conn1); DatabaseConnection conn2 = pooled.getReadOnlyConnection(null); assertSame(conn1, conn2); assertTrue(conn2.isAutoCommit()); } finally { pooled.close(); } }
@Test public void testIsOpen() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { pooled.setUrl("jdbc:h2:mem:baz"); assertTrue(pooled.isOpen(null)); DatabaseConnection conn = pooled.getReadOnlyConnection(null); pooled.releaseConnection(conn); assertTrue(pooled.isOpen(null)); } finally { pooled.close(); assertFalse(pooled.isOpen(null)); } }
@Test(expected = SQLException.class) public void testTestConnectionThatWasClosed() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); String pingStatement = pooled.getDatabaseType().getPingStatement(); 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); assertSame(conn1, conn2); conn2.queryForLong(pingStatement); } finally { pooled.close(); } }
@Test public void testMaxFree() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); try { pooled.setMaxConnectionsFree(2); DatabaseConnection conn1 = pooled.getReadWriteConnection(null); DatabaseConnection conn2 = pooled.getReadWriteConnection(null); DatabaseConnection conn3 = pooled.getReadWriteConnection(null); pooled.releaseConnection(conn1); pooled.releaseConnection(conn2); pooled.releaseConnection(conn3); DatabaseConnection conn4 = pooled.getReadWriteConnection(null); // not conn1 which should be closed assertSame(conn2, conn4); } finally { pooled.close(); } }