> 文章列表 > SpringMVC事务控制(xml文件配置和注解配置)

SpringMVC事务控制(xml文件配置和注解配置)

SpringMVC事务控制(xml文件配置和注解配置)

事务的定义

事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。
原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的操作要么都做,要么都不做。
一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
持久性(durability)。持久性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。

 

   事务(Transaction)是访问并可能更新数据库中各项数据项的一个程序执行单元。 事务由事务开始(begin transaction)和事务结束(end transaction)之间执行的全体操作组成。

        事务是一个不可分割的数据库操作序列,也是数据库并发控制的基本单位,其执行的结果必须使数据库从一种一致性状态变到另一种一致性状态。

        事务结束有两种,事务中的步骤全部成功执行时,提交事务。如果其中一个失败,那么将会发生回滚操作,并且撤销之前的所有操作。也就是说,事务内的语句,要么全部执行成功,要么全部执行失败。

        事务是恢复和并发控制的基本单位。

        事务具有四个特征:原子性、一致性、隔离性和持久性。这四个特征通常称为ACID。

 

编程式事务控制三大对象

1.PlatformTransactionManager
2.TransactionDefinition
3.TransactionStatus

PlatformTransactionManager对象
PlatformTransactionManager 接口是 spring 的事务管理器,它里面提供了我们常用的操作事务的方法

TransactionDefinition

TransactionStatus

SpringMVC要做的事情
在SpringMVC中我们主要要做的是当事务出现问题的时候把数据给回滚
比如A向B转钱
这要有两个动作:1.A 减钱 2.B加钱 如果1 和2 中间出现问题就把数据给回滚

转钱例子的环境搭建(用配置文件的方式)
在搭建环境的时候一共需要三个包 Controller Service 和Dao
Dao是最底层的 他相当于是只提供最底层的零件
Service是用Dao层提供的零件来组装成工具来让Service层来使用
Controller是用Service提供的工具来完成一些功能
在Dao层和Service层 我们要先写接口 来确定我们要完成什么功能
然后再写这些接口的实现类 impl

这下面的实现类中都提供了set方法是为了让spring容器通过xml配置文件来注入

dao层接口

public interface AccountDao {public void out(String outMan,double money);public void in(String inMan,double money);
}

dao层接口的实现

public class AccountDaoimpl implements AccountDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}@Overridepublic void out(String outMan, double money) {jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);}@Overridepublic void in(String inMan, double money) {jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);}
}
public class AccountDaoimpl implements AccountDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}@Overridepublic void out(String outMan, double money) {jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);}@Overridepublic void in(String inMan, double money) {jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);}
}

service层接口

public interface AccountService {public void transfer(String outMan,String inMan,double money);}

service层接口的实现

public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDaoimpl accountDao) {this.accountDao = accountDao;}@Overridepublic void transfer(String outMan, String inMan, double money) {accountDao.out(outMan,money);//  int a=1/0;accountDao.in(inMan,money);}
}

实现功能的controller

public class AccountController {public static void main(String[] args) {ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");AccountService bean = app.getBean(AccountService.class);bean.transfer("tom","lucy",500);}
}

配置文件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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://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 name="jdbcUrl" value="jdbc:mysql://localhost:3306/test1"/><property name="user" value="root"/><property name="password" value="123456"/></bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean><bean id="accountDao" class="com.tong.dao.impl.AccountDaoimpl"><property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean><bean id="accountService" class="com.tong.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"/></bean><bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean>
<tx:advice id="txadvice" transaction-manager="transcationManager"><tx:attributes><tx:method name="*"/></tx:attributes>
</tx:advice>
<aop:config><aop:advisor advice-ref="txadvice" pointcut="execution(* com.service.*.*(..))"/>
</aop:config></beans>

通知 (事务)的增强

<!--    通知 事务的增强-->
<tx:advice id="txAdvice" transaction-manager="transcationManager"><tx:attributes><tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" /></tx:attributes>
</tx:advice>

name:切点方法名称
isolation:事务的隔离级别
propogation:事务的传播行为
timeout:超时时间
read-only:是否只读

配置事务的aop织入

<!--    配置事务的aop织入--><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"/>新加的功能(事务功能)    👆                👆原来的转账功能</aop:config>

织入指的是把原来的功能和新加的功能结合到一起的过程
execution(* com.itheima.service.impl. * . * (.*.))
com前面实际上要写 public void 这种东西 但是 *的意思是全部都可以
这个里面是指定对应的类 其中 的意思是当前的所有 两个点..指的是当前包的下面的所有

用注解的方式配置声明式事务控制
编写 AccoutDao

@Repository("accountDao")
👆是这个在spring容器中的名字
public class AccountDaoImpl implements AccountDao {
@Autowired
👆是从spring容器中根据一样的类来配置
private JdbcTemplate jdbcTemplate;
public void out(String outMan, double money) {
jdbcTemplate.update("update account set money=money-? where 
name=?",money,outMan);
}
public void in(String inMan, double money) {
jdbcTemplate.update("update account set money=money+? where 
name=?",money,inMan);
}
}

编写 AccoutService

@Service("accountService")👆这个在spring容器中的名字
@Transactional👆 这是标记当前这个是事务
public class AccountServiceImpl implements AccountService {
@Autowired
👆是从spring容器中找到一样的类来注入到👇中
private AccountDao accountDao;
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = 
Propagation.REQUIRED)
👆配置通知的属性
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
int i = 1/0;
accountDao.in(inMan,money);
}
}

编写controller

 public static void main(String[] args) {ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-anno.xml");AccountService accountService = app.getBean(AccountService.class);accountService.transfer("tom","lucy",500);}

编写 applicationContext.xml 配置文件
主要的是下面这些

<!—之前省略datsSource、jdbcTemplate、平台事务管理器的配置-->
<!--组件扫描-->
<context:component-scan base-package="要扫描的地方"/>
<!--事务的注解驱动-->
<tx:annotation-driven/>

全部的是

<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://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 name="jdbcUrl" value="jdbc:mysql://localhost:3306/test1"/><property name="user" value="root"/><property name="password" value="123456"/></bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean><bean id="accountDao" class="com.tong.dao.impl.AccountDaoimpl"><property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean><bean id="accountService" class="com.tong.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"/></bean><bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean>
<tx:advice id="txadvice" transaction-manager="transcationManager"><tx:attributes><tx:method name="*"/></tx:attributes>
</tx:advice>
<aop:config><aop:advisor advice-ref="txadvice" pointcut="execution(* com.service.*.*(..))"/>
</aop:config></beans>