我在cxf(jax-rs)+ Struts中使用OpenJPA 2.没有外部事务管理器。为什么我会收到“只能在事务处于活动状态时执行操作。”?
我的persistence.xml
是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="myPU"
transaction-type="RESOURCE_LOCAL">
<class>com.bb.bba.entity.Advertisement</class>
<class>com.bb.bba.entity.Poi</class>
<properties>
<property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="openjpa.ConnectionURL" value="jdbc:derby://localhost:1527/bba;create=true" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
<property name="openjpa.DynamicEnhancementAgent" value="false" />
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
<property name="openjpa.ConnectionUserName" value="APP" />
<property name="openjpa.ConnectionPassword" value="asdasd" />
<property name="openjpa.Log" value="SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
我收到以下错误。
35692 2012-06-02 03:50:57,847 DEBUG [http-33791-1] resolver.DefaultTraversableResolver.initJpa (DefaultTraversableResolver.java:77) - Found javax.persistence.PersistenceUtil on classpath.
35729 2012-06-02 03:50:57,884 DEBUG [http-33791-1] resolver.DefaultTraversableResolver.initJpa (DefaultTraversableResolver.java:91) - Instantiated an instance of org.apache.bval.jsr303.resolver.JPATraversableResolver.
35755 2012-06-02 03:50:57,910 TRACE [http-33791-1] jsr303.DefaultMessageInterpolator.loadBundle (DefaultMessageInterpolator.java:189) - ValidationMessages not found by thread local classloader
35755 2012-06-02 03:50:57,910 TRACE [http-33791-1] jsr303.DefaultMessageInterpolator.loadBundle (DefaultMessageInterpolator.java:189) - ValidationMessages not found by validator classloader
35756 2012-06-02 03:50:57,911 DEBUG [http-33791-1] jsr303.DefaultMessageInterpolator.getFileBasedResourceBundle (DefaultMessageInterpolator.java:176) - ValidationMessages not found. Delegating to org.apache.bval.jsr303.ValidationMessages
35778 2012-06-02 03:50:57,933 DEBUG [http-33791-1] xml.ValidationParser.parseXmlConfig (ValidationParser.java:88) - No META-INF/validation.xml found. Using annotation based configuration only.
646 myPU INFO [http-33791-1] openjpa.Runtime - OpenJPA dynamically loaded a validation provider.
922 myPU INFO [http-33791-1] openjpa.Runtime - Starting OpenJPA 2.1.1
1703 myPU INFO [http-33791-1] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.DerbyDictionary".
11326 myPU WARN [http-33791-1] openjpa.Enhance - Creating subclass for "[class com.bb.bba.entity.Poi, class com.bb.bba.entity.Advertisement]". This means that your application will be less efficient and will consume more memory than it would if you ran the OpenJPA enhancer. Additionally, lazy loading will not be available for one-to-one and many-to-one persistent attributes in types using field access; they will be loaded eagerly instead.
我在这里
......非致命用户错误&gt; org.apache.openjpa.persistence.TransactionRequiredException:只能 在事务处于活动状态时执行操作。 在org.apache.openjpa.kernel.BrokerImpl.assertActiveTransaction(BrokerImpl.java:4658) 在org.apache.openjpa.kernel.DelegatingBroker.assertActiveTransaction(DelegatingBroker.java:1386) 在org.apache.openjpa.persistence.EntityManagerImpl.flush(EntityManagerImpl.java:661) 在com.bb.bba.dao.MyDao.createPoi(MyDao.java:21) 在com.bb.bba.services.Service.createPoi(POIService.java:36)
答案 0 :(得分:3)
您必须先打开一个事务,然后才能对EntityManager执行操作。这是预期的行为。您将找到适合您的应用程序的交易策略。因此,您必须决定哪个组件打开事务(特别是在哪个层上)以及如何完成(声明式或编程式)。
您使用的是哪些其他框架?例如,如果您也使用Spring,则可以使用Spring的声明式事务管理支持轻松管理事务。
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html
答案 1 :(得分:1)
问题解决了。我正在使用resource_local
,这意味着我负责交易。添加em.getTransaction.begin
解决了这个问题。