Reputation: 301
i have web application. fetch from table working well
but when I'm trying to update the same table, by using JPA i got exception message:
javax.persistence.TransactionRequiredException: No active transaction for PuId =....
i am using transaction-type="JTA"(container manage) and transaction.required. it seemed that
**
I have an example of an update from MDB using JTA transaction type and there it's works well
**
this is my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="sysAdmin" transaction-type="JTA">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<jta-data-source>comp/env/jdbc/sys-SRV</jta-data-source>
<class>com.sys.admin.jpa.sysAdminAppl</class>
<class>com.sys.admin.jpa.sysAdminTable</class>
<class>com.sys.admin.jpa.sysAdminTablePK</class>
<class>com.sys.admin.jpa.Dbsys0001</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.jdbc.DBDictionary" value="sqlserver" />
<property name="Multithreaded" value="true" />
<property name="openjpa.jdbc.Schema" value="dbsys" />
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
</properties>
</persistence-unit>
<persistence-unit name="mtmAdminUpdate" transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<non-jta-data-source>comp/env/jdbc/jpa</non-jta-data-source>
<class>com.sys.admin.jpa.sysAdminAppl</class>
<class>com.sys.admin.jpa.sysAdminTable</class>
<class>com.sys.admin.jpa.sysAdminTablePK</class>
<class>com.sys.admin.jpa.dbsys0001</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.jdbc.DBDictionary" value="sqlserver" />
<property name="Multithreaded" value="true" />
<property name="openjpa.jdbc.Schema" value="dbsys" />
</properties>
</persistence-unit>
my servlet
package com.sys.admin.servlet;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Enumeration;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sys.admin.CRUD.MatamAppl;
import com.sys.admin.jqgridPage.PageApplDetail;
import org.codehaus.jackson.map.ObjectMapper;
/**
* Servlet implementation class ViewAppl
*/
@WebServlet("/ViewAppl")
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ViewAppl extends HttpServlet {
private static final long serialVersionUID = 1L;
@PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "sysAdmin")
public EntityManager emJPA = null;
. . . .
the controller
package com.sys.admin.jpa.controller;
import java.util.List;
import com.ibm.jpa.web.JPAManager;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.aspectj.weaver.patterns.ThrowsPattern;
import com.ibm.jpa.web.NamedQueryTarget;
import com.ibm.jpa.web.Action;
import com.sys.admin.jpa.SysAdminAppl;
import com.sys.admin.jpa.SysAdminApplPK;
import com.sys.admin.jpa.SysAdminTable;
@SuppressWarnings("unchecked")
@JPAManager(targetEntity = com.sys.admin.jpa.SysAdminAppl.class)
public class SysAdminApplManager {
private EntityManager em;
private String exceptionMessage;
.
.
.
.
@Action(Action.ACTION_TYPE.UPDATE)
public String updateSysAdminAppl(SysAdminAppl sysAdminAppl)
throws Exception {
EntityManager em = getEntityManager();
try {
//em.getTransaction().begin();
sysAdminAppl = em.merge(sysAdminAppl);
//em.getTransaction().commit();
} catch (Exception ex) {
exceptionMessage = ex.getMessage();
try {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} catch (Exception e) {
ex.printStackTrace();
exceptionMessage = e.getMessage();
throw e;
}
throw ex;
} finally {
em.close();
}
return "";
}
.
.
.
this is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>sysAdmin</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref id="adminDb">
<description></description>
<res-ref-name>aliasDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
.
ibm-web-ext.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-ext
xmlns="http://websphere.ibm.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_1.xsd"
version="1.1">
<reload-interval value="3"/><resource-ref name="resourceSqlserver" isolation-level="TRANSACTION_READ_UNCOMMITTED" connection-management-policy="DEFAULT"/>
<enable-directory-browsing value="false"/>
<enable-file-serving value="true"/>
<enable-reloading value="true"/>
<enable-serving-servlets-by-class-name value="true" />
</web-ext>
ibm-web-bnd.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-bnd
xmlns="http://websphere.ibm.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_1.xsd"
version="1.1">
<virtual-host name="default_host" />
<resource-ref name="aliasDataSource"
binding-name="comp/env/jdbc/SYS-SRV">
</resource-ref>
</web-bnd>
Upvotes: 2
Views: 6915
Reputation: 3410
To the best of my knowledge, CMP transactions are set by EJBs, not Servlets in the J2EE6 spec.
I have had similar problems in the past which were related to my misunderstanding of this point.
If you look here: http://docs.oracle.com/javaee/6/tutorial/doc/bncij.html, the documentation lays this out pretty clearly:
In an enterprise bean with container-managed transaction demarcation, the EJB container sets the boundaries of the transactions. You can use container-managed transactions with any type of enterprise bean: session or message-driven. Container-managed transactions simplify development because the enterprise bean code does not explicitly mark the transaction’s boundaries. The code does not include statements that begin and end the transaction. By default, if no transaction demarcation is specified, enterprise beans use container-managed transaction demarcation.
Typically, the container begins a transaction immediately before an enterprise bean method starts and commits the transaction just before the method exits. Each method can be associated with a single transaction. Nested or multiple transactions are not allowed within a method.
In other words... break out your persistence code into an EJB, and call that from your servlet.
Upvotes: 2
Reputation: 33946
You need to start a JTA transaction before invoking JPA: use @Resource UserTransaction userTran
, and then use userTran.begin()
/userTran.commit()
around the calls to JPA.
Upvotes: 2