Announcement Announcement Module
Collapse
No announcement yet.
OpenEntityManagerInViewFilter works wrong iwth JTA transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenEntityManagerInViewFilter works wrong iwth JTA transaction

    I am developing an application with JPA, and JTA transactions using hibernate

    Java 7
    Spring 3.2
    Hibernate 4.1.7
    Atomikos 3.8.0
    Tomcat 7.0.25

    I use the OpenEntityManagerInViewFilter (Just for one of the persistence units. I don't access the other outside transactions) for every page, so that bean properties are acessible during JSP rendering.

    I have to persistence units defined in persistence.xml. If i define transaction-type as "RESOURCE_LOCAL", every thing works ok, but when I define transaction-type as "JTA", i get LazyInitializationException when I access not initialized properties

    So, this works ok

    Code:
    <persistence-unit name="Geiser" transaction-type="RESOURCE_LOCAL">
    ......  
    </persistence-unit>
    
    <persistence-unit name="Orve" transaction-type="RESOURCE_LOCAL">
    ......
    </persistence-unit>
    This way it throws LazyInitializationException


    Code:
    <persistence-unit name="Geiser" transaction-type="JTA">
    ......  
    </persistence-unit>
    
    <persistence-unit name="Orve" transaction-type="JTA">
    ......
    </persistence-unit>
    The cuestion is: Do I need to use transaction-type="JTA" for JTA to work properly or is enough with transaction-type="RESOURCE_LOCAL"?

    In case I need to use transaction-type="JTA", how do I solve the problem with LazyInitializationException?

  • #2
    Only setting the transaction-type to JTA isn't enough you also need to configure hibernate accordingly and next to that need to configure a JtaTransactionManager in Spring... Without those it will not work.

    Comment


    • #3
      Thanks Marten,

      I solved some issues after I wrote the question. All the stuff is configured (I think correctly). I have 2 jndi Atomikos datasources, and the JtaTransactionManager.

      Transactions seem to work correctly with transaction-type="RESOURCE_LOCAL" (I get a lot of logging from atomikos, and I saw no errors nor warnings).

      The problem when I change to transaction-type="JTA" seems to be that hibernate session is closed when I exit the @Transactional boundary instead of being closed in the OpenEntityManagerInViewFilter. As a result I get a LazyInitializationException when I try to access uninitialized properties outside the @Transactional

      I was just wondering if there is any problem using transaction-type="RESOURCE_LOCAL" with spring managed transactions or transaction-type="JTA" is mandatory?

      Comment


      • #4
        I solved some issues after I wrote the question. All the stuff is configured (I think correctly). I have 2 jndi Atomikos datasources, and the JtaTransactionManager.
        Judging from the behavior there is something wrong with your setup. Post the configuration so that we can have a look.

        Comment


        • #5
          Hi,

          I was trying to make a simple example to post, and I found the problem.

          I am using a custom framework on top of spring. Some components can't know which persistence unit to use until runtime, so I can't use @PersistenceContext. The problem was that I was reusing the EntityManager on every request instead of using a different one per request

          Thanks

          Comment

          Working...
          X