<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>
<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>
<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>
<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





ApplicationContext.xml
<bean id="sessionFactory_itdb" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate_itdb.cfg.xml</value>
</property>
</bean>
<bean id="it_dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/itdb</value>
</property>
</bean>
<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>
<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>
<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"/>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">








</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)開始寫數(shù)據(jù),則另外一個(gè)數(shù)據(jù)則不允許同時(shí)進(jìn)行寫操作,但允許其他事務(wù)讀此行數(shù)據(jù)。該隔離級(jí)別可以通過“排他寫鎖”實(shí)現(xiàn)。
● 授權(quán)讀取(Read Committed):允許不可重復(fù)讀取,但不允許臟讀取。這可以通過“瞬間共享讀鎖”和“排他寫鎖”實(shí)現(xiàn)。讀取數(shù)據(jù)的事務(wù)允許其他事務(wù)繼續(xù)訪問該行數(shù)據(jù),但是未提交的寫事務(wù)將會(huì)禁止其他事務(wù)訪問該行。
● 可重復(fù)讀取(Repeatable Read):禁止不可重復(fù)讀取和臟讀取,但是有時(shí)可能出現(xiàn)幻影數(shù)據(jù)。這可以通過“共享讀鎖”和“排他寫鎖”實(shí)現(xiàn)。讀取數(shù)據(jù)的事務(wù)將會(huì)禁止寫事務(wù)(但允許讀事務(wù)),寫事務(wù)則禁止任何其他事務(wù)。
● 序列化(Serializable):提供嚴(yán)格的事務(wù)隔離。它要求事務(wù)序列化執(zhí)行,事務(wù)只能一個(gè)接著一個(gè)地執(zhí)行,但不能并發(fā)執(zhí)行。如果僅僅通過“行級(jí)鎖”是無法實(shí)現(xiàn)事務(wù)序列化的,必須通過其他機(jī)制保證新插入的數(shù)據(jù)不會(huì)被剛執(zhí)行查詢操作的事務(wù)訪問到。
● 更新丟失(Lost update):兩個(gè)事務(wù)都同時(shí)更新一行數(shù)據(jù),但是第二個(gè)事務(wù)卻中途失敗退出,導(dǎo)致對(duì)數(shù)據(jù)的兩個(gè)修改都失效了。這是因?yàn)橄到y(tǒng)沒有執(zhí)行任何的鎖操作,因此并發(fā)事務(wù)并沒有被隔離開來。
● 臟讀取(Dirty Reads):一個(gè)事務(wù)開始讀取了某行數(shù)據(jù),但是另外一個(gè)事務(wù)已經(jīng)更新了此數(shù)據(jù)但沒有能夠及時(shí)提交。這是相當(dāng)危險(xiǎn)的,因?yàn)楹芸赡芩械牟僮鞫急换貪L。
● 不可重復(fù)讀取(Non-repeatable Reads):一個(gè)事務(wù)對(duì)同一行數(shù)據(jù)重復(fù)讀取兩次,但是卻得到了不同的結(jié)果。例如,在兩次讀取的中途,有另外一個(gè)事務(wù)對(duì)該行數(shù)據(jù)進(jìn)行了修改,并提交。
● 兩次更新問題(Second lost updates problem):無法重復(fù)讀取的特例。有兩個(gè)并發(fā)事務(wù)同時(shí)讀取同一行數(shù)據(jù),然后其中一個(gè)對(duì)它進(jìn)行修改提交,而另一個(gè)也進(jìn)行了修改提交。這就會(huì)造成第一次寫操作失效。
● 虛讀(Phantom Reads):事務(wù)在操作過程中進(jìn)行兩次查詢,第二次查詢的結(jié)果包含了第一次查詢中未出現(xiàn)的數(shù)據(jù)(這里并不要求兩次查詢的SQL語句相同)。這是因?yàn)樵趦纱尾樵冞^程中有另外一個(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ì)