简体   繁体   中英

DBCP Connection Pool loginTimeout

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.

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.

@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");

PS:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM