Java蜘蛛人 歡迎大家

          歡迎大家 來到我的blog , 如果我身邊的朋友 有什么不懂可以直接來問我 我會細心的幫助你的. 如果網絡上的朋友有什么不懂的 可以加我Java蜘蛛人 QQ48187537
          posts - 54, comments - 192, trackbacks - 0, articles - 1

           

          關鍵字: spring 事務
          (1)配置:
              Spring的事務管理是通過AOP代理實現的,其中的事務通知由元數據驅動。代理對象與事務元數據結合產生一個AOP代理,它使用一個PlatformTransactionManager實現,配合TransactionInterceptor,在方法調用前后實施事務。
          Java代碼
          1. <?xml version="1.0" encoding="UTF-8"?>   
          2.   
          3. <beans xmlns="http://www.springframework.org/schema/beans"    
          4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
          5.     xmlns:context="http://www.springframework.org/schema/context"    
          6.     xmlns:aop="http://www.springframework.org/schema/aop"    
          7.     xmlns:tx="http://www.springframework.org/schema/tx"    
          8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
          9.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd    
          10.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd    
          11.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">   
          12.   
          13.     <description>springApp</description>       
          14.     <!-- dataSource for MySQL -->   
          15.     <bean id="dataSource"  
          16.         class="org.apache.commons.dbcp.BasicDataSource"  
          17.         destroy-method="close">   
          18.         <property name="driverClassName"  
          19.             value="com.mysql.jdbc.Driver" />   
          20.         <property name="url"  
          21.             value="jdbc:mysql://localhost:3306/springapp" />   
          22.         <property name="username" value="root" />   
          23.         <property name="password" value="****" />   
          24.     </bean>      
          25.   
          26.     <!-- Hibernate SessionFactory for MySQL -->   
          27.     <bean id="sessionFactory"  
          28.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
          29.         <property name="dataSource" ref="dataSource" />   
          30.   
          31.         <property name="mappingDirectoryLocations">   
          32.             <list>   
          33.                 <value>classpath:/</value>   
          34.             </list>   
          35.         </property>   
          36.         <property name="hibernateProperties">   
          37.             <props>   
          38.                 <prop key="hibernate.dialect">   
          39.                     org.hibernate.dialect.MySQLDialect   
          40.                 </prop>   
          41.                 <prop key="hibernate.show_sql">true</prop>   
          42.                 <prop key="hibernate.jdbc.fetch_size">50</prop>   
          43.                 <prop key="hibernate.jdbc.batch_size">100</prop>   
          44.             </props>   
          45.         </property>   
          46.   
          47.     </bean>   
          48.   
          49.   
          50.     <!--Transaction -->    
          51.     <bean id="txManager"    
          52.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">    
          53.             <property name="sessionFactory" ref="sessionFactory" />    
          54.     </bean>    
          55.   
          56.     <aop:config> <!--use CGLIB:proxy-target-class="true-->   
          57.         <aop:pointcut id="serviceOperator" expression="execution(* com.logcd.business.service.*.*(..))"/>   
          58.         <aop:advisor  advice-ref="txAdvice" pointcut-ref="serviceOperator"/>    
          59.         <!--   
          60.         <aop:advisor pointcut="execution(* com.logcd.business.service..*Service.*(..))" advice-ref="txAdvice"/>          
          61.         -->   
          62.     </aop:config>    
          63.        
          64.     <tx:advice id="txAdvice" transaction-manager="txManager">    
          65.         <tx:attributes>    
          66.             <tx:method name="find*" read-only="true" />    
          67.             <tx:method name="load*" read-only="true" />    
          68.             <tx:method name="is*" read-only="true"/>    
          69.             <tx:method name="save*"    
          70.                 rollback-for="Exception"/>    
          71.             <tx:method name="insert*"    
          72.                 rollback-for="Exception" />    
          73.             <tx:method name="remove*"  
          74.                 rollback-for="Exception"/>    
          75.             <tx:method name="add*"  
          76.                 no-rollback-for="Exception" />    
          77.         </tx:attributes>    
          78.     </tx:advice>    
          79.     <!--Transaction -->    
          80.        
          81.     <!-- DAO -->   
          82.     <bean id="genericDao" lazy-init="true" abstract="true"  
          83.         class="com.logcd.bo.dao.impl.GenericDaoImpl">   
          84.         <property name="sessionFactory">   
          85.             <ref local="sessionFactory" />   
          86.         </property>   
          87.     </bean>   
          88.   
          89.     <bean id="customersDao" parent="genericDao"  
          90.         class="com.logcd.bo.dao.impl.CustomersDaoImpl" />   
          91.   
          92.     <bean id="customerDao" parent="genericDao"  
          93.         class="com.logcd.bo.dao.impl.CustomerDaoImpl" />   
          94.   
          95.     <bean id="addressDao" parent="genericDao"  
          96.         class="com.logcd.bo.dao.impl.AddressDaoImpl" />   
          97.   
          98.     <bean id="customerManageService"  
          99.         class="com.logcd.business.service.impl.CustomerManageServiceImpl"  
          100.         autowire="byName"/>   
          101.            
          102. </beans>  


          (2)測試
          Java代碼
          1. package com.logcd.test;   
          2.   
          3. import org.springframework.context.ApplicationContext;   
          4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
          5.   
          6. import com.logcd.bo.Customers;   
          7. import com.logcd.business.service.CustomerManageService;   
          8.   
          9. import junit.framework.TestCase;   
          10.   
          11. public class SpringServiceTest extends TestCase {   
          12.   
          13.     private CustomerManageService customerManageService;    
          14.        
          15.     protected void setUp() throws Exception {   
          16.         super.setUp();   
          17.         ApplicationContext app = new ClassPathXmlApplicationContext("appContext.xml");   
          18.         customerManageService = (CustomerManageService) app.getBean("customerManageService");   
          19.     }   
          20.   
          21.     protected void tearDown() throws Exception {   
          22.         super.tearDown();   
          23.     }   
          24.   
          25.     public void testService() throws Exception{   
          26.         Customers cus = new Customers();   
          27.         cus.setName("testService");   
          28.         cus.setAge(29);   
          29.         customerManageService.saveCustomers(cus);   
          30.     }   
          31. }  


          附:pointcut里的語法
          execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)其中帶問號的modifiers-pattern?(public/protected) 和 declaring-type-pattern? throws-pattern? 可以不填

          如execution(* *..BookManager.save(..))

          第一顆* 代表ret-type-pattern 返回值可任意,
          *..BookManager 代表任意Pacakge里的BookManager類。
          如果寫成com.xyz.service.* 則代表com.xyz.service下的任意類
          com.xyz.service..* com.xyz.service則代表com.xyz.service及其子package下的任意類
          save代表save方法,也可以寫save* 代表saveBook()等方法
          (..) 匹配0個參數或者多個參數的,任意類型
          (x,..) 第一個參數的類型必須是X
          (x,,,s,..) 匹配至少4個參數,第一個參數必須是x類型,第二個和第三個參數可以任意,第四個必須是s類型。

          下面是我 JDBC 模板配置的 事務

          <?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: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.0.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
               
                
               
          <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                
          <property name="locations">
                  
          <list>
                     
          <value>WEB-INF/jdbc.properties</value>
                  
          </list> 
                
          </property> 
               
          </bean>  
            
          <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
             
          <property name="dataSource" >
              
          <ref local="dataSource2" />
             
          </property>
             
          <property name="lazyInit" value="true"></property>
            
          </bean>
             
          <!-- JdbcTemplate
                
          <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
                   
          <property name="dataSource"><ref bean="dataSource2"/></property>
                
          </bean>
            
          --> 
            
          <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
             
          <property name="driverClassName">
              
          <value>com.mysql.jdbc.Driver</value>
             
          </property>
             
          <property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
             
          <property name="username" value="root"></property>
             
          <property name="password" value="12345"></property>
            
          </bean>
               
               
          <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                 
          <property name="dataSource" ref="dataSource2"></property>     
               
          </bean>
                
               
          <!-- 這是JPA 的配置 -->
               
          <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"></bean>
               
                
          <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
                  
          <property name="dataSource" ref="dataSource"></property>
                  
          <property name="jpaVendorAdapter">
                     
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                            
          <property name="showSql" value="true" />
                      
          </bean>
                  
          </property>
                
          </bean>
               
               
          <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
                
          <property name="driverClass" value="${jdbc.driverClassName}"></property>
                
          <property name="jdbcUrl"    value="${jdbc.url}"></property>
                
          <property name="user">  
                  
          <value>${jdbc.username}</value>
                
          </property>
                
          <property name="password">
                  
          <value>${jdbc.password}</value>
                
          </property> 
                 
          <property name="minPoolSize" value="5"></property>
                 
          <property name="maxPoolSize" value="20"></property>
                 
          <property name="acquireIncrement" value="5"></property>
               
          </bean>
               
               
          <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
                 
          <property name="entityManagerFactory" ref="entityManagerFactory"></property> 
               
          </bean>
               
          <tx:annotation-driven transaction-manager="transactionManager" />
             
          <!-- end -->
             
             
            
          <!-- AOP 事務 -->   
               
          <aop:config>
                 
          <aop:pointcut id="serviceOperator" expression="execution(* com.zcq.*.*.update(..))" />
                 
          <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperator" />
               
          </aop:config>
               
               
          <tx:advice id="txAdvice" transaction-manager="TransactionManager">
                
          <tx:attributes>
                   
          <tx:method name="update" rollback-for="Exception" /> 
                
          </tx:attributes>
               
          </tx:advice>
             
          <!-- end -->
             
               
                 
          <bean id="personservice" class="com.zcq.serviceImp.PersonServiceImp"></bean>
               
          <bean id="loginAction" class="com.zcq.action.LoginAction" scope="prototype" >
                 
          <property name="service" ref="personservice"></property>
                 
          <property name="pp" ref="pp"></property>
               
          </bean>
               
               
          <bean id="pp" class="com.zcq.serviceImp.PersonJDBC">
                 
          <property name="jdbcTemplate" ref="jdbcTemplate"></property>
                 
          <!--  
                 
          <property name="transactionManager" ref="TransactionManager"></property>-->
               
          </bean>
           
               
          </beans>




              JDBC 單一個類配置事務

           

          <bean id="loginAction" class="com.zcq.action.LoginAction" scope="prototype" >
                 
          <property name="service" ref="personservice"></property>
                 
          <property name="pp" ref="userDaoProxy"></property>  <!-- 作為事物代理工廠bean 直接返回類型是下面PersonJDBC id是userDaoProxy -->
               
          </bean>
               
               
          <bean id="pp" class="com.zcq.serviceImp.PersonJDBC">
                 
          <property name="jdbcTemplate" ref="jdbcTemplate"></property>
                 
          <!-- 
                 配置JDBC 事物之一
                 
          <property name="transactionManager" ref="TransactionManager"></property>
                  
          -->
               
          </bean>
               
               
               
          <bean id="userDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                  
          <property name="transactionManager">
                      
          <ref bean="TransactionManager" />
                  
          </property>        
                  
          <property name="target">
                      
          <ref local="pp"/>
                  
          </property>
                  
          <property name="transactionAttributes">
                      
          <props>
                        
          <prop key="insert*">PROPAGATION_REQUIRED</prop>
                        
          <prop key="update*">PROPAGATION_REQUIRED</prop>
                        
          <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                      
          </props>        
                  
          </property>
               
          </bean>

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 库尔勒市| 西畴县| 湘乡市| 凤冈县| 乌什县| 镇赉县| 屯昌县| 永昌县| 江华| 皮山县| 静海县| 托克托县| 富川| 璧山县| 灵宝市| 辽源市| 容城县| 六盘水市| 颍上县| 隆林| 肥东县| 南汇区| 区。| 巴彦淖尔市| 南丹县| 墨玉县| 汤原县| 丰都县| 合作市| 公主岭市| 白银市| 阳曲县| 东丰县| 页游| 连云港市| 永修县| 长兴县| 班玛县| 西林县| 舞阳县| 建平县|