Spring声明式事务管理
声明式事务:在配置文件中声明
两种实现方式:
一、基于XML的声明式事务控制
二、基于注解的声明式事务控制
场景实现
A向B转账,A账户转出500,B账户转入500,如果转账过程中发生异常(例如A转出后,网络中断,B无法正常转入),A、B账户都不会执行操作。
项目结构

数据库表

一、基于XML的声明式事务控制
持久层:实体类
package com.domain;
public class Account {
String name;
int money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
Dao层:操作数据库
接口
package com.dao;
public interface AccountDao {
public void out(String name,int money);
public void in(String name,int monet);
}
实现类
package com.dao;
import org.springframework.jdbc.core.JdbcTemplate;
public class AccountDaoImp implements AccountDao{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
public void out(String name, int money) {
String sql = "update Account set money=money-"+money+" where name='"+name+"'";
jdbcTemplate.update(sql);
}
public void in(String name, int money) {
String sql = "update Account set money=money+"+money+" where name='"+name+"'";
jdbcTemplate.update(sql);
}
}
业务层
接口
package com.service;
public interface AccountService {
public void transfer(String outMan,String inMan,int money);
}
实现类
package com.service;
import com.dao.AccountDao;
public class AccountServiceImp implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao){
this.accountDao = accountDao;
}
public void transfer(String outMan, String inMan, int money) {
accountDao.out(outMan,money);
int a = 1/0;
accountDao.in(inMan,money);
}
}
控制层
package com.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.AccountService;
public class AccountController {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);
String inMan = "liu";
String outMan = "wang";
accountService.transfer(outMan, inMan, 500);
}
}
application.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/one"></property>
<property name="user" value="root"></property>
<property name="password" value="admin123"></property>
</bean>
<!-- jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="accountDao" class="com.dao.AccountDaoImp">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--目标对象 内部的方法就是切点-->
<bean id="accountService" class="com.service.AccountServiceImp">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知,事务的增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 设置事务的属性信息-->
<tx:attributes>
<tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--配置事务的aop织入-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
pom.xml,导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Spring</artifactId>
<groupId>groupId</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>it_tx</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>
5.0.5.RELEASE
</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
</project>
二、基于注解的声明式事务控制
持久层:实体类
package com.domain;
public class Account {
String name;
int money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
Dao层:操作数据库
接口
package com.dao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Component("accountDao")
//Dao层也可以用@Repository
public interface AccountDao {
public void out(String name, int money);
public void in(String name, int monet);
}
实现类
package com.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Component("accountDao")
//Dao层也可以用@Repository
public class AccountDaoImp implements AccountDao{
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
public void out(String name, int money) {
String sql = "update Account set money=money-"+money+" where name='"+name+"'";
jdbcTemplate.update(sql);
}
public void in(String name, int money) {
String sql = "update Account set money=money+"+money+" where name='"+name+"'";
jdbcTemplate.update(sql);
}
}
业务层
接口
package com.service;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component("accountService")
//业务层也可以用@Service
public interface AccountService {
public void transfer(String outMan, String inMan, int money);
}
实现类
package com.service;
import com.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
@Component("accountService")
//业务层也可以用@Service
public class AccountServiceImp implements AccountService {
@Autowired
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao){
this.accountDao = accountDao;
}
@Transactional(isolation = Isolation.REPEATABLE_READ)
public void transfer(String outMan, String inMan, int money) {
accountDao.out(outMan,money);
int a = 1/0;
accountDao.in(inMan,money);
}
}
控制层
package com.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.AccountService;
public class AccountController {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);
String inMan = "liu";
String outMan = "wang";
accountService.transfer(outMan, inMan, 500);
}
}
application.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 组件扫描-->
<context:component-scan base-package="com"></context:component-scan>
<!-- 数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/one"></property>
<property name="user" value="root"></property>
<property name="password" value="admin123"></property>
</bean>
<!-- jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

浙公网安备 33010602011771号