JPA实体经理由Websphere Container Managed Transaction关闭

时间:2012-01-04 07:50:53

标签: spring transactions websphere containers openjpa

我们的应用程序使用带有EJB3.0功能包的IBM WAS6.1容器。事务是通过从ejb服务bean启动的EJB3.0事务来容器管理的。我们使用Spring for DI。共有3层。 Spring DAO,Spring Service和EJB3.0 Bean服务。

现在我尝试将OpenJPA与WAS6.1一起使用。 EntityManager已成功注入SpringDAO,但容器在事务提交之前关闭EntityManager。 (Datasource是JNDI数据源)

这是stacktrace:

[1/4/12 9:23:17:769 EET] 0000001f ExceptionUtil E   CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "inquiryReconcilation" on bean "BeanId(PaymentSystemAppV1#PaymentSystemServiceEjbV1.jar#PaymentServiceBean, null)". Exception data: <openjpa-1.0.3-SNAPSHOT-r420667:649224 fatal user error> org.apache.openjpa.persistence.InvalidStateException: You have closed the EntityManager, though the persistence context will remain active until the current transaction commits.
at org.apache.openjpa.persistence.EntityManagerImpl.assertNotCloseInvoked(EntityManagerImpl.java:1068)

我们的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="PaymentSystemEntityV1" transaction-type="JTA">
    <jta-data-source>paymentDsSrv</jta-data-source>
    <mapping-file>META-INF/namedQueries.xml</mapping-file>
    /* entities */
    <properties>
        <property name="openjpa.TransactionMode" value="managed" />
        <property name="openjpa.ConnectionFactoryMode" value="managed" />
        <property name="openjpa.jdbc.DBDictionary" value="db2" />
        <property name="openjpa.jdbc.Schema" value="PAYMENT"/>
        <property name="openjpa.Log" value="DefaultLevel=INFO, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
    </properties>
</persistence-unit>

Spring Context:

    <bean id="dbsJpaDao" parent="daoBase" class="com.kavuntek.dds.dao.impl.DbsJpaDaoImpl" />

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <bean id="entityManagerFactory" class="javax.persistence.Persistence" 
         factory-method="createEntityManagerFactory" >
       <constructor-arg type="java.lang.String" value="PaymentSystemEntityV1"/>  
     </bean>

DAO:

public class DbsJpaDaoImpl implements IDbsJpaDao{

@PersistenceContext
private EntityManager entityManager;

public List<DbsLimit> subscriberLimitInquiry(){
    ...
    return entityManager.createQuery(queryString);
    }

EJB3.0服务Bean:

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionManagement(value = TransactionManagementType.CONTAINER)
@Interceptors(SpringBeanAutowiringInterceptor.class)
@Local(IDDSServiceV1.class)
public class DDSServiceBean iplements IDDSServiceV1{

@Autowired
private IDDSService ddsService;

public List<DbsInvoice> subscriberLimitInquiry()
... 

我还尝试使用以下参数弹出LocalContainerEntityManagerFactory:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
       <property name="persistenceUnitName" value="PaymentSystemEntityV1"/>
       <property name="dataSource" ref="dataSource"/>
       <property name="jpaVendorAdapter">
          <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"/>
       </property>
    </bean> 

因为事务不是Spring管理的,所以使用WebSphereUowTransactionManager很重要吗? 什么都没有改变。

<bean id="transactionManager"
class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>

有什么意见吗?

感谢。

P.S。当我在DAO中调用entityManager.getTransaction()时,我得到:

Exception data: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

0 个答案:

没有答案