Search in sources :

Example 1 with DruidPooledConnection

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

the class StatFilter method dataSource_getConnection.

@Override
public DruidPooledConnection dataSource_getConnection(FilterChain chain, DruidDataSource dataSource, long maxWaitMillis) throws SQLException {
    DruidPooledConnection conn = chain.dataSource_connect(dataSource, maxWaitMillis);
    if (conn != null) {
        conn.setConnectedTimeNano();
        StatFilterContext.getInstance().pool_connection_open();
    }
    return conn;
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection)

Example 2 with DruidPooledConnection

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

the class PoolableCallableStatementTest method setUp.

protected void setUp() throws Exception {
    MockConnection mockConn = new MockConnection();
    DruidConnectionHolder connHolder = new DruidConnectionHolder(dataSource, mockConn, 0);
    conn = new DruidPooledConnection(connHolder);
    raw = new MockCallableStatement(null, null);
    stmt = new DruidPooledCallableStatement(conn, new PreparedStatementHolder(new PreparedStatementKey("", null, null, 0, 0, 0), raw)) {

        protected SQLException checkException(Throwable error) throws SQLException {
            if (error instanceof SQLException) {
                return (SQLException) error;
            }
            return new SQLException(error);
        }
    };
    Assert.assertEquals(0, raw.getOutParameters().size());
    stmt.registerOutParameter(1, Types.INTEGER);
    Assert.assertEquals(1, raw.getOutParameters().size());
    stmt.registerOutParameter(2, Types.DECIMAL, 10);
    Assert.assertEquals(2, raw.getOutParameters().size());
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) PreparedStatementHolder(com.alibaba.druid.pool.PreparedStatementHolder) DruidPooledCallableStatement(com.alibaba.druid.pool.DruidPooledCallableStatement) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidConnectionHolder(com.alibaba.druid.pool.DruidConnectionHolder) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 3 with DruidPooledConnection

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

the class OracleExceptionSorterTest_closeConn_1 method test_connect.

public void test_connect() throws Exception {
    String sql = "SELECT 1";
    {
        DruidPooledConnection conn = dataSource.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.execute();
        pstmt.close();
        conn.close();
        Assert.assertEquals(0, dataSource.getActiveCount());
        Assert.assertEquals(1, dataSource.getPoolingCount());
        Assert.assertEquals(1, dataSource.getCreateCount());
    }
    DruidPooledConnection conn = dataSource.getConnection();
    MockConnection mockConn = conn.unwrap(MockConnection.class);
    Assert.assertNotNull(mockConn);
    conn.prepareStatement(sql);
    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);
    conn.close();
    {
        Connection conn2 = dataSource.getConnection();
        conn2.close();
    }
    Assert.assertEquals(0, dataSource.getActiveCount());
    Assert.assertEquals(1, dataSource.getPoolingCount());
    Assert.assertEquals(2, dataSource.getCreateCount());
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) PreparedStatement(java.sql.PreparedStatement) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 4 with DruidPooledConnection

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

the class OracleExceptionSorterTest_closeConn_3 method test_connect.

public void test_connect() throws Exception {
    String sql = "SELECT 1";
    {
        DruidPooledConnection conn = dataSource.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.execute();
        pstmt.close();
        conn.close();
        Assert.assertEquals(0, dataSource.getActiveCount());
        Assert.assertEquals(1, dataSource.getPoolingCount());
        Assert.assertEquals(1, dataSource.getCreateCount());
    }
    DruidPooledConnection conn = dataSource.getConnection();
    MockConnection mockConn = conn.unwrap(MockConnection.class);
    Assert.assertNotNull(mockConn);
    conn.setAutoCommit(false);
    conn.setReadOnly(false);
    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);
    conn.close();
    {
        Connection conn2 = dataSource.getConnection();
        conn2.close();
    }
    Assert.assertEquals(0, dataSource.getActiveCount());
    Assert.assertEquals(1, dataSource.getPoolingCount());
    Assert.assertEquals(2, dataSource.getCreateCount());
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) PreparedStatement(java.sql.PreparedStatement) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 5 with DruidPooledConnection

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

the class OracleExceptionSorterTest_commit method test_connect.

public void test_connect() throws Exception {
    String sql = "SELECT 1";
    {
        DruidPooledConnection conn = dataSource.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.execute();
        pstmt.close();
        conn.close();
        Assert.assertEquals(0, dataSource.getActiveCount());
        Assert.assertEquals(1, dataSource.getPoolingCount());
        Assert.assertEquals(1, dataSource.getCreateCount());
    }
    DruidPooledConnection conn = dataSource.getConnection();
    MockConnection mockConn = conn.unwrap(MockConnection.class);
    Assert.assertNotNull(mockConn);
    conn.setAutoCommit(false);
    conn.prepareStatement(sql);
    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);
    Exception commitError = null;
    try {
        conn.commit();
    } catch (Exception ex) {
        commitError = ex;
    }
    Assert.assertNotNull(commitError);
    conn.close();
    {
        Connection conn2 = dataSource.getConnection();
        conn2.close();
    }
    Assert.assertEquals(0, dataSource.getActiveCount());
    Assert.assertEquals(1, dataSource.getPoolingCount());
    Assert.assertEquals(2, dataSource.getCreateCount());
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) PreparedStatement(java.sql.PreparedStatement) MockConnection(com.alibaba.druid.mock.MockConnection) SQLException(java.sql.SQLException)

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