Spring的事务控制-基于xml方式
程序员文章站
2022-07-09 14:27:03
介绍:该程序模拟了转账操作,即Jone减少500元,tom增加500元1.导入坐标 junit junit 4.13
介绍:该程序模拟了转账操作,即Jone减少500元,tom增加500元
1.导入坐标
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
2.创建Account实体类
public class Account {
private String name;
private String money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
}
3.创建AccountDao接口以及实现类AccountDaoImpl;
public interface AccountDao {
public void out(String outMan,double money);
public void in(String inMan,double money);
}
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
@Override
public void out(String outMan, double money) {
template.update("update account set money=money-? where name=?",money,outMan);
}
@Override
public void in(String inMan, double money) {
template.update("update account set money=money+? where name=?",money,inMan);
}
}
4.创建AccountService接口以及AccountServiceImpl实现类
public interface AccountService {
public void transfer(String outMan,String inMan,double money);
}
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outMan, String inMan,double money) {
accountDao.out(outMan,money);
accountDao.in(inMan,money);
}
}
5.编写spring配置文件(applicationContext.xml)
<?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">
<!--将AccountDao注入到spring容器-->
<bean id="accountDao" class="com.hao.dao.impl.AccountDaoImpl">
<property name="template" ref="jdbcTemplate"/>
</bean>
<!--将AccountService注入到spring容器-->
<bean id="accountService" class="com.hao.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
<property name="user" value="root"/>
<property name="password" value="hao20001010"/>
</bean>
<!-- 将模板对象注入到spring容器-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
6.数据库表的创建
7.测试
public class AccountController {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService service= context.getBean(AccountService.class);
service.transfer("Jone","tom",500);
}
}
结果:
将Jone和tom的钱修改为5000元
当我们在业务方法transfer中手动加入错误代码,让其报错
再此运行,我们发现控制台报错,接着查看数据库中的数据
出现这样的原因就是,当程序执行完accountDao.out(outMan,money)时,Jone的钱减少500,但是当执行下一步int i=1/0时,程序出现错误,然后就不再执行下一步功能了,所有出现了这样的情况;
这样的问题该怎么解决呢?
利用aop思想,将事务提取出来交给spring管理,在程序运行时,与方法进行切入即可;这样可以保证如果程序运行时出现错误,则双方的金额都不会改变
<?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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--将AccountDao注入到spring容器-->
<bean id="accountDao" class="com.hao.dao.impl.AccountDaoImpl">
<property name="template" ref="jdbcTemplate"/>
</bean>
<!--将AccountService注入到spring容器,目标对象,内部的方法就是切点-->
<bean id="accountService" class="com.hao.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置平台事务管理器,当前的DataSourceTransactionManager是jdbc、mybatis技术,如果以后使用了其他技术,则此处需要修改-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知,事务的增强,引入事务的命名空间tx-->
<tx:advice id="tx" transaction-manager="transactionManager">
<!--设置属性信息的 -->
<tx:attributes>
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop织入,advice-ref引入通知(唯一标识id) pointcut(切入的方法)-->
<aop:config>
<aop:advisor advice-ref="tx" pointcut="execution(* com.hao.service.impl.*.*(..))"/>
</aop:config>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
<property name="user" value="root"/>
<property name="password" value="hao20001010"/>
</bean>
<!-- 将模板对象注入到spring容器-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
将金额修改为5000元
再次运行:
===================================================
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
1.name:切入点方法名称
2.isolation:事务的隔离级别
3.propogation:事务的传播行为
4.timeout:超时时间
5.read-only:是否只读
本文地址:https://blog.csdn.net/Kevinnsm/article/details/109940646
推荐阅读