Search in sources :

Example 51 with MockConnection

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

the class OracleExceptionSorterTest_stmt_executeUpdate 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();
    }
    DruidPooledConnection conn = dataSource.getConnection();
    MockConnection mockConn = conn.unwrap(MockConnection.class);
    Assert.assertNotNull(mockConn);
    Statement stmt = conn.createStatement();
    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);
    SQLException execErrror = null;
    try {
        stmt.executeUpdate(sql);
    } catch (SQLException ex) {
        execErrror = ex;
    }
    Assert.assertNotNull(execErrror);
    Assert.assertSame(exception, execErrror);
    SQLException commitError = null;
    try {
        conn.commit();
    } catch (SQLException ex) {
        commitError = ex;
    }
    Assert.assertNotNull(commitError);
    Assert.assertSame(exception, commitError.getCause());
    conn.close();
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 52 with MockConnection

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

the class OracleExceptionSorterTest_stmt_executeUpdate_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();
    }
    DruidPooledConnection conn = dataSource.getConnection();
    MockConnection mockConn = conn.unwrap(MockConnection.class);
    Assert.assertNotNull(mockConn);
    Statement stmt = conn.createStatement();
    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);
    SQLException execErrror = null;
    try {
        stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
    } catch (SQLException ex) {
        execErrror = ex;
    }
    Assert.assertNotNull(execErrror);
    Assert.assertSame(exception, execErrror);
    SQLException commitError = null;
    try {
        conn.commit();
    } catch (SQLException ex) {
        commitError = ex;
    }
    Assert.assertNotNull(commitError);
    Assert.assertSame(exception, commitError.getCause());
    conn.close();
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 53 with MockConnection

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

the class OracleExceptionSorterTest_concurrent method test_connect.

public void test_connect() throws Exception {
    final CountDownLatch latch_0 = new CountDownLatch(1);
    Thread errorThread = new Thread() {

        public void run() {
            try {
                Connection conn = dataSource.getConnection();
                latch_0.countDown();
                MockConnection mockConn = conn.unwrap(MockConnection.class);
                Assert.assertNotNull(mockConn);
                SQLException exception = new SQLException("xx", "xxx", 28);
                mockConn.setError(exception);
                try {
                    conn.createStatement();
                } catch (SQLException ex) {
                // ex.printStackTrace();
                }
                conn.close();
            } catch (Exception error) {
                error.printStackTrace();
            }
        }
    };
    errorThread.start();
    final CountDownLatch workLatch = new CountDownLatch(2);
    final CountDownLatch workCompleteLatch = new CountDownLatch(2);
    for (int i = 0; i < 2; ++i) {
        Thread thread = new Thread() {

            public void run() {
                try {
                    for (int i = 0; i < 1000; ++i) {
                        workLatch.countDown();
                        Connection conn = dataSource.getConnection();
                        conn.close();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    workCompleteLatch.countDown();
                }
            }
        };
        thread.start();
    }
    workLatch.await();
    latch_0.countDown();
    workCompleteLatch.await();
    Assert.assertEquals(2001, dataSource.getConnectCount());
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) CountDownLatch(java.util.concurrent.CountDownLatch) MockConnection(com.alibaba.druid.mock.MockConnection) SQLException(java.sql.SQLException)

Example 54 with MockConnection

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

the class OracleExceptionSorterTest_setIsolationLevel 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);
    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);
    Exception setError = null;
    try {
        conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    } catch (Exception ex) {
        setError = ex;
    }
    Assert.assertNotNull(setError);
    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)

Example 55 with MockConnection

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

the class OracleExceptionSorterTest_closeConn 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);
    PreparedStatement pstmt = conn.prepareStatement(sql);
    pstmt.setFetchSize(1000);
    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);
    Assert.assertEquals(1, dataSource.getActiveCount());
    conn.close();
    Assert.assertEquals(0, dataSource.getActiveCount());
    {
        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)

Aggregations

MockConnection (com.alibaba.druid.mock.MockConnection)74 SQLException (java.sql.SQLException)54 PreparedStatement (java.sql.PreparedStatement)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)6 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)4 MockStatement (com.alibaba.druid.mock.MockStatement)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 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 NewProxyConnection (com.mchange.v2.c3p0.impl.NewProxyConnection)2