-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: hibernate3 + spring + jpa + jta + j2se
PostPosted: Thu Aug 10, 2006 6:53 pm 
Newbie

Joined: Thu Aug 10, 2006 6:49 pm
Posts: 3
I am trying to set up and application using spring with hibernate and jpa with an application managed entitymanager. Everything is fine so long as an exception doesn't get called onCommit. The problem appears to be that Spring closes the EntityManager before the transaction commits and when Hibernate tries to roll back the transaction, it attempts to first get its session and finds it has been closed by Spring. Hibernate then throws an IllegalStateException.

Any help will be greatly appreciated. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 11:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I am following the spec here. So Spring doesn't seems to

_________________
Emmanuel


Top
 Profile  
 
 Post subject: spec section
PostPosted: Fri Aug 11, 2006 9:34 am 
Newbie

Joined: Thu Aug 10, 2006 6:49 pm
Posts: 3
Thanks. Can you tell me where this is this in the spec?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 1:54 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Don't know by heart. Search for the keywords mark for rollback and exception, you should find something saying that when a PersistenceException is raised, the EM must mark the TX for rollback

_________________
Emmanuel


Top
 Profile  
 
 Post subject: closed EntityManagers
PostPosted: Fri Aug 11, 2006 4:53 pm 
Newbie

Joined: Thu Aug 10, 2006 6:49 pm
Posts: 3
The spec indicates that you can still call getTransaction on a closed EntityManager, then rollback can be called on the transaction. The Hibernate code for JTA transactions throws an exception because the EntityManager is closed instead of calling rollback on the transaction. Is this what should be happening?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 9:58 am 
Beginner
Beginner

Joined: Fri Aug 27, 2004 1:59 pm
Posts: 33
Emmanuel, here is a spec reference that illustrates this problem:

JPA spec shows the following example in section 5.7.1.1 - an EJB Session Bean method running within CMT:

Code:
public LineItem createLineItem(Order order, Product product, int quantity) {
  EntityManager em = emf.createEntityManager();
  LineItem li = new LineItem(order, product, quantity);
  order.getLineItems().add(li);
  em.persist(li);
  em.close();
  return li; // remains managed until JTA transaction ends
}


(Note that this is for an application-managed entity manager.)

From my understanding, and from this SPEC example, the EntityManager can be closed prior to transaction commit with the entities remaining managed until JTA transaction completion. Hence, the close call is supposed to be logical, with the underlying resource kept open and closed after transaction completion.

The concrete scenario I am seeing:
* my code uses an EM and then closes it (as in the example above)
* the transaction is then committed, which triggers hibernate validator
* hibernate validator throws an exception (as expected)
* AEMI.throwPersistenceException catches the validator exception and invokes markForRollback
* AEMI.markForRollback, as part of its normal processing, invokes getSession
* getSession fails hard because the session is closed

The comment on the close() method in the spec (p46) indicates that the EM should be able to obtain the transaction after close (which is what is logically needed to mark-as-rollback).

So, it seems that even though the EM is closed this flow should work. No? Are there some limitations with application managed EMs with hibernate? Should I file a jira issue with a test?

Any help would be appreciated.

problem I am seeing is that in AbstractEntityManagerImpl.markAsRollback

_________________
-barry
---------------------
polly want-a point?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 11:13 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hi,
I've had a second look.
The spec sample you show is actually working in Hibernate because a JTA transaction has been started and the Persistence provider is enlisted in it.

In which context exactly are you using the spring transaction wrapper.
Is Spring either delegates to JTA or use em.getTransaction()?
If JTA is used, does it actually do em.joinTransaction()? (Which is mandatory for the container)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 11:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I tried to look at the EntityManagerFactoryUtils code, but downloading springframework 2.0-rc3, I can't see any src/org/springframework/orm/jpa

I'll have to rely on you

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 1:44 pm 
Newbie

Joined: Thu Aug 17, 2006 11:26 am
Posts: 2
Melody, don't know if this'll help, but I'm using the same combination of technologies and was having trouble getting persists to work initially. I discovered that apparently, I have to use Hibernate 3.2 cr2 with Spring 2.0 rc2 to get everything working. If I use Hibernate 3.2 cr3, I get a transaction exception of some sort. Haven't tried Spring 2.0 rc3 yet.

Emmanuel, the code you're looking for is in the downloaded Spring package under "tiger/src", rather than just "src" (don't know why).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 11:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://opensource.atlassian.com/projects/hibernate/browse/EJB-215

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Spring Transactions on JPA
PostPosted: Wed Nov 05, 2008 11:42 pm 
Newbie

Joined: Wed Nov 05, 2008 11:40 pm
Posts: 1
I have an article regarding Spring Transactions on JPA
http://www.ideyatech.com/2008/09/troubl ... ns-on-jpa/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.