3

当我在数据源上设置登录超时时,它有什么作用?它会在后台调用 DriverManager.setLoginTimeout() 吗?问这个是因为我很好奇是否真的可以设置每个数据源的登录超时。假设我有不同的 DBMS 想要连接,并且这些 DB 具有不同的响应能力,那么期望不同的连接超时容限是合理的,对吧?

如果答案是肯定的,那么 DriverManager.setLoginTimeot() 方法是否有充分的理由是静态的?

4

1 回答 1

2

This property should be per DataSource (although as far as I can see there is no explicit mention of this in the JDBC spec). The DataSource implementation of some drivers don't even use DriverManager when creating connections.

Be aware though that the value set on the DriverManager will need to be explicitly used by the driver, as DriverManager itself does nothing with the value (except store it). For example the PostgreSQL driver only uses the DriverManager value if no explicit timeout was configured in the JDBC url, Properties object or DataSource properties.

The reason for DriverManager.setLoginTimeout() is that you do not control which Driver is actually being used by the DriverManager(*) so the only way to control it is through the DriverManager.

(*) except of course by the drivers you added to the classpath (and in Java 5 or earlier: which you explicitly loaded).

于 2012-05-30T13:58:05.390 回答