-->
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.  [ 6 posts ] 
Author Message
 Post subject: Another Lost Newbie - HSQLDB not saving
PostPosted: Mon Jul 24, 2006 10:00 pm 
Newbie

Joined: Mon Jul 24, 2006 8:55 pm
Posts: 6
I am using some sample apps I downloaded. The code compiles and runs without any exceptions, but no data is saved. I am using:
Windows XP
Eclipse 3.2
Hibernate 3.1.1
HSQLDB 1.8

The java code
=========

Code:
package actions;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import tables.Product;

public class Driver1 {
   private static Logger log =Logger.getLogger(Driver1.class);
   
   public static void main(String[] args)
                  throws Exception
   {
      Session session = null;
      Transaction tx = null;
      // 1. Build a Product
      Product p = new Product();
      p.setId(10);
      p.setName("Widgets");
      p.setAmount(100);
      p.setPrice(10.10);
      session = HibernateUtil.currentSession();
      tx = session.beginTransaction();

      try
      {
         session.save(p);
         tx.commit();
         session.flush();
         HibernateUtil.closeSession();
      }
      catch (HibernateException e) {
         e.printStackTrace();
         if (tx != null && tx.isActive())
            tx.rollback();
      }      
      //
      //   Now look for the Products just inserted
       String query = "select p from Product as p";
       session = HibernateUtil.currentSession();
       tx = null;
       tx = session.beginTransaction();
          
       // search and return
       List list = session.createQuery(query).list();
            
       if (list.size() == 0)
       {System.out.println("No products found ");}
          
      for (Iterator iter = list.iterator(); iter.hasNext();)
      {
         Product p2 = (Product) iter.next();
         System.out.println("Product ID = " + p2.getId());
         System.out.println("Name       = " + p2.getName());
         System.out.println("Price      = " + p2.getPrice());
         System.out.println("Amount     = " + p2.getAmount());
         System.out.println(" ");
      }
      tx.commit();
      session.flush();
      HibernateUtil.closeSession();

   
   }
}


This is HibernateUtil
=============
package actions;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

public static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this thread has none yet
if (s == null) {
s = sessionFactory.openSession();
// Store it in the ThreadLocal variable
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}

The Hibernate xml config
================
<?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">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>

<!-- HSQL connection -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>

<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<!-- use the 'file:' format to point to a database not in the project -->
<!-- added shutdown=true from comment in blog -->
<!-- is supposed to ix bug that prevents data saving to DB -->
<property name="hibernate.connection.url">jdbc:hsqldb:file:c:\temp\productsDB\products_t</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">2</property>

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- thread is the short name for
org.hibernate.context.ThreadLocalSessionContext
and let Hibernate bind the session automatically to the thread
-->
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>

<!-- this will create the database tables for us -->
<property name="hibernate.hbm2ddl.auto">create</property>

<mapping resource="tables/Order.hbm.xml" />
<mapping resource="tables/Product.hbm.xml" />
<mapping resource="tables/OrderItem.hbm.xml" />

</session-factory>

</hibernate-configuration>

The HSQLDB Properties
===============
#HSQL Database Engine
#Mon Jul 24 20:31:29 CDT 2006
hsqldb.script_format=0
runtime.gc_interval=0
sql.enforce_strict_size=false
hsqldb.cache_size_scale=8
readonly=false
hsqldb.nio_data_file=true
hsqldb.cache_scale=14
version=1.8.0
hsqldb.default_table_type=memory
hsqldb.cache_file_scale=1
hsqldb.log_size=200
modified=yes
hsqldb.cache_version=1.7.0
hsqldb.original_version=1.8.0
hsqldb.compatible_version=1.8.0

The HSQLBD script file
===============
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
CREATE MEMORY TABLE ORDERS(ID INTEGER NOT NULL PRIMARY KEY,ORDER_DATE TIMESTAMP NOT NULL,PRICE_TOTAL DOUBLE NOT NULL)
CREATE MEMORY TABLE PRODUCTS(ID INTEGER NOT NULL PRIMARY KEY,NAME VARCHAR NOT NULL,PRICE DOUBLE NOT NULL,AMOUNT INTEGER NOT NULL)
CREATE MEMORY TABLE ORDER_ITEMS(ID INTEGER NOT NULL PRIMARY KEY,ORDER_ID INTEGER NOT NULL,PRODUCT_ID INTEGER NOT NULL,AMOUNT INTEGER NOT NULL,PRICE DOUBLE NOT NULL)
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET WRITE_DELAY 20

????
So what am I doing wrong?

Thanks,
Rich Morrison


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 9:50 am 
Newbie

Joined: Tue Jul 18, 2006 10:47 am
Posts: 2
Location: Saint Louis, MO
I have about the same setup as you do and my transactions are being committed fine.... Well, I'm using spring with some AOP transaction interceptors as well.

I wonder if it may have something to do with a HSQLDB configuration issue.

Just some help.
-Turn debugging way up and make sure you see the actual inserts in the logs.

-Also you may be able to change the CREATE table to use "CACHED" instead of "MEMORY" I think the memory tables don't support transactions / rollbacks but I wouldn't think this would matter.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 11:18 am 
Newbie

Joined: Tue Nov 15, 2005 8:27 pm
Posts: 19
Location: Columbia, MO USA
With HSQLDB version 1.7.2 and later you have to set a connection property "shutdown=true" to be able to persist the changes you make to the database.

<property name="hibernate.connection.shutdown">true</property>

I think there is a comment in the config file you posted to that effect.

Bruce


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 11:44 am 
Newbie

Joined: Mon Jul 24, 2006 8:55 pm
Posts: 6
Bruce,

Yes there is a mention of shutdown in the HSQLDB manual. The example I saw used "shutdown=true" after the URL string.

I did not know there is a seperate key-value setting for shutdown.

I will give it a try.

Thanks,
RichM


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 11:27 am 
Beginner
Beginner

Joined: Thu Jul 20, 2006 12:08 pm
Posts: 21
Location: Germany
RichMorrison wrote:
Bruce,

Yes there is a mention of shutdown in the HSQLDB manual. The example I saw used "shutdown=true" after the URL string.

I did not know there is a seperate key-value setting for shutdown.

I will give it a try.

Thanks,
RichM


Hi RichM,

there ist a property "connection.shutdown"="true" in hibernate.cfg.xml

greets

marlon


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 05, 2006 9:57 pm 
Newbie

Joined: Tue Nov 15, 2005 8:27 pm
Posts: 19
Location: Columbia, MO USA
It can be done either way. You can put "shutdown=true" in your url string. You can also specify the property in the hibernate.cfg.xml file using the syntax I originally posted.

Bruce


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.