JBoss AS 6 and JPA. Feature or bug ??
markboletti May 27, 2011 6:25 AMHi all,
I have a Java EE 6 application packed in a .war file which is running on JBoss AS 6.0.0. The application contains EJB 3.1 which are packed into the Web application. In particular I have a method which insert some inserts using the EntityManager Api:
@Stateless
public class SaveKpi {
@PersistenceContext(unitName = "userLocalDatabase")
private EntityManager em;
public void insert() {
try {
MetricTemp temp = new MetricTemp();
temp.setContentObjectSid("22345");
temp.setObjectNm("test");
em.persist(temp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
However this results in a
12:18:32,859 ERROR [STDERR] javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
12:18:32,859 ERROR [STDERR] at org.jboss.jpa.deployment.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:157)
12:18:32,859 ERROR [STDERR] at org.jboss.jpa.deployment.PersistenceUnitDeployment.verifyInTx(PersistenceUnitDeployment.java:318)
12:18:32,859 ERROR [STDERR] at org.jboss.jpa.impl.tx.TransactionScopedEntityManager.verifyInTx(TransactionScopedEntityManager.java:105)
12:18:32,859 ERROR [STDERR] at org.jboss.jpa.impl.tx.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:205)
12:18:32,859 ERROR [STDERR] at kpi.action.SaveKpi.insert(SaveKpi.java:66)
When trying from within a Bean Managed Transaction, insert is performed successfully.
@TransactionManagement(TransactionManagementType.BEAN)
public class SaveKpi {
@PersistenceContext(unitName = "userLocalDatabase")
private EntityManager em;
@Resource
public UserTransaction utx;
public void insert() {
try {
utx.begin();
MetricTemp temp = new MetricTemp();
temp.setContentObjectSid("22345");
temp.setObjectNm("test");
em.persist(temp);
em.flush();
utx.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Is it a bug or simply I cannot initiate an EJB CMT transaction from within the Web context....maybe I should pack the EJB in a .jar file and assemble all the application in a .ear package ?
Thanks a lot in advance
Mark