Hi all,
I am using Hibernate 3.6.0 with JPA on a BMT EJB 3 stateless session bean on Jboss AS 6.0.0 final. Everything seems to work fine at the beginning, however, I found out that either manually calling em.flush() in code or the auto flush() before another query would force any dirty changes to be written to the database and getting committed. Even if the method rollback afterwards, the changes do not rollback.
Is this an expected behavior or a bug or am I doing something wrong with my setting?
As I was using native hibernate, the flush would not commit the changes to db directly.
Here's my simplified code and setting.
persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="MyEjb" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:MyDS</jta-data-source>
<class>com.quincy.entity.MyLine</class>
<properties>
<property name="hibernate.connection.defaultNChar" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLMyISAMDialect"/>
<property name="hibernate.ejb.cfgfile" value="META-INF/hibernate.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>
hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hibernate.max_fetch_depth">3</property>
</session-factory>
</hibernate-configuration>
session bean
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();
}
}
}
This has been driving me crazy for days. Can somebody help me?