0

I am using a connection pool and the Spring TransactionTemplate. If you want to shutdown the connection pool first all connection have to be returned to the pool, this means connection.close() has to be called. I have one thread using the TransactionTemplate for some queries and another thread that wants to call some shutdown method on the connection pool, but before doing this it first has to tell the TransactionTemplate to close all connection (actually only returning them to the pool).

How can this be done in Spring to immediately call close on the used connection?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149

1 Answers1

0

If you are using Hibernate along with Spring please use:

hibernate.connection.release_mode=after_transaction

If you want to release connection just after transaction.

hibernate.connection.release_mode=after_statement

If you want to release connection after each statement

This two settings are the only ways I know that will make used connection being released faster than the default behavior. At least as far as Hibernate is concerned. If you are using other library please describe which.

goroncy
  • 2,053
  • 1
  • 19
  • 16
  • I am working directly with the TransactionTemplate, no other frameworks. Mycode -> Spring transactions -> JDBC -> Database Does hibernate translate these settings into some call on the Spring framework? – Franz Kafka Sep 15 '11 at 18:14
  • I guess you are using DataSourceTransactionManager. It works like this. If you call commit on your TransactionTemplate object it delegates commit to its transactionManager implementation. Whatever happens DataSourceTransactionManager implementation will cleanup the transaction at the very end. One think that cleanup does is connection release. If you will enter "debug" (org.springframework.jdbc package) level on your logging you will see that your connection is being released jast after successful commit. Connection will not be closed if your datasource is implementing SmartDataSource. – goroncy Sep 16 '11 at 08:55
  • Yeah thanks, I know that. Maybe my question was not clear: I want to close the connection even if transactions are still running. Option 1 would be to use "kill" in linux, or System.exit(0) in java, but then the database has tonnes of pending connections created from the conntion pool. Before exiting the program, all connections shall be forced closed. I can't wait for transactions to end. I want some radical close call. – Franz Kafka Sep 16 '11 at 09:18
  • And if someone pulls a plug from your machine you would like to have your connections to be closed also? I think that what you want will always be insufficient. Implementations of connections pools normally enables you to destroy connection pool. But it all works with the assumption that nothing will happen to power in your computer ;). Another thing is to just run a database job which releases not valid connections. So if you want to be radical let database admin do his job and release "really" broken connections for you. – goroncy Sep 16 '11 at 10:17