use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.
the class TestAbondon method test_0.
public void test_0() throws Exception {
DruidPooledConnection conn = dataSource.getConnection();
Assert.assertEquals(false, conn.isClosed());
Thread.sleep(10);
for (int i = 0; i < 100; ++i) {
if (conn.isAbandonded()) {
break;
}
Thread.sleep(10);
}
Assert.assertEquals(true, conn.isAbandonded());
}
use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.
the class PGValidConnectionChecker method isValidConnection.
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
if (validateQuery == null || validateQuery.isEmpty()) {
validateQuery = this.defaultValidateQuery;
}
if (conn.isClosed()) {
return false;
}
if (conn instanceof DruidPooledConnection) {
conn = ((DruidPooledConnection) conn).getConnection();
}
if (conn instanceof ConnectionProxy) {
conn = ((ConnectionProxy) conn).getRawObject();
}
int queryTimeout = validationQueryTimeout <= 0 ? defaultQueryTimeout : validationQueryTimeout;
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
if (queryTimeout >= 0) {
// pgsql Driver 9.0以及以下版本不支持setQueryTimeout,可通过设置queryTimeout<0兼容低版本
stmt.setQueryTimeout(queryTimeout);
}
rs = stmt.executeQuery(validateQuery);
return true;
} finally {
JdbcUtils.close(rs);
JdbcUtils.close(stmt);
}
}
use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.
the class EncodingConvertFilterTest method test_stat.
public void test_stat() throws Exception {
Assert.assertTrue(dataSource.isInited());
EncodingConvertFilter filter = (EncodingConvertFilter) dataSource.getProxyFilters().get(0);
DruidPooledConnection conn = dataSource.getConnection();
final String PARAM_VALUE = "中国";
PreparedStatement stmt = conn.prepareStatement("select ?");
stmt.setString(1, PARAM_VALUE);
MockPreparedStatement raw = stmt.unwrap(MockPreparedStatement.class);
String param1 = (String) raw.getParameters().get(0);
Assert.assertEquals(PARAM_VALUE, new String(param1.getBytes(SERVER_ENCODING), CLIENT_ENCODING));
Assert.assertFalse(param1.equals(PARAM_VALUE));
ResultSet rs = stmt.executeQuery();
MyResultSet rawRs = rs.unwrap(MyResultSet.class);
rawRs.setValue(filter.encode((ConnectionProxy) conn.getConnection(), text));
rs.next();
Assert.assertEquals(text, rs.getString(1));
rs.close();
stmt.close();
conn.close();
}
use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.
the class DruidDataSourceTest9_phyMaxUseCount method test_for_phyMaxUseCount.
public void test_for_phyMaxUseCount() throws Exception {
Connection phyConn = null;
for (int i = 0; i < 100; ++i) {
DruidPooledConnection conn = dataSource.getConnection();
if (i % 10 == 0) {
if (conn.getConnection() == phyConn) {
throw new IllegalStateException();
}
}
phyConn = conn.getConnection();
conn.close();
// System.out.println(i);
}
}
use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.
the class DruidPooledConnectionTest1 method test_handleException_1.
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());
}
Aggregations