Spring声明式事务处理理解

来源:互联网 发布:2015抽样人口普查数据 编辑:程序博客网 时间:2024/05/10 12:32


1、事物有四个特性:原子性、一致性、隔离性、持久性。

2、对于一次操作要么全部执行成功,要么全部不成功,这就是原子性,这也是很重要的一个特性,对于一次转账操作,对于中间的任何一个环节不成功,那么整个转账过程都是不成功的,spring对于事物管理提供了很好的支持。用一个简单的例子来说明。

所需要的jar基础包:




i、使用一个类模拟一次简单的银行转账

package com.akwolf.spring.jdbc;import org.springframework.jdbc.core.JdbcTemplate;public class SpringTransactionImpl {// Spring 对JDBC的操作也进行了封装,具体的类就是JdbcTemplateprivate JdbcTemplate jdbcTemplate;// 留作注入jdbcTemplatepublic void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}public JdbcTemplate getJdbcTemplate() {return jdbcTemplate;}/** * 模拟一下转账业务 */public void updateBank() {String sql = "update bank b set b.balance = b.balance - 100 where b.id = 1";jdbcTemplate.update(sql);//try {//throw new SQLException("模拟异常!") ;//} catch (SQLException e) {//e.printStackTrace();//}sql = "update bank b set b.balance = b.balance + 100 where b.id = 2";jdbcTemplate.update(sql);}}


ii、对于beans.xml的配置,注意要要引入相应的schema头文件


<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-2.5.xsd           http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 配置数据源 --><beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><property name="url"><value>jdbc:mysql://192.168.17.4:3306/mytest</value></property><property name="username"><value>zhangh</value></property><property name="password"><value>123456</value></property></bean><!-- 托管JdbcTemplate --><beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg ref="dataSource" /></bean><!-- 托管模拟操作类SpringTransactionImpl --><beanid="jdbcImpl"class="com.akwolf.spring.jdbc.SpringTransactionImpl"><propertyname="jdbcTemplate"ref="jdbcTemplate" /></bean><!-- 声明式异常处理 --><!-- 1、定义事物管理器 --><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><!-- 2、需要进行管理的目标 --><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="*"read-only="false" /></tx:attributes></tx:advice><!-- 3、使用aop切入 --><aop:config><aop:pointcutexpression="execution(* com.akwolf.spring.jdbc.*.*(..))"id="managerMethod" /><aop:advisoradvice-ref="txAdvice"pointcut-ref="managerMethod" /></aop:config></beans>




iii、测试运行

package com.akwolf.spring.jdbc;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringJdbcTest {private static ApplicationContext context ;@BeforeClasspublic static void init(){context = new ClassPathXmlApplicationContext("beans.xml") ;}@Testpublic void testJdbc() {SpringTransactionImpl impl = (SpringTransactionImpl) context.getBean("jdbcImpl") ;impl.updateBank() ;//System.out.println(context.getBean("jdbcTemplate"));}}

这个例子很简单,自己随手记一下,注释写的还算详细,应该可以看明白,其他的就不啰嗦了!!


原创粉丝点击