版权声明:原创作品,谢绝转载!否则将追究法律责任。
Spring 3.1 事务配置
spring发的包最好用的是2.5.6;所依赖的包全部都有,可是后来的版本都缺这少那的,导致开发中遇到各种各样的问题。
下面主要讲述如何给你的spring应用加上事务:
1、准备依赖的包
org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.aspects-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.context.support-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.orm-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar
org.springframework.web.struts-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.aspects-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.context.support-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.orm-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar
org.springframework.web.struts-3.1.0.RELEASE.jar
-- spring 2.5.6中获取
aopalliance.jar
aspectjweaver.jar
-- MySQL驱动
mysql-connector-java-5.1.17-bin.jar
-- 必须的包
commons-dbcp-1.4.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
commons-pool-1.6.jar
log4j-1.2.16.jar
aopalliance.jar
aspectjweaver.jar
-- MySQL驱动
mysql-connector-java-5.1.17-bin.jar
-- 必须的包
commons-dbcp-1.4.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
commons-pool-1.6.jar
log4j-1.2.16.jar
2、配置
引入xml头文件
<?
xml
version
="1.0"
encoding
="GBK"
?>
< beans xmlns ="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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >
< beans xmlns ="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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >
引入配置文件
<!--
不加密时候使用 -->
<!-- <context:property-placeholder location="classpath:jdbc.properties,classpath:tdmc.properties"/>-->
<!-- 加密时候使用 -->
< bean id ="propertyConfig" class ="com.lavasoft.freamwork.ext.spring.PropertyPlaceholderConfigurerExt" >
< property name ="locations" >
< list >
< value >classpath:jdbc.properties </ value >
< value >classpath:tdmc.properties </ value >
</ list >
</ property >
</ bean >
<!-- <context:property-placeholder location="classpath:jdbc.properties,classpath:tdmc.properties"/>-->
<!-- 加密时候使用 -->
< bean id ="propertyConfig" class ="com.lavasoft.freamwork.ext.spring.PropertyPlaceholderConfigurerExt" >
< property name ="locations" >
< list >
< value >classpath:jdbc.properties </ value >
< value >classpath:tdmc.properties </ value >
</ list >
</ property >
</ bean >
配置事务:
<
bean
id
="txManager"
class
="org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
< property name ="dataSource" ref ="dataSource" />
</ bean >
< tx:advice id ="txAdvice" transaction-manager ="txManager" >
< tx:attributes >
< tx:method name ="get*" read-only ="true" />
< tx:method name ="set*" read-only ="true" />
< tx:method name ="query*" read-only ="true" />
< tx:method name ="find*" read-only ="true" />
< tx:method name ="load*" read-only ="true" />
< tx:method name ="count*" read-only ="true" />
< tx:method name ="save*" rollback-for ="Exception" />
< tx:method name ="update*" rollback-for ="Exception" />
< tx:method name ="delete*" rollback-for ="Exception" />
< tx:method name ="merage*" rollback-for ="Exception" />
</ tx:attributes >
</ tx:advice >
< aop:config >
< aop:pointcut id ="serviceOperation"
expression ="execution(* com.asiainfo.tdmc.service.*SV.*(..))" />
< aop:advisor advice-ref ="txAdvice"
pointcut-ref ="serviceOperation" />
</ aop:config >
< property name ="dataSource" ref ="dataSource" />
</ bean >
< tx:advice id ="txAdvice" transaction-manager ="txManager" >
< tx:attributes >
< tx:method name ="get*" read-only ="true" />
< tx:method name ="set*" read-only ="true" />
< tx:method name ="query*" read-only ="true" />
< tx:method name ="find*" read-only ="true" />
< tx:method name ="load*" read-only ="true" />
< tx:method name ="count*" read-only ="true" />
< tx:method name ="save*" rollback-for ="Exception" />
< tx:method name ="update*" rollback-for ="Exception" />
< tx:method name ="delete*" rollback-for ="Exception" />
< tx:method name ="merage*" rollback-for ="Exception" />
</ tx:attributes >
</ tx:advice >
< aop:config >
< aop:pointcut id ="serviceOperation"
expression ="execution(* com.asiainfo.tdmc.service.*SV.*(..))" />
< aop:advisor advice-ref ="txAdvice"
pointcut-ref ="serviceOperation" />
</ aop:config >
3、测试
设置MySQL的表为InnoDB类型,在保存中抛出异常,和不抛出异常做比较,看看能否保存,即可知道事务是否起作用。
public Test_dept saveTest_dept(Test_dept test_dept) {
test_dept = test_deptDAO.insert(test_dept);
if( true) throw new RuntimeException();
return test_dept;
}
test_dept = test_deptDAO.insert(test_dept);
if( true) throw new RuntimeException();
return test_dept;
}
发现数据库没有保存上。
去掉事务配置的xml片段,则保存进去了。
http://lavasoft.blog.51cto.com/62575/807502
本文出自 “
熔 岩” 博客,谢绝转载!
本文转自yunlielai51CTO博客,原文链接:http://blog.51cto.com/4925054/1177162,如需转载请自行联系原作者