我使用EJB将数据插入/更新到数据库,我遇到了这个问题。我的提交仅适用于更新(合并),但不回滚插入(持久)。更重要的是,我的插入(持久化)发生在事务提交之前。
我的持久单位:
<persistence-unit name="bpmBeans_RESOURCE_LOCAL" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:jboss/datasources/java_orgstruct</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.connection.characterEncoding" value="UTF-8"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="hibernate.connection.autocommit" value="false"/>
</properties>
</persistence-unit>
我如何注入实体经理工厂
@PersistenceUnit(unitName="bpmBeans_RESOURCE_LOCAL")
public EntityManagerFactory emf;
代码
会发生什么EntityManager em = emf.createEntityManager();
EntityTransaction ets = em.getTransaction();
ets.begin();
Process newObj = new Process();
em.persist(newObj); //data gets inserted at this moment, not at commit
...
...
if(allgood){
ets.commit();// now update(merge, setters on managed entities) would happend.
} else {
ets.rollback(); // this would rollback the updates, but not the insert
}
任何想法尝试什么?我想我只是非常了解一些JPA概念。
编辑:
数据源代码。数据源在WEB-INF / mysql-ds.xml文件中描述。 Driver .jar在JBoss部署中。
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
<datasource jndi-name="java:jboss/datasources/java_orgstruct" pool-name="MySQLPool">
<connection-url>jdbc:mysql://192.168.150.141/organisation_model</connection-url>
<connection-property name="zeroDateTimeBehavior">convertToNull</connection-property>
<connection-property name="characterEncoding">utf8</connection-property>
<driver>mysql-connector-java-5.1.28-bin.jar</driver>
<security>
<user-name></user-name>
<password></password>
</security>
</datasource>
</datasources>