0
votes

I am using spring transactions and hibernate to insert data into the oracle database table:

Here is the scenarion I am facing problem with: I have two tables which have one to one mapping in hibernate. And I am inserting data in these two tables using below method calls.Transaction propagates from one method to another.So that insertion of data in both the tables happens in one transaction.

Problem: is that , while inserting data in second table, if an exception like "constraintvoilationexception---can not insert null into a particular column", is thrown,....then ideally the data should not be inserted in any of the tables i.e transaction should roll back,,,...But this is not happening ...when an exception is thrown while inserting data in the second table....records do get inserted in the first table, which ideally should not happen i.e whole transaction should be rolled back...

Can you please help,...where I am wrong while applying @Transactional , or there is some other reason for this ( may be from database side, not sure though)

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void methodA(){
  // inserting data in table 1;
methodB();

}

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void methodB{
// inserting data in table 2;

}
1
check are you catching any exceptions in your code?dassum
Also, check if you are throwing runtime exceptions or checked Exceptiondassum
Posting fake code won't help us find the bug in your real code. Post real code that actually reproduces the issue.JB Nizet
Its confidential data... M sorry... I cannotR Ram
@dassum....m throwing checked exceptions at certain pointsR Ram

1 Answers

-1
votes

Defines zero (0) or more exception classes, which must be subclasses of Throwable, indicating which exception types must cause a transaction rollback. Details Here

  @Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
          public void methodA(){
          try{

          // inserting data in table 1;
           methodB();
         }
          catch(Exception ex)
         {
           }

        }

        public void methodB{
        // inserting data in table 2;

        }