Spring3.x_Struts2.x_Hibernate3.x整合之声明式事务配置

22 篇文章 8 订阅
10 篇文章 0 订阅

在文章Spring3.x_Struts2.x_Hibernate3.x整合之OpenSessionInView的实现的基础上,事务是声明在Dao中,但是通常都会在Service中来处理多个业务逻辑的关系,

如:删除,更新等,此时如果在执行了一个步骤之后抛出抛出异常就会导致数据部完整,所以事务不应该在Dao中处理,而应该在Service中处理,这也是就是Spring提供的一个非常方便的工具,声明式的事务

完整的配置beans.xml

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- 打开Spring的Annotation的支持 -->
	<context:annotation-config />
	<!-- 设定Spring去哪些包中找Annotation -->
	<context:component-scan base-package="org.oms.spring" />

	<!-- 创建数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 配置连接池的初始值 -->
		<property name="maxActive" value="500" />
		<!-- 最大空闲时,当经过一个高峰之后,连接池可以将一些用布到的连接释放,一直减少到maxIdle为止 -->
		<property name="maxIdle" value="20" />
		<!-- 当最小空闲时,当连接少于minIdle时会自动申请一些连接 -->
		<property name="minIdle" value="1" />
		<property name="maxWait" value="1000" />
	</bean>
	<!-- 导入src下的jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<!-- 创建sessionFactory工厂 -->
	<!-- 如果使用的是Annotation方式不能使用org.springframework.orm.hibernate3.LocalSessionFactoryBean,而应该使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 设置Spring去那个包中查找相应的实体类 -->
		<property name="packagesToScan">
			<value>org.oms.spring.model</value>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.format_sql">false</prop>
			</props>
		</property>
	</bean>
	<!-- 开启HibernateTemplate,并且为其注入sessionFactory, 
			使用HibernateTemplate不太方便的就是要获取session得通过getSessionFactory() -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- 配置Spring的事物处理 -->
	<!-- 创建事物管理器 -->
	<!-- SessionFactory, DataSource, etc. omitted -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 配置AOP,Spring是通过AOP来进行事物管理的 -->
	<aop:config>
		<!-- 设置pointcut表示哪些方法要加入事物处理 -->
		<!-- 以下的事务是声明在Dao中,但是通常都会在Service中来处理多个业务逻辑的关系,
		如:删除,更新等,此时如果在执行了一个步骤之后抛出抛出异常就会导致数据部完整,所以事务不应该在Dao中处理,而应该在Service中处理,
		这也是就是Spring提供的一个非常方便的工具,声明式的事务 -->
		<aop:pointcut id="allMethods"
			expression="execution(* org.oms.spring.service.*.*(..))" />
		<!-- 通过advisor来确定具体要加入事物控制的方法 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods" />
	</aop:config>
	<!-- 配置哪些方法要加入事物控制 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<!-- 让所有方法都加入事物管理(效率低,实际使用中按需处理),为了提供效率,可以把一些查询之类的方法设置为只读事务 -->
			<tx:method name="*" propagation="REQUIRED" read-only="true"/>
			<tx:method name="add" propagation="REQUIRED"/>
			<tx:method name="save" propagation="REQUIRED"/>
			<tx:method name="delete" propagation="REQUIRED"/>
			<tx:method name="update" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>

</beans>

方法案例:
@Override
public void delete(int id) {
//	long count=userHibernateDao.getGroupUserCount(id);
//	if (count>0) throw new UserException("删除的组还有用户");
	//如果在这个位置抛出异常,则删除失败,事务回滚
	//if(id>0) throw new UserException();
	userHibernateDao.deleteByGroup(id);
	
	groupHibernateDao.delete(id);
}


个人备忘笔记~~



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值