-->
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.  [ 5 posts ] 
Author Message
 Post subject: SFSB PersistenceContextType.EXTENDED Exception handling
PostPosted: Thu Oct 13, 2011 3:34 pm 
Newbie

Joined: Thu Oct 13, 2011 2:09 pm
Posts: 3
Hi,

how can I recover from PersistenceException in StatefulSessionBean.
I'm using EntityManager with PersistenceContextType.EXTENDED.
It seems that after a PersistenceException I cannot use the EntityManager any longer.
But how can the user fix wrong data input?

In my sample code I simulate the following scenario:
1. User creates new entity and enters wrong data.
2. User stores that entity.
2.1. I catch the ConstraintViolationException and throw an ApplicationException in the EJB. It results in transaction rollback.
3. User gets error-message and fixes data input
4. User stores fixed entity.
4.1 It does not work. The entityManager cannot be used any longer?
5. I have no idea how to solve my problem :-(

The ejb-client code:
Code:
public class TestClient {

   @Inject TestSFSB mySFSB;
   
   public void testIt() {
      
      TestEntity myEntity = new TestEntity();
      myEntity.setKey("key-that-exists-in-db"); // simulates user input that will violate a unique-constraint
      try {         
         mySFSB.store(myEntity);
      } catch (ApplicationException e) {
         try {
            System.out.println("Hey user we have a constraint violation, please fix your data!");
            myEntity.setKey("new key that does not exist in db"); //simulates fixed user input that would not violate
            mySFSB.store(myEntity);
         } catch (Exception e1) {
            // with the fixed data it ends here with
            // javax.ejb.EJBException: javax.persistence.PersistenceException:
            // org.hibernate.HibernateException: proxy handle is no longer valid
            e1.printStackTrace();
         }
      }
   }
}


The ejb:
Code:
@Stateful
public class TestSFSB {
   
   @PersistenceContext(type = PersistenceContextType.EXTENDED)
   EntityManager em;

    public TestEntity findMasterByPk(Long id) {
       TestEntity myEntity = em.find(TestEntity.class, id);
       return myEntity;
    }

    public void store(TestEntity myEntity) throws ApplicationException {
       
      if (em.contains(myEntity)) {
         em.persist(myEntity);
      } else if (myEntity.getId() != null && myEntity.getId() > 0L) {
         em.merge(myEntity);
      } else {
         em.persist(myEntity);
      }

       try {
         em.flush();
       } catch (PersistenceException e) {          
          Throwable cause1 = e.getCause();
          if (cause1 instanceof ConstraintViolationException) {
             // first time calling the method it comes here
             ConstraintViolationException cve = (ConstraintViolationException) cause1;
             throw new ApplicationException(cve.getErrorCode(), e);
          } else {
             // second time with fixed data it ends up here with
             // javax.persistence.PersistenceException: org.hibernate.HibernateException: proxy handle is no longer valid?
             throw e;
          }
       }
    }   
}


Thanks for your help
Günther


Top
 Profile  
 
 Post subject: Re: SFSB PersistenceContextType.EXTENDED Exception handling
PostPosted: Mon Oct 17, 2011 8:24 am 
Newbie

Joined: Wed Oct 12, 2011 10:11 am
Posts: 7
Hi,

After getting exception.when you try access entitymaneger what error you are getting.


Top
 Profile  
 
 Post subject: Re: SFSB PersistenceContextType.EXTENDED Exception handling
PostPosted: Mon Oct 17, 2011 8:34 am 
Newbie

Joined: Thu Oct 13, 2011 2:09 pm
Posts: 3
mvs_reddy1 wrote:
Hi,

After getting exception.when you try access entitymaneger what error you are getting.


I get: javax.persistence.PersistenceException: org.hibernate.HibernateException: proxy handle is no longer valid


Top
 Profile  
 
 Post subject: Re: SFSB PersistenceContextType.EXTENDED Exception handling
PostPosted: Mon Oct 17, 2011 10:36 am 
Newbie

Joined: Wed May 11, 2005 7:16 am
Posts: 9
IMO you can't....i have a similiar issue with a seam app..read this:

http://docs.jboss.org/hibernate/core/3. ... exceptions

"If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable. Ensure that the Session will be closed by calling close() in a finally block."

I posted a message to seam user forum..maybe there is more help than here....


Top
 Profile  
 
 Post subject: Re: SFSB PersistenceContextType.EXTENDED Exception handling
PostPosted: Mon Oct 17, 2011 11:03 am 
Newbie

Joined: Thu Oct 13, 2011 2:09 pm
Posts: 3
ustone wrote:
IMO you can't....i have a similiar issue with a seam app..read this:

http://docs.jboss.org/hibernate/core/3. ... exceptions

"If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable. Ensure that the Session will be closed by calling close() in a finally block."

I posted a message to seam user forum..maybe there is more help than here....


I allready read this.
But it does not really help to solve the problem.
In my ejb client I get the stateful session bean injected.
In my stateful session bean I get the extended entityManager injected.
After a PersistenceException I cannot use the the extended entityManager in the stateful session bean any longer.
Is there a way to repair that extended entityManager or to tell CDI to inject a new one?
Or is the only way to lookup the stateful session bean from JNDI and when an exception occures transfer all the ejb state to a new ejb which has a fresh entityManager?
The last possibility would be very strange in the CDI age :-(

with hope for more help
Günther


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