一、首先看看最常用的声明性事务
1、代理,最常用的声明性事务
公共配置:就是
①、配置数据源 <bean id="FramedataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${frame.driverClassName}"></property> <property name="url" value="${frame.url}"></property> <property name="username" value="${frame.username}"></property> <property name="password" value="${frame.password}"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="20"></property> <property name="maxWait" value="2000"></property> <property name="defaultAutoCommit" value="false"></property> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="180"/> </bean> ②.将数据源注入到sessionFactory <!-- Hibernate Factory --> <bean id="FramesessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="FramedataSource"></property> <!-- 在少量HBM文件的时候推荐使用 <property name="mappingResources"> <list>
</list> </property> --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SybaseDialect</prop> <prop key="show_sql">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> <!-- 文件较多的时可以统一管理hbm文件放入到对应的文件夹下面即可. --> <property name="mappingDirectoryLocations"> <list> <value>WEB-INF/hbm/system</value> <value>WEB-INF/hbm/czsf</value> </list> </property> </bean> ③ 将sessionFactory注入到hibernateTemplate模板中 <bean id="FramehibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref local="FramesessionFactory" /> </property> </bean> ④、将sessionFactory注入到事务管理器中 <bean id="FrametransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="FramesessionFactory" /> </property> </bean>
|
将上④事务管理器注入到代理事务管理器中 <!-- 事务代理 --> <bean id="FrametransactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="FrametransactionManager"/> </property> <!-- 使用 cglib.jar代理类--> <property name="proxyTargetClass"> <value>true</value> </property> <property name="transactionAttributes"> <props> <prop key="init*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="create*">PROPAGATION_REQUIRED</prop> <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="uploadFile">PROPAGATION_REQUIRED</prop> <prop key="import*">PROPAGATION_REQUIRED</prop> <prop key="do*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> 再对每一个service类进行代理 <!-- 用户信息管理 --> <bean id="yhglservice" parent="FrametransactionProxy"> <property name="target"> <bean class="tdh.czsf.service.YhglService"> <property name="hibernateTemplate"> <ref bean="FramehibernateTemplate"/> </property> </bean> </property> </bean> 注意:此service类中的方法名一定要和上面的method*或*method名的规则一样,否则不会进行事务处理 |
2、拦截器,通过拦截器,符合命名规则的方法进行事务代理
公共配置
<!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation"> <value>classpath:config/hibernate.cfg.xml</value> </property> <property name="packagesToScan"> <list> <value>com.entity</value> </list> </property> </bean>
<!-- 配置事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
<!-- 配置DAO --> <bean id="userDao" class="com.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> 可以在hibernateTemplate注入sessionFactory,然后这里面可以注入hibernateTemplate </bean> |
<!-- 第三种配置事务的方式,拦截器 (不常用)--> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager"></property> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="del*">PROPAGATION_REQUIRED, -Exception</prop> <prop key="*">PROPAGATION_REQUIRED, readOnly</prop> </props> </property> </bean> <bean id="proxyFactory" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <value>transactionInterceptor</value>//对于拦截器的ID </list> </property>
<!-- 这个属性为true时,表示被代理的是目标类本身而不是目标类的接口 -->
<property name="beanNames">//此处为service类 <list> <value>*ServiceImpl</value> </list> </property> </bean> |
Spring事务类型详解:
PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
二、关于spring+hibernate的sessionFacroy的配置获取数据库连接
三、关于hibernate单独使用获取数据库连接