Dear everyone. I have code similar to this pseudocode:
Get session 1.
Create transaction 1.
Create an entity.
Commit transaction 1.
Close session 1.
Get session 2.
Create transaction 2.
Fetch the number of entities.
Commit transaction 2.
Close session 2.
Get session 3.
Create transaction 3.
Create an entity.
Commit transaction 3.
Close session 3.
It works as expected. However, if I
throw an exception after "Close session 2" something unexpected happens.
If the persistence mechanism is working in server mode, the first entity is saved and persisted, because the transaction is committed. That's OK. But in file mode, it is not persisted at all!I would expect it to be persisted because the transaction is committed. More, I would expect the file behavior to be consistent with the server behavior. But it isn't - why?
This is how I use server mode:
Code:
<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost/xdb</property>
And this is how I use the file mode:
Code:
<property name="hibernate.connection.url">jdbc:hsqldb:file:db\filedb;shutdown=true</property>
As you can see, I was forced to use shutdown=true, otherwise the changed were not saved at all, with the exception or without it.
I'm using hibernate 3.2.4.ga and hsqldb 1.8.0.7.
This is also active:
Code:
<property name="hibernate.hbm2ddl.auto">update</property>
Thanks for any hints.
Here's the code with the exception:
Code:
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello World");
Long messageId = (Long) session.save(message);
tx.commit();
session.close();
Session newSession = HibernateUtil.getSessionFactory().openSession();
Transaction newTx = newSession.beginTransaction();
List<Message> messages = newSession.createQuery(
"from Message m order by m.text asc").list();
System.out.println("# of messages: " + messages.size());
for (Message aMessage : messages) {
System.out.println(aMessage.getText());
}
newTx.commit();
newSession.close();
if (true) {
throw new RuntimeException("Uh oh");
}
Session thirdSession = HibernateUtil.getSessionFactory().openSession();
Transaction thirdTx = thirdSession.beginTransaction();
message = (Message) thirdSession.get(Message.class, messageId);
message.setText("Greetings Earthling");
message.setNextMessage(new Message("Take me to your leader"));
thirdTx.commit();
thirdSession.close();
HibernateUtil.shutdown();