Announcement Announcement Module
Collapse
No announcement yet.
Problem with Hibernate sessions and JTA driven transactions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Hibernate sessions and JTA driven transactions

    I have a 3rd party library that starts a transaction via JTA directly and then invokes a class of my own on two separate occassions (but within the same overall JTA transaction). This custom class of my own is supplied by Spring and is wrapped in a Spring transaction aspect. Spring, using its own JtaTransactionManager, then participates in the surrounding JTA transaction. My custom class uses a DAO that is further wrapped in the Spring Hibernate3 aspect (which ensures that my DAO always uses the same Hibernate Session for the duration of the transaction, amongst other things).
    Could someone tell me what Spring is supposed to do with the Hibernate Session between calls to my custom class? I would think that since Spring is participating in a surrounding JTA transaction, that it would create a new Session on the first invocation but not close it when that first invocation returns so that it could then reuse that same Session for the second invocation, finally closing the Session when the 3rd party library commits the surrounding JTA transaction. However, it appears that Spring is creating two separate Sessions, once for each invocation.
    I've done some initial testing, and it looks like Spring does not create two separate Sessions if Spring's JtaTransactionManager is driving the overall transaction - but if the 3rd party is driving the overall transaction via JTA directly, then I'm out of luck?

  • #2
    AFAIK, Spring uses on Session per thread - if the invocations are on the same thread (like in your test) then Spring should use only one Session - if there are two threads (webapp) then Spring will use/create 2 sessions.
    What scenario are you using?

    Comment


    • #3
      After digging around in Spring source, I noticed that it provided special support if the JTA transaction manager was set directly on the LocalSessionFactoryBean. I hadn't done this because a while back I saw somewhere that if you are using Spring + JTA it wasn't necessary to also set the JTA transaction manager on the LocalSessionFactoryBean. Anyway, after setting the JTA transaction manager, things now seem to be working as expected.
      Are there any other side effects to this that I should be aware of?

      Comment


      • #4
        I realize that when a transaction is rolled back with hibernate and a JtaTransactionManager, there is no call to Session.clear, so it will let inconsistent persistent state. Indeed, even if the transaction is rolled back correctly in the database, the session will still contain the modification made in the current thread. Is there any way to avoid this behavior ?

        Comment


        • #5
          Have you ran into any problems? In case of a rollback the session should not be used and in most cases it is closed afterwards - that's why it is no cleared.

          Comment

          Working...
          X