DBCP Connection Pool loginTimeout

╄→尐↘猪︶ㄣ 提交于 2019-12-04 19:16:01

问题


According to the DBCP Document, BasicDataSource does not support setLoginTimeout(). My question is then how do I set a LoginTimeout for the creation of Connection objects? I know I can set maxWait on the pool, but my understanding is that that'll only be used for when the pool is exhausted and you're waiting for an existing connection to free up. It will not save me from the situation where a new connection needs to be created, but the connection/login into the DB hangs.

Any help is appreciated. Thanks.


回答1:


Well there is always an option to add correct parameter to the URL. Depending on which DB you are using you can add one of the parameters in JDBC url.

Here is the link that confirms that BasicDataSource does not support loginTimeout

And at the bottom of this blog There is a table which lists URL parameters for connection timeouts.




回答2:


@Sap is right, you might use the JDBC url in order to add connection properties, for example:

basicDataSource.setUrl("jdbc:postgresql://192.168.0.100:5432/postgres?connectTimeout=TIME_IN_SECONDS");

Where TIME_IN_SECONDS can be any intenger value.

Besides, we can use BasicDataSource#setConnectionProperties. As the parameter says:

The connection properties that will be sent to our JDBC driver when establishing new connections. Format of the string must be [propertyName=property;]* NOTE - The "user" and "password" properties will be passed explicitly, so they do not need to be included here.

So, it might be something like (and following the previous example):

basicDataSource.setUrl("jdbc:postgresql://192.168.0.100:5432/postgres");
basicDataSource.setConnectionProperties("connectTimeout=TIME_IN_SECONDS");

P.S:

You can add more properties using ; at the end of the property.



来源:https://stackoverflow.com/questions/11306896/dbcp-connection-pool-logintimeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!