mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
@@ -115,11 +115,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
return holder.getDataSource().isOracle();
|
||||
}
|
||||
|
||||
void closePoolableStatement(DruidPooledPreparedStatement stmt) throws SQLException {
|
||||
if (this.holder == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
public void closePoolableStatement(DruidPooledPreparedStatement stmt) throws SQLException {
|
||||
PreparedStatement rawStatement = stmt.getRawPreparedStatement();
|
||||
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.DruidPooledPreparedStatement;
|
||||
|
||||
public class DruidPooledConnectionTest1 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_conn() throws Exception {
|
||||
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
|
||||
|
||||
conn.close();
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.handleException(new RuntimeException());
|
||||
} catch (SQLException e) {
|
||||
error = e;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getRecycleCount());
|
||||
Assert.assertEquals(1, dataSource.getCloseCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
}
|
||||
|
||||
public void test_handleException_1() throws Exception {
|
||||
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.handleException(new RuntimeException());
|
||||
} catch (SQLException e) {
|
||||
error = e;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
conn.close();
|
||||
|
||||
Assert.assertEquals(1, dataSource.getRecycleCount());
|
||||
Assert.assertEquals(1, dataSource.getCloseCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
}
|
||||
|
||||
public void test_closePoolableStatement() throws Exception {
|
||||
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
|
||||
DruidPooledPreparedStatement stmt = (DruidPooledPreparedStatement) conn.prepareStatement("select 1");
|
||||
conn.close();
|
||||
conn.closePoolableStatement(stmt);
|
||||
}
|
||||
|
||||
public void test_dup_close() throws Exception {
|
||||
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
|
||||
|
||||
conn.close();
|
||||
conn.close();
|
||||
|
||||
Assert.assertEquals(1, dataSource.getRecycleCount());
|
||||
Assert.assertEquals(1, dataSource.getCloseCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
}
|
||||
|
||||
public void test_recycle() throws Exception {
|
||||
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
|
||||
|
||||
conn.close();
|
||||
conn.recycle();
|
||||
|
||||
Assert.assertEquals(1, dataSource.getRecycleCount());
|
||||
Assert.assertEquals(1, dataSource.getCloseCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
}
|
||||
|
||||
public void test_recycle_2() throws Exception {
|
||||
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
|
||||
|
||||
conn.recycle();
|
||||
conn.recycle();
|
||||
conn.close();
|
||||
|
||||
Assert.assertEquals(1, dataSource.getRecycleCount());
|
||||
Assert.assertEquals(1, dataSource.getCloseCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.filter.FilterAdapter;
|
||||
import com.alibaba.druid.filter.FilterChain;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.proxy.jdbc.CallableStatementProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
|
||||
|
||||
public class DruidPooledConnectionTest_prepareError extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.getProxyFilters().add(new FilterAdapter() {
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int autoGeneratedKeys)
|
||||
throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int resultSetType,
|
||||
int resultSetConcurrency) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int resultSetType,
|
||||
int resultSetConcurrency, int resultSetHoldability)
|
||||
throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int[] columnIndexes)
|
||||
throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, String[] columnNames)
|
||||
throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection,
|
||||
String sql) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int resultSetType, int resultSetConcurrency)
|
||||
throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int resultSetType,
|
||||
int resultSetConcurrency, int resultSetHoldability)
|
||||
throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_prepare_error() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareStatement("select 1");
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
|
||||
public void test_prepare_error_1() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareStatement("select 1", 0, 0);
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
|
||||
public void test_prepare_error_2() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareStatement("select 1", 0, 0, 0);
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
|
||||
public void test_prepare_error_3() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareStatement("select 1", 0);
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
|
||||
public void test_prepare_error_4() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareStatement("select 1", new int[0]);
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
|
||||
public void test_prepare_error_5() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareStatement("select 1", new String[0]);
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
|
||||
public void test_prepareCall_error_1() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareCall("select 1");
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
|
||||
public void test_prepareCall_error_2() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareCall("select 1", 0, 0);
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
|
||||
public void test_prepareCall_error_3() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getErrorCount());
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
conn.prepareCall("select 1", 0, 0, 0);
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
|
||||
Assert.assertEquals(1, dataSource.getErrorCount());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user