在下面的代码中(根据tomcat站点中给出的示例代码进行更改),我首先使用正确的用户名和密码创建连接池。过了一段时间后,我将用户名和密码更改为错误的用户名和密码,但连接仍然成功。我已经将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) {
}
}发布于 2022-11-25 15:27:02
根据Oracle开发人员的说法,凭据仅用于验证连接的建立。一旦建立了连接,就不会检查流经的单个数据包的凭据是否有效。这种实现可以从两端进行争论,但Oracle不同意它们应该验证已经建立的连接。这有一些很好的理由,但同样值得商榷。因此,正如@Ironluca在注释中指出的那样,您的池打开连接将不再被验证。
如果需要使会话无效,就有可能在数据库端杀死这些连接,也就是会话。这将需要DBA访问-不记得确切的sql,但我相信它可能是v$session表。
https://stackoverflow.com/questions/63595107
复制相似问题