Announcement Announcement Module
Collapse
No announcement yet.
Hibernate session and multiple transactions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate session and multiple transactions

    It seems that a new session is created at the beginning of each new transaction and closed at the end. Is it possible to share the same session
    if transactions are nested? Something like:

    // ISOLATION_REQUIRED
    doBusinessA()
    {
    A obj1 = loadById(3);

    serviceA.doPart1(obj1) // ISOLATAION_REQUIRES_NEW

    ....
    }

    class A {

    // ISOLATAION_REQUIRES_NEW
    doPart1(A obj)
    {
    // setting values....
    getHibernateTemplate().save(obj)
    }

    }

  • #2
    How are you managing your sessons? If you use the OpenSessionInViewFilter, the session's lifespan will be bound to the request/response cycle, not a single transaction.

    Comment


    • #3
      The session is managed by HibernateTransactionManager. I'm using source-annonations for transaction demarcation. A transaction is started
      by doBusiness().

      Originally posted by smccrory
      How are you managing your sessons? If you use the OpenSessionInViewFilter, the session's lifespan will be bound to the request/response cycle, not a single transaction.

      Comment


      • #4
        Then no, your sessions will not live outside of the doBusiness() methods and thus will not be recycled from one business call/transaction to the next. If you want this, I recommend moving session management to OpenSessionInViewFilter.

        Comment


        • #5
          I probably didn't explain my problem very well. I only want to share
          the session created by doBusiness1() inside itself. This is what I want:

          doBusiness1()
          {
          - new hibernate session (by spring)
          - new transaction (by spring)

          - look up a hibernate object A
          - doPartA(A)
          {
          - new transaction
          - use above session <= looks like Spring doesnt do this
          - make changes to A
          - save A
          - commit transaction
          }

          - commit transaction
          - close session
          }

          Is that more clear?




          Originally posted by smccrory
          Then no, your sessions will not live outside of the doBusiness() methods and thus will not be recycled from one business call/transaction to the next. If you want this, I recommend moving session management to OpenSessionInViewFilter.

          Comment

          Working...
          X