1

I need to ensure that an update to an entity does not succumb to intermittent issues such as network errors.

The entity is being saved by a call to getSession().update(object); where getSession() returns the current Hibernate session.

Do I need to wrap the update statement with retries? Or is there some sort of guarantee provided by Hibernate?

n00b
  • 5,843
  • 11
  • 52
  • 82

2 Answers2

2

You can surround your code in a try-catch block if object is not updated it will rollback a transaction when an exception occurred you can handle in catch block. Like following

Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     sess.update(someobject);
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null)
     tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
  • Thanks. I probably should've worded my question better. I understand I can wrap it around a try/catch but wanted more info about any guarantees hibernate or connection pooling might provide. I've rewritten a separate question if you might be able to help: https://stackoverflow.com/questions/50242962/request-guarantees-with-hibernate-or-connection-pooling – n00b May 08 '18 at 22:12
1

You could surround it in a try-catch block and take compensatory actions in case of failure. For example, you could write failures to a message queue, to be processed later.

Andres
  • 10,561
  • 4
  • 45
  • 63
  • Thanks. I probably should've worded my question better. I understand I can wrap it around a try/catch but wanted more info about any guarantees hibernate or connection pooling might provide. I've rewritten a separate question if you might be able to help: https://stackoverflow.com/questions/50242962/request-guarantees-with-hibernate-or-connection-pooling – n00b May 08 '18 at 22:12