javax.persistence.TransactionRequiredException:没有PuId的活动事务=
我有一个web应用程序。 从桌子上取东西很好用 但是 当我尝试更新同一个表时,通过使用JPA,我得到异常消息:javax.persistence.TransactionRequiredException:没有PuId的活动事务=,java,jpa,web-applications,websphere-8,Java,Jpa,Web Applications,Websphere 8,我有一个web应用程序。 从桌子上取东西很好用 但是 当我尝试更新同一个表时,通过使用JPA,我得到异常消息: javax.persistence.TransactionRequiredException: No active transaction for PuId =.... 我使用的是transaction type=“JTA”(容器管理)和transaction.required。 似乎 ** 我有一个使用JTA事务类型从MDB进行更新的示例,它运行良好 ** 这是我的persiste
javax.persistence.TransactionRequiredException: No active transaction for PuId =....
我使用的是transaction type=“JTA”(容器管理)和transaction.required。
似乎
**
我有一个使用JTA事务类型从MDB进行更新的示例,它运行良好
**
这是我的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>
<?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>
<?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>
<?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>
。
.
.
控制器
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 "";
}
.
.
.
这是我的web.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>
<?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>
<?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>
<?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>
系统管理员
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
别名数据源
javax.sql.DataSource
容器
可分享
ibm-web-ext.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>
<?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>
<?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>
<?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>
ibm web bnd.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>
<?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>
<?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>
<?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>
您需要在调用JPA之前启动JTA事务:使用@Resource UserTransaction userTran
,然后在调用JPA之前使用userTran.begin()
/userTran.commit()
。据我所知,CMP事务是由EJB设置的,而不是J2EE6规范中的servlet
我过去也遇到过类似的问题,这与我对这一点的误解有关
如果您看这里:,文档
很清楚地说明了这一点:
在具有容器管理的事务划分的企业bean中,
EJB容器设置事务的边界。你可以用
具有任何类型企业bean的容器管理事务:
会话或消息驱动。容器管理的事务简化了
开发,因为企业bean代码没有显式标记
交易的边界。代码不包括语句
开始和结束事务。默认情况下,如果没有事务
如果指定了界限,企业bean将使用容器管理
事务划分
通常,容器在事务发生之前立即开始事务
EnterpriseBean方法启动并在之前提交事务
方法退出。每个方法都可以与单个
交易在一个数据库中不允许嵌套或多个事务
方法
换句话说。。。将持久性代码分解成EJB,并从servlet调用它