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 ?

Reply via email to