在以下代码中(根据 tomcat-jdbc 站点中给出的示例代码进行更改),我首先使用正确的用户名和密码创建连接池。一段时间后,我将用户名和密码更改为错误的,但仍然连接成功。我已经设置了testOnBorrow
在true
发出连接时应该测试连接的位置。有人可以解释为什么代码在发出连接时不检查密码吗?
我的代码
PoolProperties p = new PoolProperties();
p.setUrl("myurl");
p.setDriverClassName("oracle.jdbc.driver.OracleDriver");
p.setUsername("test");
p.setPassword("bsc");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(true);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(1000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" +
"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
datasource.getPoolProperties().setUsername("Correct");
datasource.getPoolProperties().setPassword("Correct");
for (int i = 0; i < 1000; i++) {
if (i == 6) {
System.out.println("Updating wrong user.");
datasource.getPoolProperties().setUsername("Wrong");
datasource.getPoolProperties().setPassword("Wrong");
}
Connection con = null;
try {
System.out.println("Creating connection - " + i);
con = datasource.getConnection();
System.out.println("Done. connection status is open " + con.isClosed());
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(select);
int cnt = 1;
while (rs.next()) {
System.out.println((cnt++) + ". Host:" + rs.getString(1));
}
rs.close();
st.close();
System.out.println("Done - \n");
} finally {
if (con != null) try {
con.close();
} catch (Exception ignore) {
}
}