Announcement Announcement Module
Collapse
No announcement yet.
lazy Connection priming with HibernateTransactionManager possible? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • lazy Connection priming with HibernateTransactionManager possible?

    Hi,

    I am looking for suggestions on how best to configure/extend HibernateTransactionManager to access a java.sql.Connection only when/if it is actually about to be used. Let me provide a use case first:

    We have service methods annotated with @Transactional. We also use second-level cache (EhCache) and get into states when some service method invocations result in 100% cache hit rate (alright!) and do not go to DB at all. However, even in those cases a DB Connection is still checked out from the (c3p0) connection pool. Since cxn check-out/in methods in c3p0 are synchronized (and rightly so), these methods become hot spots under higher loads, causing a lot of thread blocking. So, I am looking for a way to ensure DB connection is NOT checked out until it is actually needed (ie some JDBC traffic is about to be sent).

    As far as I can tell, connection is eagerly checked out in HibernateTransactionManager.doBegin() - regardless of whether it is subsequently used during given TX. (doBegin() calls deprecated session.connection())

    What do you think is the best way to address the above problem?

    Addl considerations: I need to pass readOnly flag value and TX iso level to the Connection, so seems that prepareConnection flag should remain enabled. Given that I am using a c3p0 cxn pool, seems like hibernate.connection.release_mode should be "after_transaction" - as I'd want the pool to manage a connection's lifecycle.

  • #2
    Answering my own question:

    proxy your dataSource bean using org.springframework.jdbc.datasource.LazyConnection DataSourceProxy

    very nice!

    Comment

    Working...
    X