Hi,
Thanks again for your response.
I have made changes to my code by following the link you provided. Now the code is working properly.
The txn is getting rolled back if any of the methods fail. I have pasted the code again. Please suggest me if I have to implement anything to make my code work much better.
Code:
public void transferFunds(String fromEntity, double amt, String toEntity){
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
debitAcct(fromEntity, amt);
creditAcct(toEntity, amt);
tx.commit();
}catch(Exception e){
if(tx != null){
try{
tx.rollback();
}catch(HibernateException he){
logger.debug("Error in rolling back the txn");
}
}
}
}
private void debitAcct(String custId, double amtToDebit)
throws Exception{
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
try{
double bal = 0.0;
List list = session.createQuery("select ce from CASAEntity as ce where custId=:CustId").
setString("CustId",custId).list();
Iterator iter = list.iterator();
while(iter.hasNext()){
CASAEntity casa = (CASAEntity)iter.next();
logger.info("List of Customers");
logger.debug("{"+casa+" }");
bal = casa.getBalance() - amtToDebit;
casa.setBalance(bal);
// throw new Exception("Exception while debiting the account");
}
}catch(Exception e){
throw e;
}
}
private void creditAcct(String custId, double amtToCredit) throws Exception{
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
try{
double bal = 0.0;
List list = session.createQuery("select ce from CASAEntity as ce where custId=:CustId").
setString("CustId",custId).list();
Iterator iter = list.iterator();
while(iter.hasNext()){
CASAEntity casa = (CASAEntity)iter.next();
logger.info("List of Customers");
logger.debug("{"+casa+" }");
bal = casa.getBalance() + amtToCredit;
casa.setBalance(bal);
// throw new Exception("Exception while crediting the account");
}
}catch(Exception e){
throw e;
}
}
Thanks,
Alice