Hi all,
I am dealing with an interesting situation when using hibernate + mysql and I don't know exactly if it is a bug (in Hibernate or MYSQL or both).
I DO NOT HAVE THE SAME PROBLEM WITH POSTGRESQL:
I did a simple class (see below) to test Hibernate's behavior under a stress situation. During the execution of my class, after some iterations (about 100 in one single thread), I get an exception (also below).
I know that it is an ordinary exception. It's telling me that I tried to load and object that does not exists in DB. But is not true. This object was persisted in the previous step of my thread, i.e, it is in DB.
Thanks for the attention.
My class
public class Modulo {
private id;
private nome
//getters and setters
public String toString{//write the object};
}
My hibernate-mapping file:
<hibernate-mapping>
<class name="br.com.facosta.hibernate.Modulo " table="modulo" lazy="false" batch-size="20">
<id name="id" column="id" type="java.lang.Long">
<generator class="native">
<param name="sequence">modulo_sequence</param>
</generator>
</id>
<property name="nome" type="java.lang.String" update="true" insert="true" column="nome" length="200" not-null="true" unique="true"/>
</class>
</hibernate-mapping>
My Hibernate cfg file:b
....
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
.....
<mapping resource="br/com/facosta/hibernate/Modulo.hbm.xml"/>
My Java Program:
public class Teste implements Runnable{
private static long max = 10000; //Number of iteractions for each Thread
private Thread work;
public Teste(String nome)
{
work = new Thread(this, nome);
work.setDaemon(false);
System.out.println(work);
work.start();
}
private Long insertDb(){
Transaction trans = null;
Modulo modulo = null;
try{
Session session = HibernateUtil.currentSession();
modulo = new Modulo();
modulo.setNome(work.getName() + " i " + System.currentTimeMillis());
trans = session.beginTransaction();
session.saveOrUpdate(modulo);
trans.commit();
System.out.println("INSERT " + work.getName() + " object =" + modulo);
}catch(HibernateException hi){
System.out.println("INSERT EXCEPTION " + work.getName() + " = " + modulo);
if (trans != null) trans.rollback();
hi.printStackTrace();
System.exit(1); //Stop in first exception. Just for debug
}finally{
HibernateUtil.closeSession();
}
return modulo.getId();
}
private void updateDb(Long moduloId){
Transaction trans = null;
Modulo modulo = null;
try {
Session session = HibernateUtil.currentSession();
modulo = (Modulo) session.load(Modulo.class, moduloId);
trans = session.beginTransaction();
modulo.setNome(work.getName() + " u " + System.currentTimeMillis());
session.saveOrUpdate(modulo);
trans.commit();
System.out.println("UPDATE " + work.getName() + " object =" + modulo);
} catch(HibernateException hi) {
System.out.println("UPDATE EXCEPTION " + work.getName() + " = " + modulo);
if (trans != null) trans.rollback();
hi.printStackTrace();
System.exit(1);//Stop in first exception. Just for debug
} finally {
HibernateUtil.closeSession();
}
}
private void readDb(Long moduloId, String type){
Modulo modulo = null;
try {
Session session = HibernateUtil.currentSession();
modulo = (Modulo) session.load(Modulo.class, moduloId);
System.out.println("READ " + work.getName() + " type = " + type +" object = " + modulo);
} catch(HibernateException hi) {
System.out.println("READ EXCEPTION " + work.getName() + " = " + modulo);
hi.printStackTrace();
System.exit(1);//Stop in first exception. Just for debug
} finally {
HibernateUtil.closeSession();
}
}
public void run(){
try{
for (int i = 0; i < max; i++){
System.out.println("INSERT Thread: " + work.getName() + " count: " + i);
Long moduloId = insertDb();
readDb(moduloId, " AFTER INSERT ");
System.out.println("UPDATE Thread: " + work.getName() + " count: " + i);
updateDb(moduloId);
readDb(moduloId, " AFTER UPDATE ");
}
}catch(Exception ie){
System.out.println("RUN EXCEPTION " + work.getName());
ie.printStackTrace();
}
}
public static void main(String[] args) {
new Teste("SOMETHING1");
new Teste("SOMETHING2");
new Teste("SOMETHING3");
new Teste("SOMETHING4");
new Teste("SOMETHING5");
}
}
Hibernate version:
3.0.5
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [br.com.facosta.hibernate.Modulo#360]
at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:118)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:151)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:79)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:603)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:596)
at br.com.facosta.teste.Teste.readDb(Teste.java:88)
at br.com.facosta.teste.Teste.run(Teste.java:108)
at java.lang.Thread.run(Thread.java:534)
Name and version of the database you are using:
mysql-4.1.12-pc-linux-gnu-i686
|