use of com.alibaba.druid.mock.MockConnection in project druid by alibaba.
the class OracleExceptionSorterTest_stmt_getUpdateCount 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();
stmt.execute(sql);
SQLException exception = new SQLException("xx", "xxx", 28);
mockConn.setError(exception);
SQLException stmtErrror = null;
try {
stmt.getUpdateCount();
} catch (SQLException ex) {
stmtErrror = ex;
}
Assert.assertNotNull(stmtErrror);
Assert.assertSame(exception, stmtErrror);
SQLException commitError = null;
try {
conn.commit();
} catch (SQLException ex) {
commitError = ex;
}
Assert.assertNotNull(commitError);
Assert.assertSame(exception, commitError.getCause());
conn.close();
}
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);
}
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);
}
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();
}
Aggregations