`
alex09
  • 浏览: 969769 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

spring aop 申明事务

阅读更多
1、getCurrentSession()与openSession()的区别?
(1)采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
(2)采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭
 
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如
下配置:
(1) 如果使用的是本地事务(jdbc事务)
hibernate.current_session_context_class thread
(2) 如果使用的是全局事务(jta事务)
hibernate.current_session_context_class jta
<bean id="securityHandler" class="com.alex.spring.SecurityHandler"/>           
	
<bean id="userManager" class="com.alex.spring.UserManagerImpl"/>
	
	<aop:config>
		<aop:aspect id="security" ref="securityHandler">
			<aop:pointcut id="allAddMethod" expression="execution(* com.alex.spring.UserManagerImpl.add*(..))"/>
			<aop:before method="checkSecurity"  ointcut-ref="allAddMethod"/>
		</aop:aspect>
	</aop:config>	

申明事务配置
<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>	
	</bean>           
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>	
	</bean>
	
	<!-- 配置事务的传播特性 -->
	<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>
	
	<!-- 那些类的哪些方法参与事务 -->
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* com.alex.usermgr.manager.*.*(..))"/>
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
	</aop:config>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics