Spring 3.1 事務配置

Spring 3.1 事務配置
 
spring發的包最好用的是2.5.6;所依賴的包所有都有,但是後來的版本都缺這少那的,致使開發中遇到各類各樣的問題。
 
下面主要講述如何給你的spring應用加上事務:
 
一、準備依賴的包
 
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
 
-- 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
 
 
二、配置
 
引入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" >
 
引入配置文件
        <!-- 不加密時候使用 -->
        <!-- <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 >
 
 
三、測試
 
設置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;
        }
 
發現數據庫沒有保存上。
 
去掉事務配置的xml片斷,則保存進去了。
相關文章
相關標籤/搜索