1


我使用带有 InnoDB 引擎的 OpenEjb 4.5.0 和 MySQL 5.5.xx。
我有一个非常简单的无状态 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
一些挖掘,实际原因是 dbcpManagedConnection.commit抛出异常,如果transactionContext != null

任何方向/提示将不胜感激。

谢谢,
阿维拉姆

4

1 回答 1

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>
于 2013-01-28T08:31:08.677 回答