Monday, February 22, 2010

JFace TreeViewer collapses after refresh/setInput

A problem: TreeViewer collapses all nodes of the tree after calling refresh() or setInput().

Solution: Implement equals() and hashCode() functions for the Objects provided by your TreeContentProvider. JFace needs to copy the state of the 'old' input of the tree to new input. In order to do this it needs to find the objects from 'old' input in 'new' input. So, it needs equals and hashCode functions. If you don't implement them, the default Object equals and hashCode are used.

Saturday, February 20, 2010

Hibernate + HSQLDB - problem with persisting entities.

Problem


Entities do not get stored when working with Hibernate+HSQLDB (embedded mode - file)


Description

I just wrote a very trivial piece of code to use Hibernate with HSQLDB. the main is like this:


public static void main(String[] args){


Session s = HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
Role role = new Role("grupa1");
s.persist(role);
s.getTransaction().commit();
}

I skip the hibernate configuration file. It was correct, very similar to what you can see in the hibernate tutorial.


  • Program exited normally,

  • no exceptions,

  • nothing incorrect in the log file,

  • console displayed the SQL inserts,

  • transaction status checked with wasCommited() method returned true.



However:

  • no data was stored in the database,

  • this piece of code worked well with PostgreSQL as a database,

  • similar piece of code worked well with HSQLDB in some long running aplications,

  • I ran this piece of code many times. The entity was persisted correctly only once or twice,

  • if I added Thread.sleep(3000) after the commit statement, the entity was saved in the database



The problem appears when the application terminates 'soon' after the transaction commit. ('soon' mean less than 1s). The data is not stored when HSQLDB is terminated soon after the transaction commit. I suppose that some operations required to store data were not executed by HSQLDB ( but... the application called commit() successfully. It looks quite dangerous... )

Solution:



I did two things. It solved the problem:

  • modify the jdbc connection url. Add the shutdown=true parameter:
    <property name="connection.url"&rt;jdbc:hsqldb:file:db/db;shutdown=true</property>

  • call sessionFactory.close() before exiting the application:


    public static void main(String[] args){


    Session s = HibernateUtil.getSessionFactory().getCurrentSession();
    s.beginTransaction();
    Role role = new Role("grupa1");
    s.persist(role);
    s.getTransaction().commit();
    HibernateUtil.getSessionFactory().close();
    }



It works now.

init()

The idea of this blog came into my mind about 20 minutes ago. For the last 18 minutes I was unsuccessfully trying to create some account login. All of them were already in use. Life's tough.

Hopefully it will have more than one entry...

The idea is to describe so some problems I encountered when writing some piece of software and describe solution (if known) in a short and clear way. These may by various problems, usually already described and explained somewhere else. ( There several billion people in the world. If you have a problem, chances are that someone else in the world had the same problem and found solution. Possibly google knows about it:-) )But this blog is meant to provide short and clear description and solution (if possible), so that you don't have to read pages of some forum discussion to find the answer somewhere in the middle.

Generally the problems will be connected with Java, J2EE, Java Frameworks(Hibernate, Seam), RDBMS, etc.

Maybe the blog will die in about 15 minutes or something like 3 days and will become another abandoned projects in the internet universe :-).