Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1024419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:46:32+00:00 2026-05-16T11:46:32+00:00

Everything I read about Hibernate states that you must roll back a transaction and

  • 0

Everything I read about Hibernate states that you must roll back a transaction and close the session when an error occurs, and there’s usually some variation of the following code (taken from Hibernate’s docs) given as an example:

Session sess = factory.openSession();
Transaction tx = null;
try {
    tx = sess.beginTransaction();
    // do some work
    ...
    tx.commit();
} catch (RuntimeException e) {
    if (tx != null) tx.rollback();
    throw e; // or display error message
} finally {
    sess.close();
}

This pattern seems odd to me for a couple of reasons. First, it just seems unreasonably complicated for a framework that’s generally geared towards making things simple. More importantly, what happens if the code in the try block throws something other than a RuntimeException? It looks as if Hibernate must be able to gracefully close the session with an open transaction in that case, presumably by rolling it back, but if that’s true, why bother calling rollback at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:46:33+00:00Added an answer on May 16, 2026 at 11:46 am

    Hibernate may make a lot of things simpler, but transaction management just isn’t very simple so for every transaction you have to think very carefully what you want. Hibernate can’t help you with that.

    If the code in the try block throws anything other than a RuntimeException, your transaction obviously does not commit. But you do not explicitly rollback either. The sess.Close call in your finally block doesn’t rollback the transaction either. What happens depends on whether this is a nested transaction:

    • If it is not, then eventually the transaction times out and rolls back.
    • If it is, the parent transaction will see that a child transaction has not committed when it commits itself. This will result in a rollback of the entire transaction.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Everything that I read about sockets in .NET says that the asynchronous pattern gives
Everything I read about cookies says that setting the expiry time of a cookie
My head is about to explode with everything I've read about SQL Server and
I've been reading a lot about prepared statements and in everything I've read, no
Well is there? From everything I've read, it seems like the answer is no,but
Everything I've ever read indicates that in Javascript, the boolean value of an undefined
If you read this thread before - forget everything I wrote , I must
I'm stuck on a problem for hours now and having read everything about this
Everything I read about expires headers (http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/, eg) indicates that when taking advantage of
Everything i've read about objective-c memory management makes it sound incredible easy. To paraphrase

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.