Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Entity manager auto flush before query and commit changes to DB in transaction
    primarykey
    data
    text
    <p>I am using Hibernate 3.6.0 with JPA 2 on Jboss AS 6.0.0 final. In an EJB of mine, there's a method which updated entity values and do some query on it. The whole method is running in a BMT transaction. If anything fails, all changes should be rollback and not committed to DB.</p> <p>The Database is mySql.</p> <p>Before running JPA query, JPA will auto flush the changed states to DB to prevent any stale data from returning. However, within my method, the auto-flush directly update and commits the changes to DB and even something went wrong afterwards, the changes are not rollback. So I would like to ask if there's wrong configuration in my set up or this is a bug or something.</p> <p>EJB</p> <pre><code>@Stateless(mappedName = "MyManagementBean") @Local @TransactionManagement(TransactionManagementType.BEAN) public class MyManagement implements MyManagementLocal,MyManagementRemote { @PersistenceUnit(unitName="MyEjb") EntityManagerFactory emf; @Resource UserTransaction utx; @Resource SessionContext ctx; /** * Default constructor. */ public MyManagement () { // TODO Auto-generated constructor stub } public void dosomething(String id) throws Exception { try { utx.begin(); em = emf.createEntityManager(); Myline line = em.find(Myline.class, id); line.setStatus("R"); Stromg q += " from Myline as line "; //auto flush apply here and directly committed to DB... Iterator iter = em.createQuery(q).getResultList().iterator(); em.flush(); utx.commit();// changes should only commit after this } catch (Exception e) { e.printStackTrace(); if (utx != null) utx.rollback(); throw e; // or display error message } finally { em.close(); } } } </code></pre> <p>persistence.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;persistence version="2.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_2_0.xsd"&gt; &lt;persistence-unit name="MyEjb" transaction-type="JTA"&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;jta-data-source&gt;java:MyDS&lt;/jta-data-source&gt; &lt;class&gt;com.quincy.entity.MyLine&lt;/class&gt; &lt;properties&gt; &lt;property name="hibernate.connection.defaultNChar" value="true"/&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLMyISAMDialect"/&gt; &lt;property name="hibernate.ejb.cfgfile" value="META-INF/hibernate.cfg.xml"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p>hibernate.cfg.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; &lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="transaction.manager_lookup_class"&gt;org.hibernate.transaction.JBossTransactionManagerLookup&lt;/property&gt; &lt;!-- Echo all executed SQL to stdout --&gt; &lt;property name="show_sql"&gt;true&lt;/property&gt; &lt;property name="hibernate.max_fetch_depth"&gt;3&lt;/property&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>mysql-ds.xml</p> <pre><code>&lt;datasources&gt; &lt;local-tx-datasource&gt; &lt;jndi-name&gt;MyDS&lt;/jndi-name&gt; &lt;connection-url&gt;jdbc:mysql://10.10.150.57:3306/myds&lt;/connection-url&gt; &lt;driver-class&gt;com.mysql.jdbc.Driver&lt;/driver-class&gt; &lt;user-name&gt;user&lt;/user-name&gt; &lt;password&gt;pwd&lt;/password&gt; &lt;exception-sorter-class-name&gt;org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter&lt;/exception-sorter-class-name&gt; &lt;metadata&gt; &lt;type-mapping&gt;mySQL&lt;/type-mapping&gt; &lt;/metadata&gt; &lt;/local-tx-datasource&gt; &lt;/datasources&gt; </code></pre> <p>Upon further investigating, I found out that when ever there's a flush, the dirty changes are written and committed to DB directly. If I remove the flush(), everything works fine. However, there are system triggered flush before query and I think it's necessary.</p> <p>It seems to me that the db is auto-commited. I have tried to set the property <code>hibernate.connection.autocommit</code> to <code>false</code> but the problem just persist and a EJB warning of violating spec is prompted.</p> <p><strong>UPDATE:</strong> The cause should come from mysql. As if I switch to mssql server, the problem goes away.I also tried mysql with <code>xa-datasource</code> , still no luck...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload