On Thu, Dec 17, 2009 at 6:37 AM, David Blevins <[email protected]> wrote:
> 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).

I don't understand, why would I keep the type TRANSACTION ? I have
RESOURCE_LOCAL since I want to handle everything from the bean ? From
the document you sent me states:
--------------------------
With <persistence-unit transaction-type="RESOURCE_LOCAL"> you are
responsible for EntityManager (PersistenceContext/Cache) creating and
tracking...

    * You must use the EntityManagerFactory to get an EntityManager
    * The resulting EntityManager instance is a PersistenceContext/Cache
    * An EntityManagerFactory can be injected via the @PersistenceUnit
annotation only (not @PersistenceContext)
--------------------------

Am I missing something ?
And yes, I got the warning about ignoring the  @TransactionAttribute


>
> Hope this helps.
>
> -David
>
>

Reply via email to