Search in sources :

Example 61 with MockConnection

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

the class PoolableWrapperTest method test_isWrapper.

public void test_isWrapper() throws Exception {
    PoolableWrapper wrapper = new PoolableWrapper(new MockConnection());
    Assert.assertEquals(false, wrapper.isWrapperFor(null));
    Assert.assertEquals(true, wrapper.isWrapperFor(PoolableWrapper.class));
    Assert.assertEquals(true, wrapper.isWrapperFor(MockConnection.class));
}
Also used : PoolableWrapper(com.alibaba.druid.pool.PoolableWrapper) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 62 with MockConnection

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

the class TestLRU method unwrap.

public static MockConnection unwrap(Connection conn) throws Exception {
    if (conn instanceof ConnectionHandle) {
        ConnectionHandle handle = (ConnectionHandle) conn;
        return (MockConnection) handle.getInternalConnection();
    }
    if (conn instanceof NewProxyConnection) {
        NewProxyConnection handle = (NewProxyConnection) conn;
        Field field = NewProxyConnection.class.getDeclaredField("inner");
        field.setAccessible(true);
        return (MockConnection) field.get(handle);
    }
    return conn.unwrap(MockConnection.class);
}
Also used : Field(java.lang.reflect.Field) NewProxyConnection(com.mchange.v2.c3p0.impl.NewProxyConnection) ConnectionHandle(com.jolbox.bonecp.ConnectionHandle) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 63 with MockConnection

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

the class TestPSCache method unwrap.

public static MockConnection unwrap(Connection conn) throws Exception {
    if (conn instanceof ConnectionHandle) {
        ConnectionHandle handle = (ConnectionHandle) conn;
        return (MockConnection) handle.getInternalConnection();
    }
    if (conn instanceof NewProxyConnection) {
        NewProxyConnection handle = (NewProxyConnection) conn;
        Field field = NewProxyConnection.class.getDeclaredField("inner");
        field.setAccessible(true);
        return (MockConnection) field.get(handle);
    }
    return conn.unwrap(MockConnection.class);
}
Also used : Field(java.lang.reflect.Field) NewProxyConnection(com.mchange.v2.c3p0.impl.NewProxyConnection) ConnectionHandle(com.jolbox.bonecp.ConnectionHandle) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 64 with MockConnection

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

the class TestIdleForKylin method test_idle.

public void test_idle() throws Exception {
    MockDriver driver = MockDriver.instance;
    // BasicDataSource dataSource = new BasicDataSource();
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
    dataSource.setInitialSize(1);
    dataSource.setMaxActive(10);
    dataSource.setMaxIdle(10);
    dataSource.setMinIdle(0);
    dataSource.setMinEvictableIdleTimeMillis(50000 * 1);
    dataSource.setTimeBetweenEvictionRunsMillis(500);
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setValidationQuery("SELECT 1");
    {
        Connection conn = dataSource.getConnection();
        // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
        System.out.println("raw size : " + driver.getConnections().size());
        PreparedStatement stmt = conn.prepareStatement("SELECT 1");
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }
    {
        Connection conn = dataSource.getConnection();
        // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
        System.out.println("raw size : " + driver.getConnections().size());
        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }
    {
        int count = 4;
        Connection[] connections = new Connection[4];
        for (int i = 0; i < count; ++i) {
            connections[i] = dataSource.getConnection();
        }
        System.out.println("raw size : " + driver.getConnections().size());
        for (int i = 0; i < count; ++i) {
            connections[i].close();
        }
        System.out.println("raw size : " + driver.getConnections().size());
        System.out.println("----------sleep for evict");
        Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
        System.out.println("raw size : " + driver.getConnections().size());
    }
    System.out.println("----------raw close all connection");
    for (MockConnection rawConn : driver.getConnections()) {
        rawConn.close();
    }
    Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
    System.out.println("raw size : " + driver.getConnections().size());
    {
        Connection conn = dataSource.getConnection();
        System.out.println("raw size : " + driver.getConnections().size());
        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }
    dataSource.close();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 65 with MockConnection

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

the class StatFilterExecuteTest method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("stat");
    dataSource.setTestOnBorrow(false);
    MockDriver driver = new MockDriver() {

        public MockPreparedStatement createMockPreparedStatement(MockConnection conn, String sql) {
            return new MyMockPreparedStatement(conn, sql);
        }
    };
    dataSource.setDriver(driver);
    dataSource.init();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) 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