WildDev
WildDev

Reputation: 2367

When i have to use Hibernate's rollback

When i have to use Hibernate's rollback? Hibernate will do rollback itself in exception case, so my rollback line just external:

Session session = HibernateUtil.getSessionFactory().openSession();

try {
session.beginTransaction();

test cl = (test) session.createCriteria(test.class)
.add(...)
.list()
.get(0); // Here's throws an exception, Hibernate was rolled back automatically.

session.getTransaction().commit();
}
catch (Exception ex)
{
session.getTransaction().rollback(); // The transaction already was rolled back. Unnecessary?
}

That's the only approach to use Hibernate's rollback which i can invent but rollback has not sense there.

So in which cases i really have to use rollback?

Upvotes: 0

Views: 399

Answers (1)

user2833557
user2833557

Reputation: 677

In case there is no exception, but you still want to roleback because you found something wrong in your program logic, that's when you can use it. Like if you are updating multiple entities and your modified one and found some anomaly in the data according to your business logic, you can call rollback.

Upvotes: 1

Related Questions