1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java,v 1.17 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2.optional;
import org.postgresql.jdbc2.optional.ConnectionPool;
import org.postgresql.ds.PGConnectionPoolDataSource;
import org.postgresql.test.TestUtil;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import java.io.*;
/**
* Tests for the ConnectionPoolDataSource and PooledConnection
* implementations. They are tested together because the only client
* interface to the PooledConnection is through the CPDS.
*
* @author Aaron Mulder (ammulder@chariotsolutions.com)
*/
public class ConnectionPoolTest extends BaseDataSourceTest
{
private ArrayList connections = new ArrayList();
/**
* Constructor required by JUnit
*/
public ConnectionPoolTest(String name)
{
super(name);
}
/**
* Creates and configures a ConnectionPool
*/
protected void initializeDataSource()
{
if (bds == null)
{
bds = new ConnectionPool();
bds.setServerName(TestUtil.getServer());
bds.setPortNumber(TestUtil.getPort());
bds.setDatabaseName(TestUtil.getDatabase());
bds.setUser(TestUtil.getUser());
bds.setPassword(TestUtil.getPassword());
}
}
protected void tearDown() throws Exception
{
for (Iterator i = connections.iterator(); i.hasNext(); ) {
PooledConnection c = (PooledConnection) i.next();
try {
c.close();
} catch (Exception ex) {
// close throws nullptr or other evil things if the connection
// is already closed
}
}
}
/**
* Though the normal client interface is to grab a Connection, in
* order to test the middleware/server interface, we need to deal
* with PooledConnections. Some tests use each.
*/
protected PooledConnection getPooledConnection() throws SQLException
{
initializeDataSource();
// we need to recast to PGConnectionPool rather than
// jdbc.optional.ConnectionPool because our ObjectFactory
// returns only the top level class, not the specific
// jdbc2/jdbc3 implementations.
PooledConnection c = ((PGConnectionPoolDataSource)bds).getPooledConnection();
connections.add(c);
return c;
}
/**
* Instead of just fetching a Connection from the ConnectionPool,
* get a PooledConnection, add a listener to close it when the
* Connection is closed, and then get the Connection. Without
* the listener the PooledConnection (and thus the physical connection)
* would never by closed. Probably not a disaster during testing, but
* you never know.
*/
protected Connection getDataSourceConnection() throws SQLException
{
initializeDataSource();
final PooledConnection pc = getPooledConnection();
// Since the pooled connection won't be reused in these basic tests, close it when the connection is closed
pc.addConnectionEventListener(new ConnectionEventListener()
{
public void connectionClosed(ConnectionEvent event)
{
try
{
pc.close();
}
catch (SQLException e)
{
fail("Unable to close PooledConnection: " + e);
}
}
public void connectionErrorOccurred(ConnectionEvent event)
{
}
}
);
return pc.getConnection();
}
/**
* Makes sure that if you get a connection from a PooledConnection,
* close it, and then get another one, you're really using the same
* physical connection. Depends on the implementation of toString
* for the connection handle.
*/
public void testPoolReuse()
{
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
String name = con.toString();
con.close();
con = pc.getConnection();
String name2 = con.toString();
con.close();
pc.close();
assertTrue("Physical connection doesn't appear to be reused across PooledConnection wrappers", name.equals(name2));
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that when you request a connection from the
* PooledConnection, and previous connection it might have given
* out is closed. See JDBC 2.0 Optional Package spec section
* 6.2.3
*/
public void testPoolCloseOldWrapper()
{
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
Connection con2 = pc.getConnection();
try
{
con.createStatement();
fail("Original connection wrapper should be closed when new connection wrapper is generated");
}
catch (SQLException e)
{
}
con2.close();
pc.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that if you get two connection wrappers from the same
* PooledConnection, they are different, even though the represent
* the same physical connection. See JDBC 2.0 Optional Pacakge spec
* section 6.2.2
*/
public void testPoolNewWrapper()
{
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
Connection con2 = pc.getConnection();
con2.close();
pc.close();
assertTrue("Two calls to PooledConnection.getConnection should not return the same connection wrapper", con != con2);
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that exactly one close event is fired for each time a
* connection handle is closed. Also checks that events are not
* fired after a given handle has been closed once.
*/
public void testCloseEvent()
{
try
{
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
// a double close shouldn't fire additional events
con.close();
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
pc.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that close events are not fired after a listener has
* been removed.
*/
public void testNoCloseEvent()
{
try
{
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
pc.removeConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that a listener can be removed while dispatching
* events. Sometimes this causes a ConcurrentModificationException
* or something.
*/
public void testInlineCloseEvent()
{
try
{
PooledConnection pc = getPooledConnection();
RemoveClose rc1 = new RemoveClose();
RemoveClose rc2 = new RemoveClose();
RemoveClose rc3 = new RemoveClose();
pc.addConnectionEventListener(rc1);
pc.addConnectionEventListener(rc2);
pc.addConnectionEventListener(rc3);
con = pc.getConnection();
con.close();
con = pc.getConnection();
con.close();
}
catch (Exception e)
{
fail(e.getMessage());
}
}
/**
* Tests that a close event is not generated when a connection
* handle is closed automatically due to a new connection handle
* being opened for the same PooledConnection. See JDBC 2.0
* Optional Package spec section 6.3
*/
public void testAutomaticCloseEvent()
{
try
{
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
// Open a 2nd connection, causing the first to be closed. No even should be generated.
Connection con2 = pc.getConnection();
assertTrue("Connection handle was not closed when new handle was opened", con.isClosed());
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con2.close();
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
pc.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure the isClosed method on a connection wrapper does what
* you'd expect. Checks the usual case, as well as automatic
* closure when a new handle is opened on the same physical connection.
*/
public void testIsClosed()
{
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
assertTrue(!con.isClosed());
con.close();
assertTrue(con.isClosed());
con = pc.getConnection();
Connection con2 = pc.getConnection();
assertTrue(con.isClosed());
assertTrue(!con2.isClosed());
con2.close();
assertTrue(con.isClosed());
pc.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Ensures that a statement generated by a proxied connection returns the
* proxied connection from getConnection() [not the physical connection].
*/
public void testStatementConnection() {
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
Statement s = con.createStatement();
Connection conRetrieved = s.getConnection();
assertTrue(con.getClass().equals(conRetrieved.getClass()));
assertTrue(con.equals(conRetrieved));
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Ensures that the Statement proxy generated by the Connection handle
* throws the correct kind of exception.
*/
public void testStatementProxy() {
Statement s = null;
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
s = con.createStatement();
}
catch (SQLException e)
{
fail(e.getMessage());
}
try
{
s.executeQuery("SELECT * FROM THIS_TABLE_SHOULD_NOT_EXIST");
fail("An SQL exception was not thrown that should have been");
}
catch (SQLException e)
{
; // This is the expected and correct path
}
catch (Exception e)
{
fail("bad exception; was expecting SQLException, not" +
e.getClass().getName());
}
}
/**
* Ensures that a prepared statement generated by a proxied connection
* returns the proxied connection from getConnection() [not the physical
* connection].
*/
public void testPreparedStatementConnection() {
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
PreparedStatement s = con.prepareStatement("select 'x'");
Connection conRetrieved = s.getConnection();
assertTrue(con.getClass().equals(conRetrieved.getClass()));
assertTrue(con.equals(conRetrieved));
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Ensures that a callable statement generated by a proxied connection
* returns the proxied connection from getConnection() [not the physical
* connection].
*/
public void testCallableStatementConnection() {
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
CallableStatement s = con.prepareCall("select 'x'");
Connection conRetrieved = s.getConnection();
assertTrue(con.getClass().equals(conRetrieved.getClass()));
assertTrue(con.equals(conRetrieved));
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Ensure that a statement created from a pool can be used
* like any other statement in regard to pg extensions.
*/
public void testStatementsProxyPGStatement() {
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
Statement s = con.createStatement();
boolean b = ((org.postgresql.PGStatement)s).isUseServerPrepare();
PreparedStatement ps = con.prepareStatement("select 'x'");
b = ((org.postgresql.PGStatement)ps).isUseServerPrepare();
CallableStatement cs = con.prepareCall("select 'x'");
b = ((org.postgresql.PGStatement)cs).isUseServerPrepare();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Helper class to remove a listener during event dispatching.
*/
private class RemoveClose implements ConnectionEventListener
{
public void connectionClosed(ConnectionEvent event)
{
((PooledConnection)event.getSource()).removeConnectionEventListener(this);
}
public void connectionErrorOccurred(ConnectionEvent event)
{
((PooledConnection)event.getSource()).removeConnectionEventListener(this);
}
}
/**
* Helper class that implements the event listener interface, and
* counts the number of events it sees.
*/
private class CountClose implements ConnectionEventListener
{
private int count = 0, errorCount = 0;
public void connectionClosed(ConnectionEvent event)
{
count++;
}
public void connectionErrorOccurred(ConnectionEvent event)
{
errorCount++;
}
public int getCount()
{
return count;
}
public int getErrorCount()
{
return errorCount;
}
public void clear()
{
count = errorCount = 0;
}
}
public void testSerializable() throws IOException, ClassNotFoundException
{
ConnectionPool pool = new ConnectionPool();
pool.setDefaultAutoCommit(false);
pool.setServerName("db.myhost.com");
pool.setDatabaseName("mydb");
pool.setUser("user");
pool.setPassword("pass");
pool.setPortNumber(1111);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(pool);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
ConnectionPool pool2 = (ConnectionPool)ois.readObject();
assertEquals(pool.isDefaultAutoCommit(), pool2.isDefaultAutoCommit());
assertEquals(pool.getServerName(), pool2.getServerName());
assertEquals(pool.getDatabaseName(), pool2.getDatabaseName());
assertEquals(pool.getUser(), pool2.getUser());
assertEquals(pool.getPassword(), pool2.getPassword());
assertEquals(pool.getPortNumber(), pool2.getPortNumber());
}
}
|