Solved

Hibernate Not Rolling Back When something fails in a stored procedure

Posted on 2016-10-16
2
67 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 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 41846274
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
ID: 41846487
You're definitely right. I had a TRUNCATE TABLE command inside my transaction causing previous tables to be committed.
Thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 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