Solved

Hibernate Not Rolling Back When something fails in a stored procedure

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Creating and Managing Databases with phpMyAdmin in cPanel.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 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