Spring声明式和xml事务

如果基于注解的和xml的事务都配置了会以注解的优先。基于注解的控制粒度更细,使用方便;基于xml的一劳永逸,只需要写一次xml,以后就不用写了。

什么是事务

把一组业务当成一个业务来做;要么都成功,要么都失败,保证业务操作完整性的一种数据库机制。

Spring声明式事务需要导入下面三个依赖:

 <!--spring事务,加了这个,剩余两个自动加入-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.2.6.RELEASE</version>
            <scope>compile</scope>
        </dependency>

ACID 四大特性

A 原子性:原子性指的是 在一组业务操作下 要么都成功 要么都失败

在一组增删改查的业务下 要么都提交 要么都回滚

C 一致性:事务前后的数据要保证数据的一致性

在一组的查询业务下 必须要保证前后关联数据的一致性

I 隔离性:在并发情况下 事物之间要相互隔离。

D 持久性:数据一旦保存就是持久性的。

总结:在事务控制方面,主要有两个分类:

编程式事务:在代码中直接加入处理事务的逻辑,可能需要在代码中显式调用beginTransaction()、commit()、rollback()等事务管理相关的方法

connetion.autoCommit(false);

------

----

---

connction.commint()

catch(){

connction.rollback();

}

声明式事务:在方法的外部添加注解或者直接在配置文件中定义,将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。spring的AOP恰好可以完成此功能:事务管理代码的固定模式作为一种横切关注点,通过AOP方法模块化,进而实现声明式事务。

声明式事务的简单配置

Spring从不同的事务管理API中抽象出了一整套事务管理机制,让事务管理代码从特定的事务技术中独立出来。开发人员通过配置的方式进行事务管理,而不必了解其底层是如何实现的。

Spring的核心事务管理抽象是PlatformTransactionManager。它为事务管理封装了一组独立于技术的方法。无论使用Spring的哪种事务管理策略(编程式或声明式),事务管理器都是必须的。

事务管理器可以以普通的bean的形式声明在Spring IOC容器中。下图是spring提供的事务管理器

1、在配置文件中添加事务管理器

jdbcTemplate.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
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       https://www.springframework.org/schema/tx/spring-tx.xsd
">
    <context:component-scan base-package="cn.chunfeng"></context:component-scan>
    <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>
    <!--事务控制-->
    <!--配置事务管理器的bean-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--开启基于注解的事务控制模式,依赖tx名称空间-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

@Transactional注解应该写在哪:

@Transactional 可以标记在类上面(当前类所有的方法都运用上了事务)

@Transactional 标记在方法则只是当前方法运用事务

也可以类和方法上面同时都存在, 如果类和方法都存在@Transactional会以方法的为准。如果方法上面没有@Transactional会以类上面的为准

建议:@Transactional写在方法上面,控制粒度更细, 建议@Transactional写在业务逻辑层上,因为只有业务逻辑层才会有嵌套调用的情况。

基于xml的事务配置

jdbcTemplate.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
      https://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx
      https://www.springframework.org/schema/tx/spring-tx.xsd
">
   <context:component-scan base-package="cn.chunfeng"></context:component-scan>
   <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
       <property name="username" value="${jdbc.username}"></property>
       <property name="password" value="${jdbc.password}"></property>
       <property name="url" value="${jdbc.url}"></property>
       <property name="driverClassName" value="${jdbc.driverClassName}"></property>
   </bean>
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
   </bean>
   <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
       <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
   </bean>
   <!--事务控制-->
   <!--配置事务管理器的bean-->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>
   </bean>
   <!--
   基于xml配置的事务:依赖tx名称空间和aop名称空间
       1、spring中提供事务管理器(切面),配置这个事务管理器
       2、配置出事务方法
       3、告诉spring哪些方法是事务方法(事务切面按照我们的切入点表达式去切入事务方法)
   -->
   <bean id="bookService" class="cn.chunfeng.service.BookService"></bean>
   <aop:config>
       <aop:pointcut id="txPoint" expression="execution(* cn.chunfeng.service.*.*(..))"/>
       <!--事务建议:advice-ref:指向事务管理器的配置-->
       <aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"></aop:advisor>
   </aop:config>
   <tx:advice id="myAdvice" transaction-manager="transactionManager">
       <!--事务属性-->
       <tx:attributes>
           <!--指明哪些方法是事务方法-->
           <tx:method name="*"/>
           <tx:method name="checkout" propagation="REQUIRED"/>
           <tx:method name="get*" read-only="true"></tx:method>
       </tx:attributes>
   </tx:advice>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值