事务管理
数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行。 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源。通过将一组相关操作组合为一个要么全部成功要么全部失败的单元,可以简化错误恢复并使应用程序更加可靠。一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性、一致性、隔离性和持久性)属性。事务是数据库运行中的逻辑工作单位,由DBMS中的事务管理子系统负责事务的处理。
案例准备
Dao
public class UserDao {
  
  JdbcTemplate jdbcTemplate;
  /**
   * 添加数据
   */
  public void add(){
    int i = jdbcTemplate.update("insert into t_sysuser(id,uname,nickname)values(seq_t_sysuser.nextval,?,?)","aaa","测试");
    System.out.println("添加数据.."+i);
  }
  /**
   * 修改数据
   */
  public void udpate(){
    int i = jdbcTemplate.update("update t_sysuser set uname=? ,nickname=? where id=?","bbb","呵呵",133);
    System.out.println("修改数据.."+i);
  }
}Service层
public class UserService {
  
  private UserDao dao;
  
  public void fun(){
    dao.add();
    dao.udpate();
  }
}配置文件配置
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  <!-- 开启扫描 -->
  <context:component-scan base-package="com.dpb.*"></context:component-scan>
  
  <!-- 配置数据源 -->
  <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="username" value="pms"/>
    <property name="password" value="pms"/>
  </bean>
  
  <!-- 配置JdbcTemplate -->
  <bean class="org.springframework.jdbc.core.JdbcTemplate" >
    <constructor-arg name="dataSource" ref="dataSource"/>
  </bean>
</beans>测试
两个数据库操作方法都没问题的情况下都可以

当第二个方法出错的情况下


事务的使用
xml配置声明式事务
Spring事务的传播行为Spring事务的隔离级别
配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  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-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
  <!-- 开启扫描 -->
  <context:component-scan base-package="com.dpb.*"></context:component-scan>
  
  <!-- 配置数据源 -->
  <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="username" value="pms"/>
    <property name="password" value="pms"/>
  </bean>
  
  <!-- 配置JdbcTemplate -->
  <bean class="org.springframework.jdbc.core.JdbcTemplate" >
    <constructor-arg name="dataSource" ref="dataSource"/>
  </bean>
  
  <!-- 
  Spring中,使用XML配置事务三大步骤:    
    1. 创建事务管理器    
    2. 配置事务方法    
    3. 配置AOP
   -->
   <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
    <property name="dataSource" ref="dataSource"/>
   </bean>
   <tx:advice id="advice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="fun*" propagation="REQUIRED"/>
    </tx:attributes>
   </tx:advice>
   <!-- aop配置 -->
   <aop:config>
     <aop:pointcut expression="execution(* *..service.*.*(..))" id="tx"/>
     <aop:advisor advice-ref="advice" pointcut-ref="tx"/>
   </aop:config>
</beans>测试


注解的方式使用


测试


                
                










