<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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>
<!--配置事务管理操作类-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<!--注入事务管理-->
<property name="transactionManager" ref="transactionManager"/>
<!--定义事务隔离级别,这里-1代表默认-->
<property name="isolationLevel" value="-1"/>
<!--配置传播行为,0代表PROPAGATION_REQUIRED-->
<property name="propagationBehavior" value="0"/>
<!--由于进行读写操作,所以这里的只读设置为false,默认也是false,所以可以不用设置-->
<property name="readOnly" value="false"/>
</bean>
</beans>
// 事务传播行为 int PROPAGATION_REQUIRED = 0; int PROPAGATION_SUPPORTS = 1; int PROPAGATION_MANDATORY = 2; int PROPAGATION_REQUIRES_NEW = 3; int PROPAGATION_NOT_SUPPORTED = 4; int PROPAGATION_NEVER = 5; int PROPAGATION_NESTED = 6; // 事务隔离级别 int ISOLATION_DEFAULT = -1; int ISOLATION_READ_UNCOMMITTED = 1; int ISOLATION_READ_COMMITTED = 2; int ISOLATION_REPEATABLE_READ = 4; int ISOLATION_SERIALIZABLE = 8; int TIMEOUT_DEFAULT = -1;
@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(String fromName, String toName, double money){
accountDao.transferOut(fromName, money);
accountDao.transferIn(toName, money);
}
}
accountDao.transferOut(fromName, money); int d = 1/0; // 除0异常 accountDao.transferIn(toName, money);
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
// 配置事务管理操作类
@Autowired
private TransactionTemplate transactionTemplate;
public void transfer(final String fromName,final String toName,final double money){
// 通过transactionTemplate进行事务的管理
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
accountDao.transferOut(fromName, money);
int d = 1/0; // 除0异常
accountDao.transferIn(toName, money);
}
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有