<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启自动扫描-->
<context:component-scan base-package="cn.xuhuanfeng.transaction"/>
<!--配置数据源,这里采用dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="huanfeng"/>
</bean>
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--为AccountService创建代理类-->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!--注入事务管理-->
<property name="transactionManager" ref="transactionManager"/>
<!--注入目标类,也就是所要增强的类-->
<property name="target" ref="accountService"/>
<!--配置相应的事务属性-->
<property name="transactionAttributes">
<props>
<!--指定不同的事务的处理方式
配置格式:事务传播方式,隔离级别,readOnly,-Exception,+Exception
传播行为是唯一必须配置的,其他的如果不配置则使用默认
-Exception表示如果发生对应的异常,则回滚事务
+Exception表示即使发生对应的异常,也依旧提交事务
-->
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>
</bean>
</beans>
@Repository
public class AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void transferIn(String name, double money){
String sql = "update account set money = money + ? where name = ?";
jdbcTemplate.update(sql, money, name);
}
public void transferOut(String name, double money){
String sql = "update account set money = money - ? where name = ?";
jdbcTemplate.update(sql, money, name);
}
}
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
public void transfer(final String fromName,final String toName,final double money){
accountDao.transferOut(fromName, money);
int d = 1/0; // 除0异常
accountDao.transferIn(toName, money);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--
这里配置同前,故省略
-->
<!--aop配置-->
<aop:config>
<!--配置切点-->
<aop:pointcut id="serviceMethod" expression="execution(* cn.xuhuanfeng.transaction.AccountService.*(..))"/>
<!--对应的切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
<!--配置事务增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--配置对应的事务管理,其中name为与事务相关的方法名,可以使用通配符-->
<tx:method name="transfer*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
// 配置相应的隔离级别、事务传播等
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
@Service
public class AccountService {
// 省略其他内容
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--数据源配置等同上-->
<!--通过tx命名空间,开启主机自动扫描,并且注入事务管理器-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有