声明式事务管理的含义:service中方法起什么名字就将决定该方法是否使用事务。
applicationContext.xml中对事务的配置
<!--事务管理的 增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--被管理的 方法, 需要事务的方法-->
<!--
isolation: 事务的隔离级别 default数据库缺省的隔离级别
read-only: 确定是否是 只读事务,
propagation: 事务的传播级别, 常用required , supports 只读事务选support 需要事务就required
rollback-for: 异常的类型,回滚
no-rollback-for: 异常类型,进行提交
-->
<!--声明式的事务管理 service中方法起什么名字就将决定是否使用事务-->
<tx:method name="transfer*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="insert*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="delete*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="find*" isolation="DEFAULT" read-only="true" propagation="SUPPORTS" />
<tx:method name="select*" isolation="DEFAULT" read-only="true" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<!--增强 和 事务管理对象切入-->
<aop:config>
<aop:pointcut id="txPointCut"
expression="execution(* com.chinasoft.spring.service.impl.AccountServiceImpl.*(..))"
></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" ></aop:advisor>
</aop:config>
在applicationContext.xml文件中先定义了以transfer,insert,delete,remove开头的方法将启用事务的
而find,select开头的方法不使用事务
<tx:method name="transfer*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="insert*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="delete*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="find*" isolation="DEFAULT" read-only="true" propagation="SUPPORTS" />
<tx:method name="select*" isolation="DEFAULT" read-only="true" propagation="SUPPORTS" />
service接口类中的方法如下
public interface AccountService {
// 删除 用户信息
void deleteAccount(Integer id);
// 进行银行的账号之间的转账
void transfer(String from, String to, float money) throws Exception;
List<Account> findAll();
}
则 deleteAccount,transfer两种方法会使用事务的管理
findAll方法不使用事务的管理。
总结:声明式事务管理先定义好了何种命名的方法使用事务,根据是否要启用事务来对方法命名。