?
Solved

Hibernate Not Rolling Back When something fails in a stored procedure

Posted on 2016-10-16
2
Medium Priority
?
99 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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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. . .
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 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