These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate and hsqldb - data is not always saved to file
PostPosted: Mon Sep 08, 2008 5:38 am 
Newbie

Joined: Mon Sep 08, 2008 4:04 am
Posts: 1
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();
        }
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 7:18 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Try adding this to your properties file:
<property name="hibernate.connection.shutdown">true</property>

http://forum.hibernate.org/viewtopic.php?t=940532


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.