Results 1 to 5 of 5

Thread: OpenEntityManagerInViewFilter works wrong iwth JTA transaction

  1. #1
    Join Date
    Dec 2007
    Posts
    130

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2007
    Posts
    130

    Default

    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?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Dec 2007
    Posts
    130

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •