Hi,
I'm having some trouble using hibernate and hsqldb hopefully someone may have a clue what the problem is :).
The problem is that when I add something to the database, it looks like it's saved(I can run select and get the data) but when I quit the application and then rerun it, the data is gone. BUT, if I wait a random number of seconds before I quit the application, it may happen that some of data is saved. e.g. If I run 10 inserts, maybe 3 of them is saved when I enter the application again.
I've tried searching, and seen people having the same problem, but no solution.
I've also tried turning of the cache. If I don't use the option shutdown=true, nothing is saved when I rerun the app.
I also tried executing a "shutdown" sql query, with no result.
configureation file
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.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:file:dbFiles/data;shutdown=true</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<mapping resource="Data/User.hbm.xml"/>
<mapping resource="Data/Duty.hbm.xml"/>
<mapping resource="Data/Workday.hbm.xml"/>
<mapping resource="Data/PreferedWorkday.hbm.xml"/>
<mapping resource="Data/UserDutyWorkday.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My data class
Code:
public class DutyDAO{
private Transaction tx = null;
public DutyDAO(){
}
public void setDuty(Duty duty) throws CouldNotGetDataException{
try{
Session session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(duty);
tx.commit();
}
catch(Exception e){
if (tx != null && tx.isActive())
tx.rollback();
throw new CouldNotGetDataException("Could not save duty. reason: "+e.getMessage());
}
}
public Duty getDuty(int id) throws CouldNotGetDataException{
try{
Session session = HibernateUtil.getSession();
tx = session.beginTransaction();
Duty duty = (Duty) session.get( Duty.class, id );
tx.commit();
return duty;
}
catch(Exception e){
if (tx != null && tx.isActive())
tx.rollback();
e.printStackTrace();
throw new CouldNotGetDataException("Could not get duty with id "+id+". reason: "+e.getMessage());
}
}
public void deleteDuty(int id) throws CouldNotGetDataException{
try{
Duty duty = getDuty(id);
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.delete(duty);
tx.commit();
}
catch(Exception e){
if (tx != null && tx.isActive())
tx.rollback();
e.printStackTrace();
throw new CouldNotGetDataException("Could not delete duty with id "+id+". reason: "+e.getMessage());
}
finally
{
HibernateUtil.getSessionFactory().close();
}
}
}