spring整合hibernate时的事务管理

1.在web.xml加入OpenSessionInViewFilter


	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>excludeSuffixs</param-name>
			<param-value>js,css,jpg,gif,png</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>



2.在beans.xml配置事务


	<!-- 1.配置Spring的声明式事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 2.配置事务属性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>
	
	<!-- 3.配置事务切入点,再把事物属性和事务切入点关联起来 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.mmeorycat.myspringmvc.service.*.*(..))" id="txpointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txpointcut"/>
	</aop:config>



3. Dao层使用spring提供的sessionFactory,并用其获取session


public void update(User user){
	sessionFactory.getCurrentSession().update(user);
}


评论列表