Search in sources :

Example 6 with PooledConnection

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;
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

Example 7 with PooledConnection

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);
}
Also used : DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject) PooledConnection(javax.sql.PooledConnection)

Example 8 with PooledConnection

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);
}
Also used : PooledConnection(javax.sql.PooledConnection) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) Test(org.junit.Test)

Example 9 with PooledConnection

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;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection)

Example 10 with PooledConnection

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.");
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) Iterator(java.util.Iterator)

Aggregations

PooledConnection (javax.sql.PooledConnection)40 SQLException (java.sql.SQLException)21 Connection (java.sql.Connection)14 Test (org.junit.Test)7 Context (javax.naming.Context)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 ResultSet (java.sql.ResultSet)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 Statement (java.sql.Statement)2 Iterator (java.util.Iterator)2 XAConnection (javax.sql.XAConnection)2 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 InitialContext (javax.naming.InitialContext)1 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)1