Search in sources :

Example 1 with MockConnection

use of com.alibaba.druid.mock.MockConnection 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 2 with MockConnection

use of com.alibaba.druid.mock.MockConnection in project druid by alibaba.

the class PreparedStatementKeyTest method test_contains.

public void test_contains() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    MockConnection conn = new MockConnection();
    PreparedStatementKey k1 = new PreparedStatementKey("x1", "c1", MethodType.M1);
    PreparedStatementPool pool = new PreparedStatementPool(new DruidConnectionHolder(dataSource, conn, 0));
    MockPreparedStatement raw = new MockPreparedStatement(null, null);
    pool.put(new PreparedStatementHolder(k1, raw));
    Assert.assertTrue(pool.get(k1) != null);
    Assert.assertTrue(pool.get(k1) != null);
}
Also used : PreparedStatementHolder(com.alibaba.druid.pool.PreparedStatementHolder) PreparedStatementPool(com.alibaba.druid.pool.PreparedStatementPool) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey) DruidConnectionHolder(com.alibaba.druid.pool.DruidConnectionHolder) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 3 with MockConnection

use of com.alibaba.druid.mock.MockConnection 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 MockConnection

use of com.alibaba.druid.mock.MockConnection 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 MockConnection

use of com.alibaba.druid.mock.MockConnection 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

MockConnection (com.alibaba.druid.mock.MockConnection)76 PreparedStatement (java.sql.PreparedStatement)53 SQLException (java.sql.SQLException)53 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)49 Statement (java.sql.Statement)38 Connection (java.sql.Connection)22 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)13 MockDriver (com.alibaba.druid.mock.MockDriver)8 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)7 MockStatement (com.alibaba.druid.mock.MockStatement)5 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)4 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)3 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)3 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)3 MSSQLValidConnectionChecker (com.alibaba.druid.pool.vendor.MSSQLValidConnectionChecker)3 Field (java.lang.reflect.Field)3 ResultSet (java.sql.ResultSet)3 PoolableWrapper (com.alibaba.druid.pool.PoolableWrapper)2 DruidNativeJdbcExtractor (com.alibaba.druid.support.spring.DruidNativeJdbcExtractor)2 ConnectionHandle (com.jolbox.bonecp.ConnectionHandle)2