Announcement Announcement Module
No announcement yet.
Transaction commited too soon! Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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

    <bean id="transactionManager"
        <property name="sessionFactory" ref="sessionFactory" />
    <bean id="transactionTemplate"
        <property name="transactionManager" ref="transactionManager" />
    <bean id="autoProxyCreator"
        <property name="interceptorNames">
        <property name="proxyTargetClass">
        <property name="beanNames">
    <bean id="transactionAttributes"
        <property name="properties">
                buscar*=PROPAGATION_REQUIRED, readOnly
                localizar*=PROPAGATION_REQUIRED, readOnly
    <bean id="transactionInterceptor"
        <property name="transactionManager">
            <ref bean="transactionManager" />
        <property name="transactionAttributeSource">
            <ref bean="transactionAttributes" />
    <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
        <property name="sessionFactory" ref="sessionFactory" />

  • #2
    Check this out..


    • #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?

      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.hi[email protected]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 [[email protected]] for Hibernate transaction
      01:54:01 DEBUG [HibernateTransactionManager:doBegin:462] - Preparing JDBC Connection of Hibernate Session [[email protected]]
      01:54:01 DEBUG [HibernateTransactionManager:doBegin:534] - Exposing Hibernate transaction as JDBC transaction [[email protected]]
      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 [[email protected]]
      02:01:55 DEBUG [AbstractPlatformTransactionManager:triggerAfterCompletion:693] - Triggering afterCompletion synchronization
      02:01:55 DEBUG [HibernateTransactionManager:doCleanupAfterCompletion:659] - Closing Hibernate Session [[email protected]] 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


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

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


        • #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?


          • #6
            You mentioned the following:
   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?