0

In our application we are using Hibernate 3. Recently I faced one issue in transactions. I have some code in following manner

    method1()
    try{
    tx = session.beginTransaction();
    //New object which needs to be stored
    session.save(object);
    // Some code which generates exception
    tx.commit(); // Exception occured before commit processing
    }catch(Exception ex){
    e.printStackTrace();
    throw e;
    }finally{
    if(tx!=null && tx.wasNotCommited()){
    tx.rollback();
    }
    }

I have another method same as this, which is called after this method even in case of exception is thrown from this method.

Session is same across both of the method.

Now what I am experiencing is if my 2nd method executes successfully and commits hibernate transaction, this stores data which has been saved in 1st method which I don't want.

I can not share exact code, but prepared one Test method which is similar to this issue. My database server is mysql.

package com.opshub.jobs.core;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.opshub.dao.core.Company;
import com.opshub.utils.hibernate.HibernateSessionFactory;

public class Test {

    public static void main(String[] args) throws Exception{
        Session session = HibernateSessionFactory.openSession();
        try{
            Transaction tx = session.beginTransaction();
            try{
                Company company = new Company();
                company.setName("Gaurav");
                company.setCompanyDetails("IT Company");
                company.setCompanyAddress("India");
                session.save(company);
                if(true)
                    throw new Exception("Now go to finally");
                tx.commit();
            }catch(Exception e){
                throw e;
            }finally{
                tx.rollback();
            }
        }finally{
            Transaction tx = session.beginTransaction();
            tx.begin();
            Company company = new Company();
            company.setName("Gaurav 1");
            company.setCompanyDetails("IT Company");
            company.setCompanyAddress("India");
            session.save(company);
//          if(true)
//              throw new Exception("Now go to finally");
            tx.commit();
        }
    }

}

Any help would be appreciated.

Kobi
  • 135,331
  • 41
  • 252
  • 292
Gaurav
  • 811
  • 9
  • 21
  • where are you calling the second method from ? from the code pasted after execution of method 1 it will commit the changes in method1 even before your second method is invoked . – Freaky Thommi Nov 19 '14 at 10:23
  • Before commit transaction is method1 if exception is thrown, how can it save data in my database? Both methods are simmiler, caller class is executing method1 from try, and method2 from finally. – Gaurav Nov 19 '14 at 10:27
  • close session after commit transaction, i think that's why the 2nd method stores data what the 1st method have stored. – Teifi Nov 19 '14 at 11:08

1 Answers1

1

Finally I found the solution to this issue. Before starting another transaction/operations you can use session.clear() which removes all the pending save/update/delete operation from the session.

So in finally with rollback, just add session.save().

Thanks for your responses.

Gaurav
  • 811
  • 9
  • 21