使用OpenEJB 4.5.0和MySQL 5.5执行事务时出现异常

时间:2013-01-27 13:35:00

标签: transactions ejb-3.1 openjpa openejb


我使用OpenEjb 4.5.0和MySQL 5.5.xx和InnoDB引擎 我有一个非常简单的无状态bean,注入了EntityManagerFactory

@PersistenceUnit
private EntityManagerFactory factory

我正在进行如下交易:

EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
  tx.begin();
  Query query = em.createNativeQuery("INSERT IGNORE INTO `myTable` (myColumn) values (1)");
  query.executeUpdate();
  tx.commit();
} catch (RuntimeException re) {
  if (tx.isActive())
    tx.rollback();
} finally {
  em.close();
}

具有以下配置:
的persistence.xml

<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="ejbPU-ro" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <non-jta-data-source>mySQLDataSource</non-jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(foreignKeys=true"/>
            <property name="openjpa.jdbc.SchemaFactory" value="native(foreignKeys=true)"/>
            <property name="openjpa.Log" value="DefaultLevel=TRACE,SQL=TRACE"/>
        </properties>
    </persistence-unit>
</persistence>

openejb.xml (仅限数据源定义):

<Resource id="mySQLDataSource" type="DataSource">
    JdbcDriver          com.mysql.jdbc.Driver
    JdbcUrl             jdbc:mysql://127.0.0.1:3307/myDb?relaxAutoCommit=true
    UserName            root
    Password
</Resource>

运行我的小程序,我遇到以下异常:
 org.apache.openjpa.persistence.RollbackException: Commit can not be set while enrolled in a transaction
一些挖掘和实际原因是dbcp ManagedConnection.committransactionContext != null

时抛出异常

任何指示/提示都将受到赞赏。

谢谢,
Aviram

1 个答案:

答案 0 :(得分:0)

好的,这很尴尬:) 我忘记了最后两行:

    <Resource id="mySQLDataSource" type="DataSource">
        JdbcDriver          com.mysql.jdbc.Driver
        JdbcUrl             jdbc:mysql://127.0.0.1:3307/myDb
        UserName            root
        Password            
        JtaManaged          false
        DefaultAutoCommit   false
    </Resource>