0

I have a 3 layer web application which uses these frameworks: struts, spring and hibernate. in action class I have called a method in business logic layer which update data in a specific table. now I have to call another class to insert data in another table, these are different tables so have different classes in business logic layer and DAO layer. I want to have data integrity means if the second insert failed, I have to rollback the first one. how could I control it in action class.

try{
            policyIssuanceManagement.updatePolicy(currentPolicy);
            String result=policyDetailManagement.insertDataInPolicyDetail(policyId, riskAmount, riskPercent, riskId, regionRisk, regionPercent, regionId, stepped, steppedPercent, steppedId, discount, discountPercent, discountId);

            }
            catch (Exception e) {
                e.printStackTrace();
            }

I can not roll back the updated policy.

AFF
  • 1,515
  • 4
  • 21
  • 35

1 Answers1

1

You wrote 3 layer web application then I expect you application has service layer that implements with spring.So you should control your transaction into service layer using @Transational annotation inject in your service class.See sample service class

@Service
@Transational
public class PolicyManagement {
     @Transaction
     public void fooManagemantService() {
         policyIssuanceManagement.updatePolicy(currentPolicy);
         policyDetailManagement.insertDataInPolicyDetail(policyId, riskAmount, riskPercent, riskId, regionRisk, regionPercent, regionId, stepped, steppedPercent, steppedId, discount, discountPercent, discountId);
     }
}

This declarative transaction will rollback when one of the services method has failed.Don't forget to declare your spring-bean.xml or yourConfig.xml into

<tx:annotation-driven transaction-manager="txManager"/>

Here is the link for some reference.

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65