Solved

Hibernate Not Rolling Back When something fails in a stored procedure

Posted on 2016-10-16
2
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

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…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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