使用aop配置事务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-2.0.xsd
https://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-2.0.xsd
https://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="phrDS"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.phrConn" />
</bean>
<bean id="cdaDS"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.cdaConn" />
</bean>
<!-- CDA
<bean id="cdaDS"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>${dbconn.cda.url}</value>
</property>
<property name="username">
<value>${dbconn.cda.userName}</value>
</property>
<property name="password">
<value>${dbconn.cda.password}</value>
</property>
</bean> -->
<!--文档数据库数据源配置-Oracle数据源配置 -->
<bean id="docstoreDS"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>${dbconn.docstore.url}</value>
</property>
<property name="username">
<value>${dbconn.docstore.userName}</value>
</property>
<property name="password">
<value>${dbconn.docstore.password}</value>
</property>
</bean>
<bean id="phrSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="phrDS" />
<property name="mappingDirectoryLocations">
<list>
<value>${dbconn.docstore.mappingDirectory}</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${dbconn.docstore.dialect}
</prop>
<prop key="hibernate.show_sql">
${dbconn.docstore.showsql}
</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">
false
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<!--<prop key="current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</prop>
for jta compatilibility (jta)
org.hibernate.context.JTASessionContext
-->
<prop key="hibernate.cglib.use_reflection_optimizer">
true
</prop>
</props>
</property>
</bean>
<bean id="cdaSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="cdaDS" />
<property name="mappingDirectoryLocations">
<list>
<value>${dbconn.docstore.mappingDirectory}</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${dbconn.docstore.dialect}
</prop>
<prop key="hibernate.show_sql">
${dbconn.docstore.showsql}
</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">
false
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<!--
for jta compatilibility (jta)
org.hibernate.context.JTASessionContext
<prop key="current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</prop>-->
</props>
</property>
</bean>
<!-- Template配置 -->
<bean id="phrJDBCTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="phrDS" />
</bean>
<!-- HibernateTemplete 配置 -->
<bean id="phrHibernateTemplete"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="phrSessionFactory" />
</bean>
<!-- HibernateDAO配置 -->
<bean id="phrHibernateDao"
class="com.xbzc.phr.dao.HibernateSuperDAOImpl">
<property name="sessionFactory" ref="phrSessionFactory" />
</bean>
<!-- HibernateDAO配置-CDA查看使用 -->
<bean id="cdaHibernateDao"
class="com.xbzc.phr.dao.CdaHibernateSuperDAOImpl">
<property name="sessionFactory" ref="cdaSessionFactory" />
</bean>
<span style="color:#ff0000;"> <bean id="springTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="phrSessionFactory" />
</bean>
<!-- 通知配置-->
<tx:advice id="txAdvice"
transaction-manager="springTransactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="DaoException"/>
</tx:attributes>
</tx:advice>
<!-- 事务切面配置 ,配置参与事务的类-->
<aop:config>
<aop:pointcut id="point"
expression=" execution(* com.xbzc.phr.service.um.UserManageSrv.*(..))" />
<!-- 指定使用jta事务的类、方法 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="point" />
</aop:config>
</span></beans>
上面是我的配置文件,主要是红色的代码, rollback-for="DaoException" 很关键,这里我指定的要回滚的异常是我的自定义异常DaoException,因此在程序中,如果抛出了未被捕获的该异常,则事务回滚,参见下面代码:
[java]
public RPCMessage saveUser(TUmUserDTO entity)<span style="color:#ff0000;"> throws DaoException </span>{
log.info("服务层操作:添加从业者 saveUser (TUmUserDTO [" + entity + "])");
try {
if (entity == null) {
throw new ServiceException(
ServiceException.ERRORCODE_PARAM_NULL);
}
如果rollback-for="ServiceException" ,但是程序中catch住了该异常,则事务不回滚,因为异常被生吞了,事务已经被提交了,参见下面代码:
[java]
log.debug("服务层操作:从业者角色编辑完成");
return this.getRPCMessage();
}catch (<span style="color:#ff0000;">ServiceException </span>e){
return this.getRPCMessage(e);
}
如果rollback-for="Exception" ,则属于Exception以及Exception包含的异常都会事务回滚,这里因为DaoException,ServiceException都是继承自Exception,因此也会回滚。
配置中的:expression=" execution(* com.xbzc.phr.service.um.UserManageSrv.*(..))" /> 的作用是,事务回滚时,对哪些类中的方法起作用 。
如果没有配置rollback-for=" " 则默认只回滚未被捕获的运行时异常引起的操作。
上一篇: Python“隐藏”特性
下一篇: 关于vuex store的运用实例讲解