1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.logicalcobwebs.proxool.admin.SnapshotIF; 11 12 import java.sql.Connection ; 13 import java.sql.DriverManager ; 14 import java.sql.Statement ; 15 import java.sql.SQLException ; 16 import java.util.Properties ; 17 18 26 public class HouseKeeperTest extends AbstractProxoolTest { 27 28 private static final Log LOG = LogFactory.getLog(HouseKeeperTest.class); 29 30 public HouseKeeperTest(String alias) { 31 super(alias); 32 } 33 34 38 public void testMaximumActiveTime() throws Exception { 39 40 ConnectionResetter.setTriggerResetException(true); 41 String testName = "maximumActiveTime"; 42 String alias = testName; 43 44 String url = TestHelper.buildProxoolUrl(alias, 45 TestConstants.HYPERSONIC_DRIVER, 46 TestConstants.HYPERSONIC_TEST_URL); 47 Properties info = new Properties (); 48 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 49 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 50 info.setProperty(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, "1000"); 51 info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "1"); 52 info.setProperty(ProxoolConstants.TRACE_PROPERTY, "true"); 53 info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000"); 54 ProxoolFacade.registerConnectionPool(url, info); 55 56 assertEquals("Shouldn't be any active connections yet", 0, ProxoolFacade.getSnapshot(alias, false).getServedCount()); 57 58 final Connection connection = DriverManager.getConnection(url); 59 connection.setAutoCommit(false); 60 connection.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL); 61 long start = System.currentTimeMillis(); 62 63 assertEquals("We just opened 1 connection", 1, ProxoolFacade.getSnapshot(alias, false).getServedCount()); 64 65 new ResultMonitor() { 66 public boolean check() throws Exception { 67 return connection.isClosed(); 68 } 69 }.getResult(); 70 try { 71 Thread.sleep(3000); 72 } catch (InterruptedException e) { 73 LOG.debug("Awoken."); 74 } 75 76 long elapsed = System.currentTimeMillis() - start; 77 assertTrue("Connection has not been closed after " + elapsed + " milliseconds as expected", connection.isClosed()); 78 assertEquals("Expected the connection to be inactive", 0, ProxoolFacade.getSnapshot(alias, false).getActiveConnectionCount()); 79 80 try { 81 connection.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL); 82 fail("Calling createStatement() on a closed connection should fail"); 83 } catch (Exception e) { 84 LOG.debug("Ignoring expected exception: " + e.getMessage()); 86 } 87 88 connection.close(); 91 92 try { 94 Thread.sleep(3000); 95 } catch (InterruptedException e) { 96 LOG.debug("Awoken."); 97 } 98 SnapshotIF snapshot = ProxoolFacade.getSnapshot(alias, false); 99 assertEquals("activeConnectionCount", 0, snapshot.getActiveConnectionCount()); 100 assertEquals("availableConnectionCount", 1, snapshot.getAvailableConnectionCount()); 101 assertEquals("connectionCount", 1, snapshot.getConnectionCount()); 102 103 } 104 105 110 public void testMaximumActiveTimeWithResetFailure() throws Exception { 111 112 try { 113 ConnectionResetter.setTriggerResetException(true); 114 String testName = "maximumActiveTimeWithResetFailure"; 115 String alias = testName; 116 117 String url = TestHelper.buildProxoolUrl(alias, 118 TestConstants.HYPERSONIC_DRIVER, 119 TestConstants.HYPERSONIC_TEST_URL); 120 Properties info = new Properties (); 121 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 122 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 123 info.setProperty(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, "1000"); 124 info.setProperty(ProxoolConstants.TRACE_PROPERTY, "true"); 125 info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000"); 126 ProxoolFacade.registerConnectionPool(url, info); 127 128 assertEquals("Shouldn't be any active connections yet", 0, ProxoolFacade.getSnapshot(alias, false).getServedCount()); 129 130 final Connection connection = DriverManager.getConnection(url); 131 connection.setAutoCommit(false); 132 connection.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL); 133 long start = System.currentTimeMillis(); 134 135 assertEquals("We just opened 1 connection", 1, ProxoolFacade.getSnapshot(alias, false).getServedCount()); 136 137 new ResultMonitor() { 138 public boolean check() throws Exception { 139 return connection.isClosed(); 140 } 141 }.getResult(); 142 try { 143 Thread.sleep(3000); 144 } catch (InterruptedException e) { 145 LOG.debug("Awoken."); 146 } 147 148 long elapsed = System.currentTimeMillis() - start; 149 assertTrue("Connection has not been closed after " + elapsed + " milliseconds as expected", connection.isClosed()); 150 assertEquals("Expected the connection to be inactive", 0, ProxoolFacade.getSnapshot(alias, false).getActiveConnectionCount()); 151 152 try { 153 connection.createStatement().executeQuery(TestConstants.HYPERSONIC_TEST_SQL); 154 fail("Calling createStatement() on a closed connection should fail"); 155 } catch (Exception e) { 156 LOG.debug("Ignoring expected exception: " + e.getMessage()); 158 } 159 160 connection.close(); 163 164 try { 166 Thread.sleep(3000); 167 } catch (InterruptedException e) { 168 LOG.debug("Awoken."); 169 } 170 SnapshotIF snapshot = ProxoolFacade.getSnapshot(alias, false); 171 assertEquals("activeConnectionCount", 0, snapshot.getActiveConnectionCount()); 172 assertEquals("availableConnectionCount", 0, snapshot.getAvailableConnectionCount()); 173 assertEquals("connectionCount", 0, snapshot.getConnectionCount()); 174 } finally { 175 ConnectionResetter.setTriggerResetException(false); 177 } 178 179 } 180 181 185 public void testHouseKeeperTestSql() throws Exception { 186 187 String testName = "houseKeeperTestSql"; 188 String alias = testName; 189 190 String url = TestHelper.buildProxoolUrl(alias, 191 TestConstants.HYPERSONIC_DRIVER, 192 TestConstants.HYPERSONIC_TEST_URL); 193 Properties info = new Properties (); 194 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 195 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 196 info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, "SELECT NOW"); 197 info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000"); 198 ProxoolFacade.registerConnectionPool(url, info); 199 200 DriverManager.getConnection(url).close(); 201 202 try { 203 Thread.sleep(3000); 204 } catch (InterruptedException e) { 205 LOG.debug("Awoken."); 206 } 207 208 DriverManager.getConnection(url).close(); 209 } 210 211 215 public void testInvalidBeforeUse() throws Exception { 216 217 String testName = "invalidBeforeUse"; 218 String alias = testName; 219 220 String url = TestHelper.buildProxoolUrl(alias, 221 TestConstants.HYPERSONIC_DRIVER, 222 TestConstants.HYPERSONIC_TEST_URL); 223 Properties info = new Properties (); 224 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 225 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 226 info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, "Invalid test"); 227 info.setProperty(ProxoolConstants.TEST_BEFORE_USE_PROPERTY, Boolean.TRUE.toString()); 228 info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString()); 229 info.setProperty(ProxoolConstants.TRACE_PROPERTY, Boolean.TRUE.toString()); 230 ProxoolFacade.registerConnectionPool(url, info); 231 232 Connection connection = null; 236 Statement s = null; 237 try { 238 connection = DriverManager.getConnection(url); 239 s = connection.createStatement(); 240 s.execute(TestConstants.HYPERSONIC_TEST_SQL); 241 fail("Expected to get an exception because the test failed"); 242 } catch (SQLException e) { 243 LOG.debug("Expected exception: " + e.getMessage()); 245 } 246 247 } 248 249 253 public void testInvalidAfterUse() throws Exception { 254 255 String testName = "invalidAfterUse"; 256 String alias = testName; 257 258 String url = TestHelper.buildProxoolUrl(alias, 259 TestConstants.HYPERSONIC_DRIVER, 260 TestConstants.HYPERSONIC_TEST_URL); 261 Properties info = new Properties (); 262 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 263 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 264 info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, "Invalid test"); 265 info.setProperty(ProxoolConstants.TEST_AFTER_USE_PROPERTY, Boolean.TRUE.toString()); 266 info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString()); 267 info.setProperty(ProxoolConstants.TRACE_PROPERTY, Boolean.TRUE.toString()); 268 ProxoolFacade.registerConnectionPool(url, info); 269 270 Connection connection = null; 273 Statement s = null; 274 try { 275 connection = DriverManager.getConnection(url); 276 s = connection.createStatement(); 277 s.execute(TestConstants.HYPERSONIC_TEST_SQL); 278 } finally { 279 if (connection != null) { 280 connection.close(); 281 } 282 } 283 284 assertEquals("Available connections", 0, ProxoolFacade.getSnapshot(alias).getAvailableConnectionCount()); 287 288 } 289 290 public void testBeforeAndAfterUse() throws Exception { 291 292 String testName = "beforeAndAfterUse"; 293 String alias = testName; 294 295 String url = TestHelper.buildProxoolUrl(alias, 296 TestConstants.HYPERSONIC_DRIVER, 297 TestConstants.HYPERSONIC_TEST_URL); 298 Properties info = new Properties (); 299 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 300 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 301 info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, TestConstants.HYPERSONIC_TEST_SQL); 302 info.setProperty(ProxoolConstants.TEST_BEFORE_USE_PROPERTY, Boolean.TRUE.toString()); 303 info.setProperty(ProxoolConstants.TEST_AFTER_USE_PROPERTY, Boolean.TRUE.toString()); 304 info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString()); 305 info.setProperty(ProxoolConstants.TRACE_PROPERTY, Boolean.TRUE.toString()); 306 ProxoolFacade.registerConnectionPool(url, info); 307 308 Connection connection = null; 309 Statement s = null; 310 try { 311 connection = DriverManager.getConnection(url); 312 s = connection.createStatement(); 313 s.execute(TestConstants.HYPERSONIC_TEST_SQL); 314 } finally { 315 if (connection != null) { 316 connection.close(); 317 } 318 } 319 320 assertEquals("Available connections", 1, ProxoolFacade.getSnapshot(alias).getAvailableConnectionCount()); 322 323 } 324 325 } 326 327 | Popular Tags |