当前位置: 代码迷 >> 综合 >> spring aop 事务管理
  详细解决方案

spring aop 事务管理

热度:51   发布时间:2023-12-13 16:13:06.0

我们在开发过程中,经常会需要事务管理。比如,我们在执行某业务时,需要向表A、表B、表C更新数据,如果此业务执行成功,则表A、表B、表C更新数据;如果此业务执行失败,则表A、表B、表C回滚到初始状态。
由于我们的业务逻辑是在service层控制,因此,我们在service注入相关dao。我们在Service执行业务逻辑,分别更新数据到表A、表B、表C,通过spring aop 事务管理,保证Service层每个method的事务性。
下面我们讲解如何在applicationContext.xml配置spring AOP事务:
1.声明dataSource

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><property name="driverClass" value="#{ nplat['db.driverClass'] }" /><property name="jdbcUrl" value="#{ nplat['db.jdbcUrl'] }" /><property name="user" value="#{ nplat['db.user'] }" /><property name="password" value="#{ nplat['db.password'] }" /><property name="initialPoolSize" value="#{ nplat['c3p0.initialPoolSize'] }" /><property name="minPoolSize" value="#{ nplat['c3p0.minPoolSize'] }" /><property name="maxPoolSize" value="#{ nplat['c3p0.maxPoolSize'] }" /><property name="acquireIncrement" value="#{ nplat['c3p0.acquireIncrement'] }" /><property name="maxIdleTime" value="#{ nplat['c3p0.maxIdleTime'] }" /><property name="maxStatements" value="#{ nplat['c3p0.maxStatements'] }" /><property name="maxStatementsPerConnection" value="100" /><!--每隔60s时间检测连接是否可正常使用(unit: scond) --><property name="idleConnectionTestPeriod" value="60" /><!-- 获取connnection时测试是否有效 --><property name="testConnectionOnCheckin" value="true" /><!-- 连接测试语句 --><property name="preferredTestQuery" value="#{ nplat['c3p0.testsql'] }" /><property name="checkoutTimeout" value="10000" /><property name="numHelperThreads" value="5" />
</bean>

2.声明sessionFactory

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">          <property name="dataSource" ref="c3p0DataSource" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">#{ nplat['db.dialect'] }</prop><prop key="hibernate.hbm2ddl.auto">none</prop><prop key="hibernate.connection.release_mode">after_transaction</prop><prop key="hibernate.show_sql">false</prop><prop key="hibernate.format_sql">false</prop><prop key="hibernate.max_fetch_depth">3</prop><!-- 抓取的级联深度 --><prop key="hibernate.jdbc.fetch_size">50</prop><!-- 批量抓取的数量.mysql不支持 --><prop key="hibernate.jdbc.batch_size">30</prop><!-- 批量写入的数量 --><prop key="javax.persistence.validation.mode">none</prop><!-- HiberV3.5以上需配置该项 --></props></property><property name="packagesToScan"><list><value>com.gina.gc</value></list></property>
</bean>

3.声明transactionManager

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">    <property name="sessionFactory"><ref bean="sessionFactory" /></property>
</bean>

4.声明txAdvice

<tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 可以指定事物的传播行为/隔离级别/哪些异常不需要回滚/哪些异常需要回滚 --><tx:method name="get*" read-only="true" propagation="SUPPORTS" /><tx:method name="find*" read-only="true" propagation="SUPPORTS" /><tx:method name="is*" read-only="true" propagation="SUPPORTS" /><tx:method name="check*" read-only="true" propagation="SUPPORTS" /><tx:method name="list*" read-only="true" propagation="SUPPORTS" /><tx:method name="find*" read-only="true" propagation="SUPPORTS" /><tx:method name="*" read-only="false" propagation="REQUIRED" rollback-for="Exception" />            </tx:attributes>
</tx:advice>

read-only=”true” 表明事务只读
SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行
REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择
5.声明spring aop

<aop:config><!--expression="this(com.gina.gc.core.orm.manager.Manager)" --><aop:pointcut id="interceptorPointCuts"expression="execution(* com.gina.gc..*.manager.*Manager.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>

expression=”execution(* com.gina.gc...manager.*Manager.(..))” 中几个通配符的含义:
第一个 * —— 通配 任意返回值类型
第二个 * —— 通配 包com.gina.gc下的任意包
第三个 * —— 通配 包com.gina.gc..*.manager下以Manager结尾的class
第四个 * —— 通配 包com.gina.gc..*.manager下以Manager结尾的class的任意方法
第五个 .. —— 通配 方法可以有0个或多个参数

  相关解决方案