Thank you for your help.
I just looked into the spring integration example, and that's what I
was using. I am trying to step a way from spring in a toy project to
gain deeper understanding for transactions.
In the spring integration example, it's not very clear to me how the
transaction is being handled, and how the entityManager is
initialized. To narrow down my problem, I think what I need is a
usertransaction in the Stateless bean. I am not using spring, and
trying to do everything using openEjb only. Here's the code I am
facing problems with:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionManagement(TransactionManagementType.BEAN)
@Stateful
public class RestaurantDao3 extends BaseDao<Restaurant, Long> implements
        IRestaurantDao {

    @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)





On Wed, Dec 16, 2009 at 3:52 AM, Jean-Louis MONTEIRO
<[email protected]> wrote:
>
> Hi Mansour,
>
> Did you have a look to our sample named "spring-integration"?
> I seems to me it's what your are trying to do?
>
> Let us know if not.
>
> Jean-Louis
>
>
>
> Mansour Al Akeel wrote:
>>
>> I do have some dao working with spring and tested them as pojos. I am
>> porting them to stateless beans. I am using User transaction (bean
>> managed transaction).
>> Following the example of  entityManager injection :
>>
>> public class RestaurantDaoTest3 extends AbstractRestaurantDaoTest {
>>
>>     private static Context context;
>>
>>     @BeforeClass
>>     public static void setUpBeforeClass() throws Exception
>>     {
>>       DBSetup.runDbUnitSetup();
>>       logger.warn("Strating testing using OpenEJB");
>>       Properties properties = new Properties();
>>       properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
>>               "org.apache.openejb.client.LocalInitialContextFactory");
>>       properties.put("openEjbDatabase", "new://Resource?type=DataSource");
>>       properties.put("openEjbDatabase.JdbcDriver", "com.mysql.jdbc.Driver");
>>       properties.put("openEjbDatabase.JdbcUrl",
>>               "jdbc:mysql://neptune:3306/dbtest");
>>       properties.put("openEjbDatabase.UserName", "sa");
>>       properties.put("openEjbDatabase.PassWord", "");
>>
>>       context = new InitialContext(properties);
>>       Object obj = context.lookup("RestaurantDao3Local");
>>       dao = (IDao) obj;
>>     }
>> }
>>
>> And the relevant dao code:
>>   �...@resource
>>     private UserTransaction utx;
>>     @Override
>>     public Restaurant create(Restaurant obj)
>>     {
>>       Restaurant created = obj;
>>       try
>>           {
>>               utx.begin();
>>               obj = getEntityManager().merge(obj);
>>               getEntityManager().persist(obj);
>>               getEntityManager().flush();
>>               utx.commit();
>>           } catch (Exception e)
>>           {
>>               e.printStackTrace();
>>           }
>>       return created;
>>     }
>>
>> The exception I am getting:
>> <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 is my persistence.xml:
>>
>> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>> <persistence version="1.0"
>>       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
>> http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
>> http://java.sun.com/xml/ns/persistence/orm
>> http://java.sun.com/xml/ns/persistence/orm_1_0.xsd";
>>       xmlns="http://java.sun.com/xml/ns/persistence";
>> xmlns:orm="http://java.sun.com/xml/ns/persistence/orm";
>>       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
>>       <persistence-unit name="Testing" transaction-type="RESOURCE_LOCAL">
>>               <class>ws.mansour.entities.Restaurant</class>
>>       </persistence-unit>
>> </persistence>
>>
>>
>> I do have few quesitons here:
>> 1- In the jndi Resource, I added openEjbDatabase as a DataSource, but
>> I don't understand how did it got picked by other DAO operations (ie.
>> read).
>> 2- I am using RESOURCE_LOCAL at transaction type, which indicates that
>> the container (context) will not handle this. How do I create a
>> tranaction ? With spring I was not able to do
>> entityManager.getTranaction(), because the entityManager injected by
>> spring is proxied (AFAIK). Doing this here, will give me another
>> error, for example:
>>       try
>>           {
>>               getEntityManager().getTransaction().begin();
>>               obj = getEntityManager().merge(obj);
>>               getEntityManager().persist(obj);
>>               getEntityManager().flush();
>>               getEntityManager().getTransaction().commit();
>>           } catch (Exception e)
>>
>> will give:
>>
>> java.lang.IllegalStateException: A JTA EntityManager can not use the
>> EntityTransaction API.  See JPA 1.0 section 5.5
>>       at
>> org.apache.openejb.persistence.JtaEntityManager.getTransaction(JtaEntityManager.java:220)
>>
>> What am I doing wrong ?
>>
>>
>
> --
> View this message in context: 
> http://old.nabble.com/Transaction-required-exception-tp26805898p26807467.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>
>

Reply via email to