spring 08 声明式事务管理

概念

  • 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比如有一条SQL语句没有执行成功,那么这一组操作都将全部回滚

***事务特性(ACID) ***

  • Atomic(原子性):要么都成功,要么都失败
  • Consistent(一致性):数据应该不被破坏
  • Isolate(隔离性):用户间操作不相混淆
  • Durable(持久性):永久保存

程序中两种事务管理方式
1. 编程式事务管理

  • 编写程序式的事务管理可以清楚的定义事务的边界,可以实现细粒度的事务控制,比如你可以通过程序代码来控制你的事务何时开始,何时结束等,与后面介绍的声明式事务管理相比,它可以实现细粒度的事务控制,例如jdbc,hibernate,spring中不提倡使用。

  • JDBC事务控制:
    con.setAutoCommite (false); 设置事务手动提交

  • Hibernate中事务控制:
    session.beginTransaction(); 开启事务

  • 优缺点:
    1. 事务控制精确
    2. 事务代码,与业务逻辑处理代码,耦合在一起!
    事务代码,不能共用! 重新写事务控制操作!
    开发效率低,不便于维护! (不想用事务,要改代码!)

2. 声明式事务管理 (在Spring中使用)

  • 如果你并不需要细粒度的事务控制,你可以使用声明式事务,在Spring中,你只需要在Spring配置文件中做一些配置,即可将操作纳入到事务管理中,解除了和代码的耦合, 这是对应用代码影响最小的选择,从这一点再次验证了Spring关于AOP的概念。当你不需要事务管理的时候,可以直接从Spring配置文件中移除该设置

  • 特点:

    1. Spring提供的声明式事务管理,用到Aop概念!
    2. 对指定的方法添加事务控制,这里只需要配置即可!
    3. 修改事务控制实现或删除事务控制操作,只需要移除xml事务相关配置!
  • 注意:
    只能对某个方法应用事务! (因为“切入点表达式”拦截的是方法,控制不了方法内部代码!)
    所以,Spring声明式事务管理,即为粗粒度的事务控制!

声明式事务管理器类:
Jdbc:

DataSourceTransactionManager 管理jdbc中事务控制

Hibernate:

HibenateTransactionManager 管理hibernate中事务控制

声明式事务管理-jdbc

导入aop相关包(因为用到切面技术)
需要新导入的jar包

spring-tx.jar(tx命名空间的支持)

引入tx命名空间

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"

1. xml配置方式

applicationContext.xml(是以前的bean.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:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 加载Proerties配置文件 -->
    <context:property-placeholder location="classpath:com/xxjqr/spring01/transaction/db.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>

    <context:component-scan base-package="com.xxjqr.spring01.transaction"></context:component-scan>
    
    
    <!-- spring声明事务配置 -->
    <!-- 配置事务管理类 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!--  事务通知配置, 拦截到指定的方法后如何管理事务 -->
    <!-- find*  find开头的方法,是只读的事务 -->
    <!--   *    上面所有的方法都不满足时候,采用的事务控制规则 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    
    <!--  事务Aop配置 = 切入点表达式  + 应用上面的事务通知 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.xxjqr.spring01.transaction.*Service.*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
</beans>

@Repository
@Data
public class UserDao {
    @Resource
    private JdbcTemplate jdbcTemplate;
    
    public void save(){
        //jdbcTemplate.update("insert into user_t(name) values(?)","test");
        jdbcTemplate.update("insert into user_t(name)values('test..')");
    }
}
@Data
@Service
public class UserService {
    @Resource
    private UserDao userDao;

    public void save(){
        userDao.save();
        int i=1/0;
        userDao.save();
    }
}
public class App {

    private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml",App.class);

    @Test
    public void testApp() throws Exception {
       UserService userService = (UserService)ac.getBean("userService");
       userService.save();
    }
}
2. 注解配置方式
  • 开启声明事务注解

<tx:annotation-driven transaction-manager="txManager"/>

  • 使用@Transactional 注解
    在需要添加事务控制的方法上写这个注解

  • @Transactional
    写到方法上, 表示当前方法应用事务控制
    写到类上, 表示当前类的所有方法都会应用事务
    写到父类上, 当执行父类的这个方法时候才应用事务!

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:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 加载Proerties配置文件 -->
    <context:property-placeholder location="classpath:com/xxjqr/spring01/transaction/db.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>

    <context:component-scan base-package="com.xxjqr.spring01.transaction"></context:component-scan>
    
    
    <!-- spring声明事务配置 -->
    <!-- 配置事务管理类 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="txManager"/>
    
</beans>

类(只有UesrService发生了变化)

@Data
@Service
public class UserService {
    @Resource
    private UserDao userDao;
    
    @Transactional
    public void save(){
        userDao.save();
        int i=1/0;
        userDao.save();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容