Announcement Announcement Module
No announcement yet.
Getting reference to connection within txn from non-Spring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting reference to connection within txn from non-Spring

    Our new development is using Spring and Hibernate for transactions and persistence. I have some previously written components that have a home grown persistence framework. I need to be able to run these components within the new code and yet run them within the same txn as all other classes running. I am adapting the home grown framework to provide "hooks" into the Spring/Hibernate transactions and connections.

    What I am trying to do is get a reference to the current transaction and the existing connection with in that. That will be used in the existing framework to DAO's for connectivity within the transaction. I have a class that I have declared as transactional and pass a refrence to the session factory to this. I then use the SesssionFactoryUtils to get the data source and the connection from that. But what appears to be happening is that a new connection is retrieved from the datasource and then not returned to the pool upon completion of the txn. I know this because I exhuast the pool in my unit test using DBCP. I've also stepped through the DCBP GenericObjectPool and have seen that objects are "borrowed" from it twice, but only returned once. I'm not so concerned with DBCP as much as how to get a reference to the one single connection that the transaction is managing.

    Is there something I am missing as far as getting a reference to the currect connection associated to the executing transaction?

    Also, I've got pretty much the standard configuration, data source (DBCP), Session Factory (LocalSessionFactoryBean), transaction manager (HibernateTransactionManager), txn proxy (TransactionProxyFactoryBean), and a class that declares itself transactional using the parent="txnProxy

  • #2
    Do you call DataSourceUtils.closeConnectionIfNecessary(con, dataSource) in your code when you are done with the JDBC processing?


    • #3
      Almost, but not quite


      Good point. I am not closing the connection. But, that really is not the root cause of my issue. You see, I am somehow getting a different connection other then the one associated with the Spring session that is controlling the executing transaction. That connection is obtained and closed correctly. While the connection I have in my via is a different connection and therefore never closed. I do not close the connection becuase it is assumed that the surrounding txn is in control. Which it should be, and in some respects is.

      I am getting a reference to a bean that extends HibernateDaoSupprort. And using that session to obtain a connection. I was assuming and hoping, that the connection returned via
      would be the one that is controlled via the Spring.


      • #4

        Ok, I figured it out now. After thinking over this and looking more at why I was getting a new connection instead of the existing one, I determined that I was calling the wrong sequence of methods to get the connection.

        It was as simple as using
        Funny, how you sometimes get so focused on something and are unable to look at it in a different light. Until someone asks a simple question that makes you go... hmmmmm