-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to open a new hibernate transaction with ejb3?
PostPosted: Mon Jul 05, 2010 12:03 pm 
Newbie

Joined: Tue Jun 29, 2010 7:09 pm
Posts: 1
Hi everybody! I have a simple but annoying question. How can I make (or call for) a new transaction when running along all the hibernate process?

What I mean is the following. I have a web service running with Jboss and Hibernate (using ejb3). The thing is that I want to make 5 inserts, but for example if the no. 3 fails, the first two are rollbacked automatically and the last two are not even persisted. I always get a "EntityManager must be access within a transaction" exception, so I found that the problem was that I was using the same transaction along all the 5 inserts.

I have 5 methods (one per each insert), and I was using the annotation "@TransactionAttribute(TransactionAttributeType.SUPPORTS)" in everyone of them. I changed them to "@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)" on each one, but the problem persists. Does anyone know what should I change to get the non-fail inserts to not roll back? Thanks!


Top
 Profile  
 
 Post subject: Re: How to open a new hibernate transaction with ejb3?
PostPosted: Mon Jul 05, 2010 5:18 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
if you configure hibernate correctly you won't need to deal with transactions: at the end of you business method the container will commit the transaction, including all changes you made in the hibernate/JPA context.
So what you have to do is read the manual about configuring the transactionmanager: http://docs.jboss.org/hibernate/stable/core/reference/en/html/session-configuration.html#configuration-j2ee

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: How to open a new hibernate transaction with ejb3?
PostPosted: Tue Jul 06, 2010 2:36 am 
Newbie

Joined: Thu Aug 20, 2009 3:00 am
Posts: 11
Hello,

I don't want autocommitting and so I just call this piece of code as soon as the entities need to be committed. This does a rollback if anything fails.

Code:
   /**
    * @param entityManager - EntityManager
    */
   public static void commitEntityManager(EntityManager entityManager) {
      synchronized (entityManager) {
         EntityTransaction transaction = entityManager.getTransaction();
         try {
            transaction.begin();
            transaction.commit();
         } catch (Throwable e) {
            LOGGER.error("commitEntityManager: " + e, e);
            if (transaction != null && transaction.isActive()) {
               transaction.rollback();
            }
            FacesHelper.addError("Aanpassing mislukt... probeer opnieuw ! [" + e + "]");
         }
      }
   }


Hmmm, reread your post and I now see that you WANT the others to be inserted. Maybe just call the commit for each insert? Or probably better, use the advise of experienced poster Sanne :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.