Announcement Announcement Module
Collapse
No announcement yet.
Transaction commited too soon! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction commited too soon!

    I'm using Spring with Hibernate and plain JSP (no web framework). I wrapped my service objects with a TransactionInterceptor, but in the JSP, I use a (wrapped) service to obtain an object, the transaction is commited (and its Session closed) before returning from the service object, so when the JSP tries to access the object it gets a "could not initialize proxy - the owning Session was closed" error.

    Why is the transaction commited so soon? How can I make the transaction still be open when the JSP is rendering?

    Thanks in advance,
    Daniel Serodio

    Code:
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="transactionTemplate"
        class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager" />
    </bean>
    
    <bean id="autoProxyCreator"
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
                <value>hibernateInterceptor</value>
            </list>
        </property>
        <property name="proxyTargetClass">
            <value>true</value>
        </property>
        <property name="beanNames">
            <list>
                <value>repositorio*</value>
            </list>
        </property>
    </bean>
    
    <bean id="transactionAttributes"
        class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
        <property name="properties">
            <value>
                salvar*=PROPAGATION_REQUIRED
                buscar*=PROPAGATION_REQUIRED, readOnly
                localizar*=PROPAGATION_REQUIRED, readOnly
            </value>
        </property>
    </bean>
    
    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributeSource">
            <ref bean="transactionAttributes" />
        </property>
    </bean>
    
    <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

  • #2
    Check this out..
    http://www.jroller.com/page/kbaum/20040708

    Comment


    • #3
      The problem is exactly that
      When the businessObject bean is referenced, the HibernateInterceptor opens a hibernate session and passes the call onto the BusinessObjectImpl. When the BusinessObjectImpl has finished executing, the HibernateInterceptor transparently closes the session.
      I don't want it to close the session! I need to be open when the JSP renders.

      I've enabled Spring debug logging, and it appears that there are 2 Sessions being used for a single request: one is opened by the OSIV, and the other one by the TransactionManager. Shouldn't they both use the same Session?

      Code:
      01:52:41 DEBUG [OpenSessionInViewFilter:lookupSessionFactory:219] - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter
      01:52:41 DEBUG [OpenSessionInViewFilter:doFilterInternal:157] - Opening single Hibernate Session in OpenSessionInViewFilter
      01:52:52 DEBUG [SessionFactoryUtils:doGetSession:329] - Opening Hibernate Session
      01:54:01 DEBUG [AbstractPlatformTransactionManager:getTransaction:253] - Using transaction object [org.springframework.orm.hibernate3.HibernateTransactionManager$HibernateTransactionObject@1124cfc]
      01:54:01 DEBUG [AbstractPlatformTransactionManager:getTransaction:280] - Creating new transaction with name [br.pucsp.apt.turgon.repositorios.impl.RepositorioChequesImpl.localizar]
      01:54:01 DEBUG [HibernateTransactionManager:doBegin:449] - Opened new Session [org.hibernate.impl.SessionImpl@7c4246] for Hibernate transaction
      01:54:01 DEBUG [HibernateTransactionManager:doBegin:462] - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@7c4246]
      01:54:01 DEBUG [HibernateTransactionManager:doBegin:534] - Exposing Hibernate transaction as JDBC transaction [org.hsqldb.jdbc.jdbcConnection@1e329eb]
      02:01:55 DEBUG [HibernateTemplate:execute:357] - Found thread-bound Session for HibernateTemplate
      02:01:55 DEBUG [HibernateTemplate:execute:381] - Not closing pre-bound Hibernate Session after HibernateTemplate
      02:01:55 DEBUG [AbstractPlatformTransactionManager:triggerBeforeCommit:653] - Triggering beforeCommit synchronization
      02:01:55 DEBUG [AbstractPlatformTransactionManager:triggerBeforeCompletion:669] - Triggering beforeCompletion synchronization
      02:01:55 DEBUG [AbstractPlatformTransactionManager:processCommit:493] - Initiating transaction commit
      02:01:55 DEBUG [HibernateTransactionManager:doCommit:580] - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@7c4246]
      02:01:55 DEBUG [AbstractPlatformTransactionManager:triggerAfterCompletion:693] - Triggering afterCompletion synchronization
      02:01:55 DEBUG [HibernateTransactionManager:doCleanupAfterCompletion:659] - Closing Hibernate Session [org.hibernate.impl.SessionImpl@7c4246] after transaction
      02:01:57 DEBUG [SessionFactoryUtils:doClose:785] - Closing Hibernate Session
      02:01:57 ERROR [LazyInitializationException:<init>:19] - could not initialize proxy - the owning Session was closed
      02:01:57 DEBUG [OpenSessionInViewFilter:doFilterInternal:182] - Closing single Hibernate Session in OpenSessionInViewFilter

      Comment


      • #4
        Then I think you need to use OpenSessionInViewFilter..

        did u read the "Being Lazy in the Web Layer" section in that article?

        Comment


        • #5
          I already have the OpenSessionInViewFilter in place. The problem is that it appears there are 2 Sessions being used for a single request: one is opened by the OpenSessionInViewFilter, and the other one by the TransactionManager. Shouldn't they both use the same Session?

          Comment


          • #6
            You mentioned the following:
            ...in the JSP, I use a (wrapped) service to obtain an object...
            Can you paste an excerpt from the code where you do the object lookup?

            Comment

            Working...
            X