简体   繁体   中英

Exception on transaction rollback

I have a question about this Hibernate samples. I didn't found an answer in Hibernate docs or in Manning Persistence with JPA. May be someone can explain what happens when I use plain JDBC.

Session session = null;
Transaction tx = null;
try {
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    // Transaction actions   

    tx.commit();
} 
catch (RuntimeException ex) {
    try {
        tx.rollback();
    } 
    catch (RuntimeException rbEx) {
        log.error("Couldn’t roll back transaction", rbEx);
    }
    throw ex;
} 
finally {
    session.close();
}

My question is what will happen if transaction rollback method throw an exception? Will some transaction data be stored in database? How can I handle this exception?

My question is what will happen if transaction rollback method throw an exception?

It depends on what the exception is.

Will some transaction data be stored in database?

Unspecified. One would hope that the database will be able to recover to a point corresponding to the start of the transaction. However, there are scenarios where even that may not be possible; eg if you lost a disc drive, and you don't have a hot standby.

How can I handle this exception?

In general, you can't. If you get an exception and have no idea what it means or what caused it, the only sensible thing you can do (in a typical database application) is to shut the application down and get a human being to investigate the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM