vjame

          優(yōu)化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數(shù)據(jù)加載中……

          采用Spring聲明式事務(wù)


          1、聲明式事務(wù)配置
           * 配置SessionFactory
           * 配置事務(wù)管理器
           * 事務(wù)的傳播特性
           * 那些類那些方法使用事務(wù)

          2、編寫業(yè)務(wù)邏輯方法
           * 繼承HibernateDaoSupport類,使用HibernateTemplate來持久化,HibernateTemplate是
             Hibernate Session的輕量級封裝
           * 默認情況下運行期異常才會回滾(包括繼承了RuntimeException子類),普通異常是不會滾的
           * 編寫業(yè)務(wù)邏輯方法時,最好將異常一直向上拋出,在表示層(struts)處理
           * 關(guān)于事務(wù)邊界的設(shè)置,通常設(shè)置到業(yè)務(wù)層,不要添加到Dao上 

          3、了解事務(wù)的幾種傳播特性
           1. PROPAGATION_REQUIRED: 如果存在一個事務(wù),則支持當前事務(wù)。如果沒有事務(wù)則開啟
           2. PROPAGATION_SUPPORTS: 如果存在一個事務(wù),支持當前事務(wù)。如果沒有事務(wù),則非事務(wù)的執(zhí)行
           3. PROPAGATION_MANDATORY: 如果已經(jīng)存在一個事務(wù),支持當前事務(wù)。如果沒有一個活動的事務(wù),則拋出異常。
           4. PROPAGATION_REQUIRES_NEW: 總是開啟一個新的事務(wù)。如果一個事務(wù)已經(jīng)存在,則將這個存在的事務(wù)掛起。
           5. PROPAGATION_NOT_SUPPORTED: 總是非事務(wù)地執(zhí)行,并掛起任何存在的事務(wù)。
           6. PROPAGATION_NEVER: 總是非事務(wù)地執(zhí)行,如果存在一個活動事務(wù),則拋出異常
           7. PROPAGATION_NESTED:如果一個活動的事務(wù)存在,則運行在一個嵌套的事務(wù)中. 如果沒有活動事務(wù),
                則按TransactionDefinition.PROPAGATION_REQUIRED 屬性執(zhí)行

          4、Spring事務(wù)的隔離級別
           1. ISOLATION_DEFAULT: 這是一個PlatfromTransactionManager默認的隔離級別,使用數(shù)據(jù)庫默認的事務(wù)隔離級別.
                另外四個與JDBC的隔離級別相對應(yīng)
           2. ISOLATION_READ_UNCOMMITTED: 這是事務(wù)最低的隔離級別,它充許令外一個事務(wù)可以看到這個事務(wù)未提交的數(shù)據(jù)。
                這種隔離級別會產(chǎn)生臟讀,不可重復(fù)讀和幻像讀。
           3. ISOLATION_READ_COMMITTED: 保證一個事務(wù)修改的數(shù)據(jù)提交后才能被另外一個事務(wù)讀取。另外一個事務(wù)不能讀取該事務(wù)未提交的數(shù)據(jù)
           4. ISOLATION_REPEATABLE_READ: 這種事務(wù)隔離級別可以防止臟讀,不可重復(fù)讀。但是可能出現(xiàn)幻像讀。
                它除了保證一個事務(wù)不能讀取另一個事務(wù)未提交的數(shù)據(jù)外,還保證了避免下面的情況產(chǎn)生(不可重復(fù)讀)。
           5. ISOLATION_SERIALIZABLE 這是花費最高代價但是最可靠的事務(wù)隔離級別。事務(wù)被處理為順序執(zhí)行。
                除了防止臟讀,不可重復(fù)讀外,還避免了幻像讀。  

          聲明式事務(wù)配置

          <?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
          >
              
          <!-- 配置sessionFactory -->
              
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                  
          <property name="configLocation">
                      
          <value>classpath:hibernate.cfg.xml</value>
                  
          </property>    
              
          </bean>           
              
              
          <!-- 配置事務(wù)管理器 -->
              
          <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                  
          <property name="sessionFactory">
                      
          <ref bean="sessionFactory"/>
                  
          </property>    
              
          </bean>
              
              
          <!-- 配置事務(wù)的傳播特性 -->
              
          <tx:advice id="txAdvice" transaction-manager="transactionManager">
                  
          <tx:attributes>
                      
          <tx:method name="add*" propagation="REQUIRED"/>
                      
          <tx:method name="del*" propagation="REQUIRED"/>
                      
          <tx:method name="modify*" propagation="REQUIRED"/>
                      
          <tx:method name="*" read-only="true"/>
                  
          </tx:attributes>
              
          </tx:advice>
              
              
          <!-- 那些類的哪些方法參與事務(wù)   所有類所有方法-->
              
          <aop:config>
                  
          <aop:pointcut id="allManagerMethod" expression="execution(* com.strongit.usermgr.manager.*.*(..))"/>
                  
          <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
              
          </aop:config>
              
              
          </beans>


          在IoC容器中實例化對象

          <?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
          >
              
              
          <bean id="userManager" class="com.strongit.usermgr.manager.impl.UserManagerImpl">
                  
          <property name="sessionFactory" ref="sessionFactory"/>
                  
          <property name="logManager" ref="logManager"/>
              
          </bean>
              
              
          <bean id="logManager" class="com.strongit.usermgr.manager.impl.LogManagerImpl">
                  
          <property name="sessionFactory" ref="sessionFactory"/>
              
          </bean>
          </beans>



          用戶實現(xiàn)類需繼承HibernateDaoSupport類,調(diào)用里面的hibernate模板中的方法

          package com.strongit.usermgr.manager.impl;

          import java.util.Date;


          import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

          import com.strongit.usermgr.manager.LogManager;
          import com.strongit.usermgr.manager.UserManager;
          import com.strongit.usermgr.model.Log;
          import com.strongit.usermgr.model.User;


          public class UserManagerImpl extends HibernateDaoSupport implements UserManager {

              
          private LogManager logManager ;
              
              
          public void setLogManager(LogManager logManager) {
                  
          this.logManager = logManager;
              }

              
          public void addUser(User user) {
                      
          this.getHibernateTemplate().save(user);

                      Log log 
          = new Log();
                      log.setType(
          "安全日志");
                      log.setDetail(
          "xxx進入系統(tǒng)");
                      log.setTime(
          new Date());
                      Integer.parseInt(
          "sdfdsf");

                      logManager.addLog(log);

              }

          }

          日志實現(xiàn)類
          package com.strongit.usermgr.manager.impl;

          import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

          import com.strongit.usermgr.manager.LogManager;
          import com.strongit.usermgr.model.Log;

          public class LogManagerImpl extends HibernateDaoSupport implements LogManager {

              
          public void addLog(Log log) {
                  
          this.getHibernateTemplate().save(log);
              }

          }

          測試類,這里涉及到兩個spring配置文件,使用匹配查找
          BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
          package com.strongit.usermgr.client;

          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          import com.strongit.usermgr.manager.UserManager;
          import com.strongit.usermgr.model.User;

          public class Client {

              
          /**  
               *   @Description 方法實現(xiàn)功能描述  
               *   
          @param args
               *   
          @author lanjh 9:02:58 PM
               *   
          @return void
               *   
          @throws  拋出異常說明
               
          */
              
          public static void main(String[] args) {

                  
                  User user 
          = new User();
                  user.setName(
          "chenj");
                  
                  BeanFactory factory 
          = new ClassPathXmlApplicationContext("applicationContext-*.xml");
                  UserManager u  
          = (UserManager)factory.getBean("userManager");
                  u.addUser(user);

              }

          }

          posted on 2009-08-09 14:57 lanjh 閱讀(340) 評論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 舞钢市| 九龙坡区| 清原| 灵台县| 武汉市| 徐水县| 林口县| 璧山县| 鹿邑县| 涡阳县| 古田县| 孙吴县| 逊克县| 泽普县| 万州区| 太湖县| 阿勒泰市| 宁国市| 台中县| 潜山县| 平果县| 保定市| 梅河口市| 丹寨县| 恭城| 巴林右旗| 衡南县| 钟山县| 六枝特区| 平山县| 彰武县| 扶沟县| 九江市| 株洲县| 文成县| 林甸县| 九龙城区| 仲巴县| 云阳县| 浙江省| 陆河县|