隨筆-86  評(píng)論-33  文章-0  trackbacks-0
          web.xml
          載入Log4j配置
          <
          context-param><!--Log4j配置 在同一容器中部署多個(gè)應(yīng)用不能使用默認(rèn)的webAppRootKey,必須指定唯一KEY,以免沖突-->
              
          <param-name>webAppRootKey</param-name>
              
          <param-value>itservice.root</param-value>
              <!--在log4j.properties中設(shè)置日志路徑log4j.appender.file.File=${itservice.root}/WEB-INF/itservice.log-->

          </context-param>
          <context-param>
              
          <param-name>log4jConfigLocation</param-name>
              
          <param-value>/WEB-INF/classes/log4j.properties</param-value>
          </context-param>
          <listener>
              
          <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
          </listener>


            載入Spring配置文件
          <context-param>
              
          <param-name>contextConfigLocation</param-name>
              
          <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
              <!--可載入多個(gè)配置文件分隔符 , ;  \t \n -->

            
          </context-param>
            
          <listener>
              
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            
          </listener>

            字符編碼過(guò)濾器
          <filter>
              
          <filter-name>encodingFilter</filter-name>
              
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
              
          <init-param>
                
          <param-name>encoding</param-name>
                
          <param-value>UTF-8</param-value>
              
          </init-param>
            
          </filter>
           <filter-mapping>
              
          <filter-name>encodingFilter</filter-name>
              
          <url-pattern>/*</url-pattern>
            
          </filter-mapping>
            
          <filter-mapping>

          配置延遲加載時(shí)使用OpenSessionInView
          <
          filter>
              
          <filter-name>openSessionInViewFilter</filter-name>
              
          <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
              
          <init-param>
                
          <param-name>singleSession</param-name>
                
          <param-value>true</param-value>
              
          </init-param>
              
          <init-param>
                
          <param-name>sessionFactoryBeanName</param-name>
                 <!--指定對(duì)Spring配置中哪個(gè)sessionFactory使用OpenSessionInView-->

                
          <param-value>sessionFactory_itdb</param-value>
              
          </init-param>
            
          </filter>
              <filter-name>openSessionInViewFilter</filter-name>
              
          <url-pattern>/*</url-pattern>
            
          </filter-mapping>

          Struts-config.xml
          <action input="/index.jsp" name="loginActionForm" parameter="method" path="/loginAction" scope="session" type="org.springframework.web.struts.DelegatingActionProxy" validate="true" />
          <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
              
          <set-property property="contextConfigLocation" value="/WEB-INF/classes/action-servlet.xml" />
                 
          <!--將spring配置中關(guān)于ACTION的配置獨(dú)立到一個(gè)action-servlet.xml文件中-->
            
          </plug-in>

          ApplicationContext.xml
          <!--直接使用hibernate配置文件-->
           
          <bean id="sessionFactory_itdb" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
              
          <property name="configLocation">
                   <value>classpath:hibernate_itdb.cfg.xml</value>
              
          </property>
            </bean>

          <!--使用JNDI DataSource-->
          <bean id="it_dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName">
            
          <value>jdbc/itdb</value>
          </property>
          </bean>

          <!-- Spring配置DataSource -->
            <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              
          <property name="location">
                 
          <value>classpath:init.properties</value>
              
          </property>
            
          </bean>
           
          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
             
          <property name="driverClassName" value="${dataSource.driverClassName}"></property>
             
          <property name="url" value="${dataSource.url}"></property>
             
          <property name="username" value="${dataSource.username}"></property>
             
          <property name="password" value="${dataSource.password}"></property>
           
          </bean>
          <!-- *********************Hibernate*********************** -->
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           
          <property name="dataSource" ref="dataSource"></property>
            
          <property name="mappingResources">
            
          <list>
              
          <value>com/usish/shweb/hbm/ShwebFile.hbm.xml</value>
              
          <value>com/usish/shweb/hbm/ShwebLog.hbm.xml</value>
            
          </list>
           
          </property>
           
          <property name="hibernateProperties">
            
          <props>
               
          <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
               
          <prop key="hibernate.show_sql">false</prop>
               
          <prop key="hibernate.jdbc.fetch_size">50</prop>
               
          <prop key="hibernate.jdbc.batch_size">30</prop>
               
          <prop key="hibernate.cache.use_second_level_cache">true</prop>
               
          <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
               
          <prop key="hibernate.cache.use_query_cache">true</prop>
            
          </props>
           
          </property>
          </bean>

          <!--******************TransactionManager***********************-->
          <bean id="transactionManager"
              class
          ="org.springframework.orm.hibernate3.HibernateTransactionManager">
             
          <property name="sessionFactory">
                 
          <ref local="sessionFactory" />
             
          </property>
          </bean>
          <bean id="baseTxProxy"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
            
          <property name="transactionManager">
              
          <ref bean="transactionManager" />
            
          </property>
            
          <property name="transactionAttributes">
              
          <props>
               
          <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
               
          <prop key="get*">PROPAGATION_REQUIRED</prop>
               
          <prop key="save*">PROPAGATION_REQUIRED</prop>
               
          <prop key="insert*">PROPAGATION_REQUIRED</prop>
               
          <prop key="update*">PROPAGATION_REQUIRED</prop>
               
          <prop key="delete*">PROPAGATION_REQUIRED</prop>
              
          </props>
            
          </property>
          </bean>

          <!--AOP TX-->
          <tx:advice id="txAdvice" transaction-manager="txManager">
             <tx:attributes>
               <tx:method name="get*" propagation="NEVER"/>
               <tx:method name="find*" propagation="NEVER"/>
               <tx:method name="*" propagation="REQUIRED"/>
             </tx:attributes>
           </tx:advice>
           
           <aop:config>
              <aop:pointcut id="txBusinessMethods" expression="execution(* com.ztgame.blog.business.*BusinessImpl.*(..))"/>
              <aop:advisor advice-ref="txAdvice" pointcut-ref="txBusinessMethods"/>
           </aop:config>

          <!--annotation TX-->
          <tx:annotation-driven proxy-target-class="true" transaction-manager="txManager"/>
           
          Spring編程式事物
          <
          bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
                
          <property name="transactionManager">
                  
          <ref bean="TransactionManager"/>
                
          </property>
              
          </bean>
              
          <bean id="courseService" class="com.test.CourseService">
                
          <property name="transactionTemplate">
                  
          <ref bean="transactionTemplate"/>
                
          </property>
           
          </bean>


          private TransactionTemplate transactionTemplate;
              public void enrollStudent()...{
                  transactionTemplate.execute(new TransactionCallback()...{
                      public Object doInTransaction(TransactionStatus ts)...{
                          try ...{
          //                    需要事務(wù)控制的方法代碼
                          } catch (Exception e) ...{
                              ts.setRollbackOnly();  //回滾
                          }
                          return null;  //事務(wù)提交
                      }   
                  });
              }
          }


          7個(gè)事務(wù)策略:
          1、  PROPAGATION_REQUIRED -- 支持當(dāng)前的事務(wù),如果不存在就創(chuàng)建一個(gè)新的。這是最常用的選擇。
          2 、 PROPAGATION_SUPPORTS -- 支持當(dāng)前的事務(wù),如果不存在就不使用事務(wù)。
          3 、 PROPAGATION_MANDATORY -- 支持當(dāng)前的事務(wù),如果不存在就拋出異常。
          4 、 PROPAGATION_REQUIRES_NEW -- 創(chuàng)建一個(gè)新的事務(wù),并暫停當(dāng)前的事務(wù)(如果存在)。
          5 、 PROPAGATION_NOT_SUPPORTED -- 不使用事務(wù),并暫停當(dāng)前的事務(wù)(如果存在)。
          6 、 PROPAGATION_NEVER -- 不使用事務(wù),如果當(dāng)前存在事務(wù)就拋出異常。
          7 、 PROPAGATION_NESTED -- 如果當(dāng)前存在事務(wù)就作為嵌入事務(wù)執(zhí)行,否則與 PROPAGATION_REQUIRED 類似。

            
            5個(gè)隔離策略:  
            ISOLATION_DEFAULT  
            ISOLATION_READ_UNCOMMITED  
            ISOLATION_COMMITED  
            ISOLATION_REPEATABLE_READ  
            ISOLATION_SERIALIZABLE
           ●   未授權(quán)讀取(Read Uncommitted):允許臟讀取,但不允許更新丟失。如果一個(gè)事務(wù)已經(jīng)開(kāi)始寫(xiě)數(shù)據(jù),則另外一個(gè)數(shù)據(jù)則不允許同時(shí)進(jìn)行寫(xiě)操作,但允許其他事務(wù)讀此行數(shù)據(jù)。該隔離級(jí)別可以通過(guò)“排他寫(xiě)鎖”實(shí)現(xiàn)。
           ●   授權(quán)讀?。≧ead Committed):允許不可重復(fù)讀取,但不允許臟讀取。這可以通過(guò)“瞬間共享讀鎖”和“排他寫(xiě)鎖”實(shí)現(xiàn)。讀取數(shù)據(jù)的事務(wù)允許其他事務(wù)繼續(xù)訪問(wèn)該行數(shù)據(jù),但是未提交的寫(xiě)事務(wù)將會(huì)禁止其他事務(wù)訪問(wèn)該行。
           ●   可重復(fù)讀?。≧epeatable Read):禁止不可重復(fù)讀取和臟讀取,但是有時(shí)可能出現(xiàn)幻影數(shù)據(jù)。這可以通過(guò)“共享讀鎖”和“排他寫(xiě)鎖”實(shí)現(xiàn)。讀取數(shù)據(jù)的事務(wù)將會(huì)禁止寫(xiě)事務(wù)(但允許讀事務(wù)),寫(xiě)事務(wù)則禁止任何其他事務(wù)。
           ●   序列化(Serializable):提供嚴(yán)格的事務(wù)隔離。它要求事務(wù)序列化執(zhí)行,事務(wù)只能一個(gè)接著一個(gè)地執(zhí)行,但不能并發(fā)執(zhí)行。如果僅僅通過(guò)“行級(jí)鎖”是無(wú)法實(shí)現(xiàn)事務(wù)序列化的,必須通過(guò)其他機(jī)制保證新插入的數(shù)據(jù)不會(huì)被剛執(zhí)行查詢操作的事務(wù)訪問(wèn)到。


           ●   更新丟失(Lost update):兩個(gè)事務(wù)都同時(shí)更新一行數(shù)據(jù),但是第二個(gè)事務(wù)卻中途失敗退出,導(dǎo)致對(duì)數(shù)據(jù)的兩個(gè)修改都失效了。這是因?yàn)橄到y(tǒng)沒(méi)有執(zhí)行任何的鎖操作,因此并發(fā)事務(wù)并沒(méi)有被隔離開(kāi)來(lái)。
           ●   臟讀取(Dirty Reads):一個(gè)事務(wù)開(kāi)始讀取了某行數(shù)據(jù),但是另外一個(gè)事務(wù)已經(jīng)更新了此數(shù)據(jù)但沒(méi)有能夠及時(shí)提交。這是相當(dāng)危險(xiǎn)的,因?yàn)楹芸赡芩械牟僮鞫急换貪L。
           ●   不可重復(fù)讀?。∟on-repeatable Reads):一個(gè)事務(wù)對(duì)同一行數(shù)據(jù)重復(fù)讀取兩次,但是卻得到了不同的結(jié)果。例如,在兩次讀取的中途,有另外一個(gè)事務(wù)對(duì)該行數(shù)據(jù)進(jìn)行了修改,并提交。
           ●   兩次更新問(wèn)題(Second lost updates problem):無(wú)法重復(fù)讀取的特例。有兩個(gè)并發(fā)事務(wù)同時(shí)讀取同一行數(shù)據(jù),然后其中一個(gè)對(duì)它進(jìn)行修改提交,而另一個(gè)也進(jìn)行了修改提交。這就會(huì)造成第一次寫(xiě)操作失效。
           ●   虛讀(Phantom Reads):事務(wù)在操作過(guò)程中進(jìn)行兩次查詢,第二次查詢的結(jié)果包含了第一次查詢中未出現(xiàn)的數(shù)據(jù)(這里并不要求兩次查詢的SQL語(yǔ)句相同)。這是因?yàn)樵趦纱尾樵冞^(guò)程中有另外一個(gè)事務(wù)插入數(shù)據(jù)造成的。

                                           Dirty reads          non-repeatable reads            phantom reads
          SERIALIZABLE                   不會(huì)                   不會(huì)                               不會(huì)
          REPEATABLE READ            不會(huì)                   不會(huì)                               會(huì)
          READ COMMITTED             不會(huì)                   會(huì)                                  會(huì)
          READ UNCOMMITTED         會(huì)                      會(huì)                                  會(huì)




          posted on 2007-07-20 10:00 Derek.Guo 閱讀(2352) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          MSN:envoydada@hotmail.com QQ:34935442
          主站蜘蛛池模板: 龙游县| 贡觉县| 巨野县| 龙游县| 郴州市| 都兰县| 北宁市| 大庆市| 平安县| 项城市| 桦甸市| 泰州市| 林甸县| 资阳市| 邵阳县| 茂名市| 收藏| 古蔺县| 龙南县| 正阳县| 建阳市| 容城县| 合江县| 英超| 富顺县| 辽宁省| 高雄市| 民县| 高清| 高淳县| 吉首市| 岗巴县| 社会| 松桃| 西乌| 尼木县| 英吉沙县| 勃利县| 马鞍山市| 盘山县| 兴隆县|