Spring配置事务,关系,总结

来源:互联网 发布:在淘宝上怎么搜索店铺 编辑:程序博客网 时间:2024/05/16 08:02

原文转自

http://blog.csdn.net/u012815721/article/details/40184219?utm_source=tuicool&utm_medium=referral

最常用的方法为使用

声明式事务管理 (使用注解配置 )
对需要管理事务的方法,添加注解@Transactionnal
* @Transactionnal 可以加载类上面 或者 方法名上面

在applicationContext.xml中添加

@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,readOnly=false,rollbackFor=ArithmeticException.class)
propagation=Propagation.REQUIRED 传播行为
isolation=Isolation.DEFAULT 隔离级别
readOnly=false 是否只读
rollbackFor=ArithmeticException.class 发生异常回滚
noRollbackFor=xxx.class 发生异常 仍然提交事务
applicationContext4.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">      <context:property-placeholder location="classpath:jdbc.properties"/>      <!-- 配置数据源 -->      <!-- c3p0数据源  -->      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">          <property name="driverClass" value="${jdbc.driverClass}"></property>          <property name="jdbcUrl" value="${jdbc.url}"></property>          <property name="user" value="${jdbc.user}"></property>          <property name="password" value="${jdbc.password}"></property>      </bean>                 <!-- 配置JdbcTemplate , 将数据源注入到jdbcTemplate -->      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">          <property name="dataSource" ref="dataSource"></property>      </bean>      <!-- 将jdbcTemplate 注入DAO -->      <bean id="accountDAO" class="dao.AccountDAOImpl">          <property name="jdbcTemplate" ref="jdbcTemplate"></property>      </bean>      <!-- 将DAO注入Service -->      <bean id="accountService" class="service.AccountServiceImpl2">          <property name="accountDAO" ref="accountDAO"></property>      </bean>      <!-- 事务管理器 -->      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">          <property name="dataSource" ref="dataSource"></property>      </bean>      <!-- 注解驱动事务管理 -->      <tx:annotation-driven transaction-manager="transactionManager"/>  </beans>  

创建测试类

package test;  import org.junit.Test;  import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;  import service.IAccountService;  public class AccountTest {      @Test      // 测试用例   声明式事务管理 (使用注解配置 )      public void demo4(){          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext4.xml");          IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");          accountService.transfer("aaa", "bbb", 200);      }      @Test      // 测试用例 声明式事务管理 (使用XML配置声明式事务 基于tx/aop)      public void demo3(){          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml");          IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");          accountService.transfer("aaa", "bbb", 200);      }      @Test      // 测试用例  声明式事务管理 (原始方式)      public void demo2(){          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");          // 原始方式进行声明事务管理,必须获得代理对象          IAccountService accountService = (IAccountService) applicationContext.getBean("accountServiceProxy");          accountService.transfer("aaa", "bbb", 200);      }      @Test      // 测试用例  编程式      public void demo1(){          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");          IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");          accountService.transfer("aaa", "bbb", 200);      }  }  
0 0