use of javax.sql.PooledConnection in project tomcat by apache.
the class KeyedCPDSConnectionFactory method validateObject.
/**
* Validates a pooled connection.
*
* @param key ignored
* @param p wrapped {@link PooledConnectionAndInfo} containing the
* connection to validate
* @return true if validation succeeds
*/
@Override
public boolean validateObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) {
try {
validateLifetime(p);
} catch (final Exception e) {
return false;
}
boolean valid = false;
final PooledConnection pconn = p.getObject().getPooledConnection();
Connection conn = null;
validatingSet.add(pconn);
if (null == _validationQuery) {
int timeout = _validationQueryTimeout;
if (timeout < 0) {
timeout = 0;
}
try {
conn = pconn.getConnection();
valid = conn.isValid(timeout);
} catch (final SQLException e) {
valid = false;
} finally {
Utils.closeQuietly(conn);
validatingSet.remove(pconn);
}
} else {
Statement stmt = null;
ResultSet rset = null;
// logical Connection from the PooledConnection must be closed
// before another one can be requested and closing it will
// generate an event. Keep track so we know not to return
// the PooledConnection
validatingSet.add(pconn);
try {
conn = pconn.getConnection();
stmt = conn.createStatement();
rset = stmt.executeQuery(_validationQuery);
if (rset.next()) {
valid = true;
} else {
valid = false;
}
if (_rollbackAfterValidation) {
conn.rollback();
}
} catch (final Exception e) {
valid = false;
} finally {
Utils.closeQuietly(rset);
Utils.closeQuietly(stmt);
Utils.closeQuietly(conn);
validatingSet.remove(pconn);
}
}
return valid;
}
use of javax.sql.PooledConnection in project tomcat by apache.
the class KeyedCPDSConnectionFactory method makeObject.
/**
* Creates a new {@link PooledConnectionAndInfo} from the given {@link UserPassKey}.
*
* @param upkey {@link UserPassKey} containing user credentials
* @throws SQLException if the connection could not be created.
* @see org.apache.tomcat.dbcp.pool2.KeyedPooledObjectFactory#makeObject(java.lang.Object)
*/
@Override
public synchronized PooledObject<PooledConnectionAndInfo> makeObject(final UserPassKey upkey) throws Exception {
PooledConnectionAndInfo pci = null;
PooledConnection pc = null;
final String username = upkey.getUsername();
final String password = upkey.getPassword();
if (username == null) {
pc = _cpds.getPooledConnection();
} else {
pc = _cpds.getPooledConnection(username, password);
}
if (pc == null) {
throw new IllegalStateException("Connection pool data source returned null from getPooledConnection");
}
// should we add this object as a listener or the pool.
// consider the validateObject method in decision
pc.addConnectionEventListener(this);
pci = new PooledConnectionAndInfo(pc, username, password);
pcMap.put(pc, pci);
return new DefaultPooledObject<>(pci);
}
use of javax.sql.PooledConnection in project tomcat by apache.
the class EqualsHashCodeTest method testEquals.
@Test
public void testEquals() throws Exception {
Connection con1 = datasource.getConnection();
Connection real1 = ((PooledConnection) con1).getConnection();
Assert.assertEquals(con1, con1);
con1.close();
Assert.assertEquals(con1, con1);
Connection con2 = datasource.getConnection();
Connection real2 = ((PooledConnection) con2).getConnection();
Assert.assertEquals(real1, real2);
Assert.assertEquals(con2, con2);
Assert.assertNotSame(con1, con2);
con2.close();
Assert.assertEquals(con2, con2);
}
use of javax.sql.PooledConnection in project voltdb by VoltDB.
the class ManagedPoolDataSource method wrapConnectionAndMarkAsInUse.
private Connection wrapConnectionAndMarkAsInUse(PooledConnection pooledConnection) throws SQLException {
pooledConnection = assureValidConnection(pooledConnection);
Connection conn = pooledConnection.getConnection();
if (doResetAutoCommit) {
conn.setAutoCommit(isAutoCommit);
}
if (doResetReadOnly) {
conn.setReadOnly(isReadOnly);
}
if (doResetTransactionIsolation) {
conn.setTransactionIsolation(transactionIsolation);
/* TESING ONLY!!
System.err.println("<<<<<<<<< ISO LVL => " + transactionIsolation
+ " >>>>>>>>>>>>");
*/
}
if (doResetCatalog) {
conn.setCatalog(catalog);
/* TESTING ONLY!
System.err.println("<<<<<<<<< CAT => " + catalog
+ " >>>>>>>>>>>>");
*/
}
if (validationQuery != null) {
// End-to-end test before return the Connection.
java.sql.ResultSet rs = null;
try {
rs = conn.createStatement().executeQuery(validationQuery);
if (!rs.next()) {
throw new SQLException("0 rows returned");
}
} catch (SQLException se) {
closePhysically(pooledConnection, "Closing non-validating pooledConnection.");
throw new SQLException("Validation query failed: " + se.getMessage());
} finally {
if (rs != null) {
rs.close();
}
}
}
this.connectionsInUse.add(pooledConnection);
SessionConnectionWrapper sessionWrapper = new SessionConnectionWrapper(pooledConnection.getConnection());
this.sessionConnectionWrappers.put(pooledConnection, sessionWrapper);
return sessionWrapper;
}
use of javax.sql.PooledConnection in project voltdb by VoltDB.
the class ManagedPoolDataSource method closeImmediatedly.
/**
* Closes this connection
*/
public synchronized void closeImmediatedly() {
close();
Iterator iterator = this.connectionsInUse.iterator();
while (iterator.hasNext()) {
PooledConnection connection = (PooledConnection) iterator.next();
SessionConnectionWrapper sessionWrapper = (SessionConnectionWrapper) this.sessionConnectionWrappers.get(connection);
closeSessionWrapper(sessionWrapper, "Error closing session wrapper. Connection pool was shutdown immediatedly.");
}
}
Aggregations