My old application following chain to query my database: Spring Tx -> Hibernate -> C3P0
. Now I need to implement new features based on the existing architecture.
I normally enter a transactional context by using either the @Transactional
annotation or manually invoking the PlatformTransactionManager
.
Sometimes, to perform asynchronous and big data operations, I open a stateless session using the SessionFactory
API. We never had any additional problem as our thread pool is well controlled
For the first time, my requirement is to perform several DB operations in parallel to speed up performance. I have a doubt about it, because I am extremely careful about multithreaded operations.
For each entity in the database, I can perform a reconciliation operation on a separate thread. But each reconciliation operation uses a pair of connections for each of the two threads it spawns. So there are basically 4 connections for each thread.
Multithreading class teaches students that in order to prevent a deadlock (eating philosophers issue), resources shall be acquired in a transactional way: once you have acquired a fork, if you cannot acquire the second in a reasonable time, release the first and try again.
My question is simple. Given the SessionFactory
API, how can I write code that won't wait indefinitely for 4 connections from c3p0 if the pool is full? I mean I need 4 StatelessSession
s only if there is room for 4, otherwise I can wait and retry.
As far as I know, the SessionFactory
API is blocking and does not allow to set a watchdog