使用事务管理抽象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;

   }

}

  

你可能感兴趣的:(事务管理)