On Dec 17, 2009, at 3:52 AM, Mansour Al Akeel wrote:

@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionManagement(TransactionManagementType.BEAN)
@Stateful
public class RestaurantDao3 extends BaseDao<Restaurant, Long> implements
        IRestaurantDao {

If you're using OpenEJB 3.1.x, you should be getting a warning saying the @TransactionAttribute annotation will be ignored as you are not using Container-Managed Transactions -- @TransactionManagement(TransactionManagementType.CONTAINER)

   @Override
   public Restaurant create(Restaurant obj)
   {
        try
            {
                Context ctx = new InitialContext();
                UserTransaction ut = (UserTransaction) ctx
                        .lookup("java:comp/UserTransaction");
                ut.begin();
                obj = getEntityManager().merge(obj);
                getEntityManager().persist(obj);
                getEntityManager().flush();
                ut.commit();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        return obj;
   }
....
}

I am getting the same exception, if I remove the line
"entityManager.flush()" then it works fine but nothing is saved to the
the DB.
The expection I get is:
<openjpa-1.2.1-r752877:753278 nonfatal user error>
org.apache.openjpa.persistence.TransactionRequiredException: Can only
perform operation while a transaction is active.
at org .apache .openjpa.kernel.BrokerImpl.assertActiveTransaction(BrokerImpl.java: 4380) at org .apache .openjpa .kernel .DelegatingBroker.assertActiveTransaction(DelegatingBroker.java:1330) at org .apache .openjpa.persistence.EntityManagerImpl.flush(EntityManagerImpl.java: 591) at org .apache .openejb.persistence.JtaEntityManager.flush(JtaEntityManager.java:130)

This should be fine depending on how you get your entity manager reference and how your persistence unit is declared. Your persistence unit should be of type TRANSACTION and you should be using @PersistenceContext to get a reference to the EntityManager (not @PersistenceUnit or an EntityManagerFactory).

Hope this helps.

-David

Reply via email to