-->
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.  [ 1 post ] 
Author Message
 Post subject: Persistence context flush failed in transaction-scoped
PostPosted: Wed Mar 28, 2012 7:58 am 
Newbie

Joined: Sat Nov 19, 2011 8:47 am
Posts: 3
Hi,

I was practising the JPA using hibernate. Below is the EJB "CopyOfItemDAOBean" (stateless, transaction-scoped) which invokes "updateItemState" method and passes detached object with modified cost value. But database was not showing the modified value. I created bean ItemDAOBean with TransactionManagementType.BEAN and manually flushed the EntityManager and the cost value got updated. I checked the logs and for "TransactionManagementType.CONTAINER" the flushing of persistence context was not done. As per the JPA specification when the transaction gets ended the corresponding EntityManager and Persistence context will be flushed and closed. I am not sure why this was not happening in this case. Can any one help me on this ?

Code:
@Stateless(mappedName = "itemDAOBean2")
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class CopyOfItemDAOBean implements ItemDAO {

   /*
    * @PersistenceContext( type=PersistenceContextType.EXTENDED, properties =
    * @PersistenceProperty( name="org.hibernate.flushMode", value="MANUAL") )
    */

   @PersistenceContext
   private EntityManager em;

   /*
    * @Resource(name="java:comp/UserTransaction") UserTransaction utx;
    */

   @Override
   public Item addItem(Item i) {
      System.out.println("addItem method called !!!");
      em.persist(i);
      return i;
   }

   @Override
   public void updateItemState(Set<Item> iList) {
      for (Item i : iList) {
         em.merge(i);
      }
   }
}


Code:
@Stateless(mappedName="itemDAOBean")
@TransactionManagement(TransactionManagementType.BEAN)
public class ItemDAOBean implements ItemDAO {

      
   @PersistenceContext                  
   private EntityManager em;
   
   @Resource(name="java:comp/UserTransaction")
   UserTransaction utx;
   
   @Override
   public Item addItem(Item i) {
      System.out.println( "addItem method called !!!");
      try {
         utx.begin();
         em.persist(i);
         em.flush();
         utx.commit();
      } catch (NotSupportedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (SystemException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (SecurityException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IllegalStateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (RollbackException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (HeuristicMixedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (HeuristicRollbackException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      return i;
      }
      
   
   

   @Override
   public void updateItemState(Set<Item> iList)
      try {
         utx.begin();
         for(Item i : iList){
            em.merge(i);   
         }
         em.flush();
         utx.commit();
      } catch (NotSupportedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (SystemException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (SecurityException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IllegalStateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (RollbackException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (HeuristicMixedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (HeuristicRollbackException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      }
   
}



Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <persistence-unit name="em" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>myDataSource</jta-data-source>
      <properties>
         <!-- property name="hibernate.ejb.cfgfile" value="META-INF/hibernate.cfg.xml"/ -->
         <property name="hibernate.transaction.factory_class"
            value="org.hibernate.transaction.JTATransactionFactory" />
         <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
         <property name="hibernate.hbm2ddl.auto" value="create"/>
      </properties>
   </persistence-unit>
</persistence>



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

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.