使用事务管理抽象API进行事务界定的代码示例

public class FooService
{
   private PlatformTransactionManager transactionManager;
   
   public void serviceMethod()
   {
   
		TransactionDefinition definition=...;
		TransactionStatus txStatus=getTransactionManager().getTransaction(definition);
		
		try{
		
		 // dao1.doDataAccess();
		 // dao2.doDataAccess();
		 // ...
		} catch(DataAccessException e){
		 getTransactionManager().rollback(txStatus);
		 throw e;
		}catch(OtherNecessaryException e){
		 getTransactionManager().rollback(txStatus);
		 throw e;
		} 
   
   }

   public PlatformTransactionManager getTransactionManager(){
    return transactionManager;
   }
  
   public void setTransactionManager(PlatformTransactionManager transactionManager){
    this.transactionManager=transactionManager;
   }
}

  

posted @ 2012-07-10 14:24  来自非洲大草原的食人虎  阅读(197)  评论(0编辑  收藏  举报