Search in sources :

Example 81 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class OracleValidConnectionChecker method isValidConnection.

public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
    if (validateQuery == null || validateQuery.isEmpty()) {
        validateQuery = this.defaultValidateQuery;
    }
    if (conn.isClosed()) {
        return false;
    }
    if (conn instanceof DruidPooledConnection) {
        conn = ((DruidPooledConnection) conn).getConnection();
    }
    if (conn instanceof ConnectionProxy) {
        conn = ((ConnectionProxy) conn).getRawObject();
    }
    if (validateQuery == null || validateQuery.isEmpty()) {
        return true;
    }
    int queryTimeout = validationQueryTimeout <= 0 ? timeout : validationQueryTimeout;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.createStatement();
        stmt.setQueryTimeout(queryTimeout);
        rs = stmt.executeQuery(validateQuery);
        return true;
    } finally {
        JdbcUtils.close(rs);
        JdbcUtils.close(stmt);
    }
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 82 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class DruidXADataSource method getXAConnection.

@Override
public XAConnection getXAConnection() throws SQLException {
    DruidPooledConnection conn = this.getConnection();
    Connection physicalConn = conn.unwrap(Connection.class);
    XAConnection rawXAConnection = createPhysicalXAConnection(physicalConn);
    return new DruidPooledXAConnection(conn, rawXAConnection);
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) XAConnection(javax.sql.XAConnection)

Example 83 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class DruidConnectionHolderTest4 method test_toString.

public void test_toString() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection();
    DruidConnectionHolder holder = conn.getConnectionHolder();
    Field field = DruidConnectionHolder.class.getDeclaredField("statementPool");
    field.setAccessible(true);
    Assert.assertNull(field.get(holder));
    holder.toString();
    Assert.assertNull(field.get(holder));
    holder.getStatementPool();
    Assert.assertNotNull(field.get(holder));
    holder.toString();
    PreparedStatement stmt = conn.prepareStatement("select 1");
    stmt.execute();
    stmt.close();
    conn.close();
    Assert.assertEquals(1, holder.getStatementPool().size());
    holder.toString();
}
Also used : Field(java.lang.reflect.Field) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) PreparedStatement(java.sql.PreparedStatement) DruidConnectionHolder(com.alibaba.druid.pool.DruidConnectionHolder)

Example 84 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class DruidDataSourceTest_getPooledConnection method test_event_error.

public void test_event_error() throws Exception {
    DruidPooledConnection conn = (DruidPooledConnection) dataSource.getPooledConnection();
    final AtomicInteger errorCount = new AtomicInteger();
    conn.addConnectionEventListener(new ConnectionEventListener() {

        @Override
        public void connectionErrorOccurred(ConnectionEvent event) {
            errorCount.incrementAndGet();
        }

        @Override
        public void connectionClosed(ConnectionEvent event) {
        }
    });
    PreparedStatement stmt = conn.prepareStatement("select ?");
    try {
        stmt.executeQuery();
    } catch (SQLException e) {
    }
    Assert.assertEquals(1, errorCount.get());
    conn.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) ConnectionEvent(javax.sql.ConnectionEvent) PreparedStatement(java.sql.PreparedStatement) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 85 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class DruidDataSourceTest_recycle2 method test_recycle.

public void test_recycle() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute("select 1");
    Assert.assertEquals(0, dataSource.getPoolingCount());
    Assert.assertEquals(1, dataSource.getActiveCount());
    conn.close();
    Assert.assertEquals(1, dataSource.getPoolingCount());
    Assert.assertEquals(0, dataSource.getActiveCount());
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Statement(java.sql.Statement)

Aggregations

DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)152 SQLException (java.sql.SQLException)99 PreparedStatement (java.sql.PreparedStatement)61 MockConnection (com.alibaba.druid.mock.MockConnection)49 Statement (java.sql.Statement)46 Connection (java.sql.Connection)20 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)10 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)7 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)5 ResultSet (java.sql.ResultSet)5 DruidPooledPreparedStatement (com.alibaba.druid.pool.DruidPooledPreparedStatement)4 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)4 Properties (java.util.Properties)4 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 EncodingConvertFilter (com.alibaba.druid.filter.encoding.EncodingConvertFilter)2 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockResultSet (com.alibaba.druid.mock.MockResultSet)2 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)2 PreparedStatementPool (com.alibaba.druid.pool.PreparedStatementPool)2 CountDownLatch (java.util.concurrent.CountDownLatch)2