1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.sql.DriverManager ; 12 import java.sql.SQLException ; 13 import java.sql.Connection ; 14 import java.util.Properties ; 15 16 24 public class ConnectionPoolTest extends AbstractProxoolTest { 25 26 private static final Log LOG = LogFactory.getLog(ConnectionPoolTest.class); 27 28 public ConnectionPoolTest(String name) { 29 super(name); 30 } 31 32 36 public void testMaximumConnectionCount() throws Exception { 37 38 String testName = "maximumConnectionCount"; 39 String alias = testName; 40 41 String url = TestHelper.buildProxoolUrl(alias, 42 TestConstants.HYPERSONIC_DRIVER, 43 TestConstants.HYPERSONIC_TEST_URL); 44 Properties info = new Properties (); 45 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 46 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 47 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "2"); 48 info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true"); 49 ProxoolFacade.registerConnectionPool(url, info); 50 51 DriverManager.getConnection(url); 52 DriverManager.getConnection(url); 53 54 try { 55 DriverManager.getConnection(url); 56 fail("Didn't expect to get third connection"); 57 } catch (SQLException e) { 58 LOG.debug("Ignoring expected exception: " + e.getMessage()); 60 } 61 62 assertEquals("activeConnectionCount", 2, ProxoolFacade.getSnapshot(alias, true).getActiveConnectionCount()); 63 64 } 65 66 69 public void testMaximumEqualsMinimumConnectionCount() throws Exception { 70 71 String testName = "maximumEqualsMinimumConnectionCount"; 72 String alias = testName; 73 74 String url = TestHelper.buildProxoolUrl(alias, 75 TestConstants.HYPERSONIC_DRIVER, 76 TestConstants.HYPERSONIC_TEST_URL); 77 Properties info = new Properties (); 78 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 79 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 80 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "1"); 81 info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "1"); 82 info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true"); 83 ProxoolFacade.registerConnectionPool(url, info); 84 85 Connection c1 = DriverManager.getConnection(url); 87 c1.close(); 88 89 Connection c2 = DriverManager.getConnection(url); 91 92 try { 93 Connection c3 = DriverManager.getConnection(url); 94 c3.close(); 95 fail("Didn't expect to get third connection"); 96 } catch (SQLException e) { 97 LOG.debug("Ignoring expected exception: " + e.getMessage()); 99 } 100 101 assertEquals("activeConnectionCount", 1, ProxoolFacade.getSnapshot(alias, true).getActiveConnectionCount()); 102 c2.close(); 103 104 DriverManager.getConnection(url).close(); 106 107 assertEquals("activeConnectionCount", 0, ProxoolFacade.getSnapshot(alias, true).getActiveConnectionCount()); 108 109 } 110 111 114 public void testShutdownPatience() throws ProxoolException, SQLException { 115 116 String testName = "shutdownPatience"; 117 String alias = testName; 118 119 String url = TestHelper.buildProxoolUrl(alias, 120 TestConstants.HYPERSONIC_DRIVER, 121 TestConstants.HYPERSONIC_TEST_URL); 122 Properties info = new Properties (); 123 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 124 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true"); 125 ProxoolFacade.registerConnectionPool(url, info); 126 127 new Thread (new Closer(DriverManager.getConnection(url), 5000)).start(); 129 130 long startTime = System.currentTimeMillis(); 131 ProxoolFacade.removeConnectionPool(alias, 100000); 132 long shutdownTime = System.currentTimeMillis() - startTime; 133 assertTrue("shutdown took too long", shutdownTime < 50000); 134 assertTrue("shutdown was too quick", shutdownTime > 2000); 135 } 136 137 141 public void testShutdownImpatience() throws ProxoolException, SQLException { 142 143 String testName = "shutdownImpatience"; 144 String alias = testName; 145 146 String url = TestHelper.buildProxoolUrl(alias, 147 TestConstants.HYPERSONIC_DRIVER, 148 TestConstants.HYPERSONIC_TEST_URL); 149 Properties info = new Properties (); 150 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 151 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 152 info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true"); 153 ProxoolFacade.registerConnectionPool(url, info); 154 155 new Thread (new Closer(DriverManager.getConnection(url), 100000)).start(); 157 158 long startTime = System.currentTimeMillis(); 159 ProxoolFacade.removeConnectionPool(alias, 3000); 160 long shutdownTime = System.currentTimeMillis() - startTime; 161 assertTrue("shutdown took too long", shutdownTime < 50000); 162 assertTrue("shutdown was too quick", shutdownTime > 1000); 163 } 164 165 class Closer implements Runnable { 166 167 private Connection connection; 168 169 private long duration; 170 171 public Closer(Connection connection, long duration) { 172 this.connection = connection; 173 this.duration = duration; 174 } 175 176 public void run() { 177 long startTime = System.currentTimeMillis(); 178 try { 179 Thread.sleep(duration); 180 } catch (InterruptedException e) { 181 LOG.error("Awoken", e); 182 } 183 try { 184 connection.close(); 185 LOG.debug("Connection closed after " + (System.currentTimeMillis() - startTime) 186 + " milliseconds."); 187 } catch (SQLException e) { 188 LOG.error("Problem closing connection", e); 189 } 190 } 191 192 } 193 194 } 195 196 238 | Popular Tags |