billc.cn
billc.cn

Reputation: 7317

Does Spring automatically close DataSource and is closing needed?

I am setting up a connection pool as a bean in Spring like millions of applications.

Due to transaction management, when the app shuts down, all connections would be returned to the pool. The pool would then be dereferenced and/or the JVM would then shutdown.

So, is it still necessary to close the connection pool? Does Spring automatically do so for DataSources?

Upvotes: 3

Views: 7612

Answers (1)

Srikanth Balaji
Srikanth Balaji

Reputation: 2718

You generally do not close a DataSource - you just close the connection returned by a DataSource. The DataSource itself is never "open" as such.

One easiest way the connection pool closing taken care by itself is - when the app shutsdown, the context unloads, by defining destroy-method = "close" in the connection pool bean, It shutsdown the connections.

Example

<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

Upvotes: 1

Related Questions