0

I use EJB to insert/update data to the database and I have this problem. My commit only works with updating(merge), but doesn't rollback inserts(persist). Even more, my inserts(persist) happen before transaction commit.

my persistence unit:

<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>

how i inject entity manager factory

@PersistenceUnit(unitName="bpmBeans_RESOURCE_LOCAL")
public EntityManagerFactory emf;

what happens with the code

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
}

Any ideas what to try? I guess I'm just understanding some JPA concepts badly.

EDIT:

Datasource code. Datasource is described in WEB-INF/mysql-ds.xml file . Driver .jar is in JBoss deployments.

<?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>
ptrdom
  • 100
  • 9
  • 1
    I don't think you should be using the jta-data-source tag in your persistence.xml with a resource-local transaction type. see http://stackoverflow.com/questions/3217586/difference-between-a-jta-datasource-and-a-resource-local-datasource for details. I'm also not sure how the hibernate.transaction settings interact with the resource_local trans type. Would the defaults not suffice to allow you to control the transaction with em.getTransaction()? – Chris Mar 18 '14 at 14:56
  • Setting to non-jta-data-source tag didn't help. My datasource is described in application, I'll add the code now. It seems like remove and update queries happen on/after commit(), but insert, whether using .merge() or .persist() happens instantly after that command. Any ideas? – ptrdom Mar 19 '14 at 09:00
  • Did you remove the JTA settings I mentioned as well? If so, show what you are now using. Also, how are you sure that the commit happens when persist is called, and what code are you using for the delete and update? If you call em.flush() after the merges and deletes, do they still get rolled back if you call ets.rollback()? Why are you setting the hibernate.connection.autocommit property to false and other hibernate.connection properties - have you tried with a simple test first and then added properties you absolutely need? – Chris Mar 19 '14 at 12:11

0 Answers0