Solved

Hibernate Not Rolling Back When something fails in a stored procedure

Posted on 2016-10-16
2
36 Views
Last Modified: 2016-10-17
Hello Sirs,
I noticed something very strange. I have a MySQL stored procedure structured as follows:
      DECLARE EXIT HANDLER FOR SQLEXCEPTION
            BEGIN
                  ROLLBACK;
                  RESIGNAL;
            END;
     
      SET SESSION sql_mode='';

      START TRANSACTION;

        COMMIT;

In my hibernate.cfg.xml, I set autoCommit to false.
On top of that my DAO calling the SP looks like this:
public boolean runBillingCycle(TiersContrat data) {
        Session session = null;
        Transaction tx = null;
        try {
            session = FinanceHibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
           
            session.doWork((Connection cnctn) -> {

                String sql = "{CALL erp_partners_billing_cycle(?,?,?,?)}";

                CallableStatement stmt = cnctn.prepareCall(sql);

                stmt.setInt("p_companyID", data.getCompanyID());
                stmt.setInt("p_periode", data.getPeriode());
                stmt.setString("p_majuser", data.getMajUser());
                stmt.setString("p_typetiers", data.getTypeTiers());
                stmt.execute();

            });
           
            tx.commit();

          } catch (Exception e) {
            FinanceHibernateUtil.rollbackTransaction(tx);
            MyExceptionUtil.handle(null, e);
            return false;

        } finally {
            FinanceHibernateUtil.closeSession(session);
        }
        return true;
    }

What can the reason for Hibernate or MySQL to not rolling back tables updated before in the same transaction when something goes wrong?

I am looking forward to hearing from you.
0
Comment
Question by:Omer-Pitou
2 Comments
 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
Comment Utility
Hi!

Is this a nested transaction ? If so an implicit commit is issued for any current transaction when you issue "START TRANSACTION" (see here and here for further info)

Regards,
     Tomas Helgi
0
 

Author Comment

by:Omer-Pitou
Comment Utility
You're definitely right. I had a TRUNCATE TABLE command inside my transaction causing previous tables to be committed.
Thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now