5

What happens with Entities in session if I make rollback ? Do they get back to the state before transaction ? In particular do they get new ids ?

Example:

session.startTransaction();
Entity e = new Entity(); //e.id == null
session.save (e);        //suppose it was ok
session.rollback();      // e.id == ???

Update:

I've made the Hibernate 4 test. After the test the entity has become a new id.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Tony
  • 2,266
  • 4
  • 33
  • 54
  • Have you also tested it? Did it work? Also have you tried to do a flush() before rollback() (so that you get an ID)? – V G Dec 17 '13 at 15:07
  • @AndreiI I've tested it with Hibernate 4. The id was assigned to entity, and not changed after rollback. – Tony Dec 17 '13 at 15:29

3 Answers3

7

I will simply quote from the JPA implementation (3.3.2 Transaction Rollback):

For both transaction-scoped and extended persistence contexts, transaction rollback causes all pre-exist-ing managed instances and removed instances[31] to become detached. The instances’ state will be the state of the instances at the point at which the transaction was rolled back. Transaction rollback typically causes the persistence context to be in an inconsistent state at the point of rollback. In particular, the state of version attributes and generated state (e.g., generated primary keys) may be inconsistent. Instances that were formerly managed by the persistence context (including new instances that were made persistent in that transaction) may therefore not be reusable in the same manner as other detached objects—for example, they may fail when passed to the merge operation.[32]

This actually means, that it depends whether you have or not an ID right before calling em.rollback() (which depends on FlushMode & JPA implementation). If an ID was assigned, than the ID will remain set. If not, then you will have no ID.

V G
  • 18,822
  • 6
  • 51
  • 89
  • 3
    Andrei is completely correct. But I just wanted to add that with Hibernate you can enable a setting `hibernate.use_identifier_rollback` (set to true) to reset the state of generated identifiers on a rollback. – Steve Ebersole Dec 17 '13 at 15:42
  • @SteveEbersole cool hint but as I understood `hibernate.use_identifier_rollback` is used if entity was deleted? I'm not sure what happens on rollback. See: [link to hibernate 3 docu](http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html) – Tony Dec 18 '13 at 12:49
4

It's not a bug, but a feature

Identity and Sequences are transaction-less. If they weren't, then one transaction would hold the lock, and if that transaction didn't release it for 5 minutes, no other transaction could insert any other record for the same table.

So, back to your questions:

What happens with Entities in session if I make rollback?

The entity identifier does not change at all. Now, rolling back the transaction means the flush hasn't gone through. So, while its fine to commit several consecutive transactions, if an exception is thrown and you roll back, then the Session should be close, as the Hibernate Session Javadoc says:

If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.

Do they get back to the state before the transaction?

No, of course not. The auto-generated ids are not bound to the currently running transaction.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
1

All managed entites will become detached.

Unless you are using an extended persistence context, rolling back also ends the persistence context.

(see OpenJPA docs: http://openjpa.apache.org/documentation.html)

WPrecht
  • 1,340
  • 1
  • 17
  • 29
  • Ok, thank you. But what will be happen to entities ? Will entities changed back to state before transaction ? – Tony Dec 17 '13 at 14:43
  • I'd have to test them, or you could, but I suspect the in memory entities will stay in whatever state they were at the roll back. The copies in the data store will, of course, be returned to their state before the transaction. – WPrecht Dec 17 '13 at 14:45