spring 声明 hibernate 的 事物的几种用法

以下两个bean的配置是下面要用到的。
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!--数据库的连接池-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
	<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl"
			value="jdbc:mysql://localhost:3306/extjs?useUnicode=true&characterEncoding=UTF-8" />
		<property name="user" value="root" />
		<property name="password" value="mysql" />
		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="1" />
		<!--连接池中保留的最小连接数。-->
		<property name="minPoolSize" value="1" />
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="300" />
		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="60" />
		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="5" />
		<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod" value="60" />
	</bean>
	<!-- sessionFactory工厂 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<value>cn/csdn/model/Admin.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
	</bean>
	<!-- hiberante事务管理器 -->
	<bean id="hibernateTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 事务的通知 -->
	<tx:advice id="txadvice" transaction-manager="hibernateTransactionManager">
		<tx:attributes>
			<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" />
			<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" />
			<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
	<!-- 给事务配置一个切面 -->
	<aop:config>
		<!-- advisor 切入点和通知组合体 -->
		<aop:pointcut expression="execution(* cn.csdn.service..*(..))"
			id="txPointcut" />
		<aop:advisor advice-ref="txadvice" pointcut-ref="txPointcut" />
	</aop:config>
	<!-- 导入其它的配置文件-->
	<import resource="beans-dao.xml"/>
	<import resource="beans-service.xml"/>
	<import resource="beans-action.xml"/>
</beans>

2种;;;<pre name="code" class="html"> <!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <!-- 业务逻辑层(是对各个DAO层的正面封装)主要用到<<门面模式>> --> <bean id="fundService" class="com.jack.fund.service.serviceimpl.FundService"> <property name="operdao"> <ref bean="operatorDAO" /> </property> <property name="producedao"> <ref bean="fundProduceDAO" /> </property> <property name="customerdao"> <ref bean="customerDAO" /> </property> <property name="accountdao"> <ref bean="accountDAO" /> </property> <property name="fundaccountdao"> <ref bean="fundAccountDAO" /> </property> <property name="fundtransdao"> <ref bean="fundTransDAO" /> </property> </bean>


 可能还有其他很多模块。<bean id="fundService"/>可能只是其中的模块。第一种:配置声明式事务的方法如下。也是我们最常用的方法了,它适用于你的库表比较少的情况下。 
<bean id="fundServiceDAOProxy"  
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
      <!-- 配置事务管理器 -->  
      <property name="transactionManager">  
       <ref bean="transactionManager" />  
      </property>  
      <!-- 此属性指定目标类本省是否是代理的对象.如果目标类没有实现任何类,就设为true代表自己 -->  
      <property name="proxyTargetClass">  
       <value>false</value>  
      </property>  
      <property name="proxyInterfaces">  
       <value>com.jack.fund.service.IFundService</value>  
      </property>  
      <!-- 目标bean -->  
      <property name="target">  
       <ref bean="fundService" />  
      </property>  
      <!-- 配置事务属性 -->  
      <property name="transactionAttributes">  
       <props>  
        <prop key="delete*">PROPAGATION_REQUIRED</prop>  
        <prop key="add*">PROPAGATION_REQUIRED</prop>  
        <prop key="update*">PROPAGATION_REQUIRED</prop>  
        <prop key="save*">PROPAGATION_REQUIRED</prop>  
        <prop   key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
       </props>  
      </property>  
     </bean>  

以下可能还有其他的xxxServiceDAOProxy.大家可以看出针对每一个功能模块配置一个业务代理服务。如果模块多大话,就显得代码有点多了,发现他们只是稍微一点不一样。这时我们就应该想到继承的思想。用第二种方法。第二种:配置声明式事务的方法如下。这种情况适合相对比较多的模块时使用。
 <!-- 利用继承的思想简化配置,要把abstract="true" -->  
     <bean id="transactionBase"  
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
      lazy-init="true" abstract="true">  
      <!-- 配置事务管理器 -->  
      <property name="transactionManager">  
       <ref bean="transactionManager" />  
      </property>  
      <!-- 配置事务属性 -->  
      <property name="transactionAttributes">  
       <props>  
        <prop key="delete*">PROPAGATION_REQUIRED</prop>  
        <prop key="add*">PROPAGATION_REQUIRED</prop>  
        <prop key="update*">PROPAGATION_REQUIRED</prop>  
        <prop key="save*">PROPAGATION_REQUIRED</prop>  
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
       </props>  
      </property>  
     </bean>  

而具体的模块可以简单的这样配置。 只要指明它的parent(父类)就可以了。 父类一般把abstract="true",因为在容器加载的时候不需要初始化,等到用的时候再有它的子类调用的时候,再去初始化。
   <bean id="fundServiceDAOProxy" parent="transactionBase" >  
      <property name="target">  
      <ref bean="fundService" />  
      </property>  
     </bean>  

这样配置的话,如果有多个像fundService这样模块时,可以少些很多重复的代码。第三种:配置声明式事务的方法如下。主要利用BeanNameAutoProxyCreator自动创建事务代理
  <bean id="transactionInterceptor"  
     class="org.springframework.transaction.interceptor.TransactionInterceptor">  
     <property name="transactionManager">  
      <ref bean="transactionManager" />  
     </property>  
     <!-- 配置事务属性 -->  
     <property name="transactionAttributes">  
      <props>  
       <prop key="delete*">PROPAGATION_REQUIRED</prop>  
       <prop key="add*">PROPAGATION_REQUIRED</prop>  
       <prop key="update*">PROPAGATION_REQUIRED</prop>  
       <prop key="save*">PROPAGATION_REQUIRED</prop>  
       <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
      </props>  
     </property>  
    </bean>  
      
    <bean  
     class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
     <property name="beanNames">  
      <list>  
       <value>fundService</value>  
      </list>  
     </property>  
     <property name="interceptorNames">  
      <list>  
       <value>transactionInterceptor</value>  
      </list>  
     </property>  
    </bean>  

这种方法主要利用了拦截器的原理。 前三种方法一般都必需指定具体的模块bean. 如果模块过多话,比如一个大型的网站一般有几十个模块,我们就得考虑用第四种的配置方式了。自动创建事务代理的方式了。第四种:配置声明式事务的方法如下。
<bean id="transactionInterceptor"  
      class="org.springframework.transaction.interceptor.TransactionInterceptor">  
      <property name="transactionManager">  
       <ref bean="transactionManager" />  
      </property>  
    </bean>  
      
    <!-- 自动代理 -->  
     <bean id="autoproxy"  
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
      <!-- 可以是Service或DAO层(最好是针对业务层*Service) -->  
      <property name="beanNames">  
       <list>  
        <value>*Service</value>  
       </list>  
      </property>  
      <property name="interceptorNames">  
       <list>  
           <value>transactionInterceptor</value>  
       </list>  
      </property>  
     </bean>  

自动代理还有一种用法就是结合正则表达式和advice使用。
<bean id="transactionInterceptor"  
      class="org.springframework.transaction.interceptor.TransactionInterceptor">  
      <property name="transactionManager">  
       <ref bean="transactionManager" />  
      </property>  
    </bean>  
      
     <bean id="autoProxyCreator"  
      class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" >  
     </bean>  
      
    <bean id="regexpMethodPointcutAdvisor"  
      class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
      <property name="advice">  
      <ref bean="transactionInterceptor" />  
      </property>  
      <property name="pattern">  
      <value>.*</value>  
      </property>  
     </bean>  

这个方法可以针对具体的模块进行拦截并进行事务处理。 在你的实际项目中,你可以根据你的情况选用不同的方法。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值