spring 事务匹配模式

Spring 声明式事务无需编写程序代码即实现事务控制:

1.2配置信息参考如下:

   1.给每个service 注入时指定spring 代理类:

        <bean id="tbljdService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

       <property name="transactionManager" ref="transactionManager"/>

       <property name="target" ref="tbljdServiceTarget"/>

       <property name="transactionAttributes">

         <props>

            <prop key="add*">PROPAGATION_REQUIRED</prop>

            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>

            <prop key="execute">PROPAGATION_REQUIRED</prop>

         </props>

       </property>

    </bean>

    <bean id="transactionManager"       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

       <property name="sessionFactory" ref="sessionFactory" />

    </bean>

<bean id="tbljdServiceTarget"

     class="com.ssh.service.impl.TblJdServiceImpl">

     <property name="tblJdDao">

         <ref bean="tblJdDao" />

     </property>

     <property name="tblQxService">

         <ref bean="tblQxService" />

     </property>

  </bean>

 

由于以上代码在每个 service 注入的时候指定spring代理类,如果service比较多的话实现比较繁琐,可以把通用的代码统一管理起来,参照以下做法:

    <bean id="baseService" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

       <property name="transactionManager" ref="transactionManager"/>

       <property name="transactionAttributes">

         <props>

            <prop key="add*">PROPAGATION_REQUIRED</prop>

            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>

            <prop key="execute">PROPAGATION_REQUIRED</prop>

         </props>

       </property>

    </bean>

   

    <bean id="tbljdService" parent="baseService">

       <property name="target" ref="tbljdServiceTarget"/>

    </bean>

<bean id="tbljdServiceTarget"

     class="com.ssh.service.impl.TblJdServiceImpl">

     <property name="tblJdDao">

         <ref bean="tblJdDao" />

     </property>

     <property name="tblQxService">

         <ref bean="tblQxService" />

     </property>

  </bean>

 

 

 

事务类型

说明

PROPAGATION_REQUIRED

如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

PROPAGATION_SUPPORTS

支持当前事务,如果当前没有事务,就以非事务方式执行(即:不需要事务处理环境,如果有事务正在运行的话,这个方法也可以运行在这个事务里)

PROPAGATION_MANDATORY

使用当前的事务,如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW

新建事务,表示方法必须运行在自己的事务里。

PROPAGATION_NEVER

以非事务方式执行,如果当前存在事务,则抛出异常。

 

 

 

 

可以使用spring2.0 声明式事务简化1.2 。代码如下:

<?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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  <tx:advice id="txAdvice" transaction-manager="transactionManager">

    <tx:attributes>

      <tx:method name="execute" propagation="REQUIRED"/>

    </tx:attributes>

  </tx:advice>

  <aop:config>

<aop:pointcut id="serviceMethod" 

expression="execution(* com.ssh.service.*.*(..))"/>

    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config>

 

集成SSH框架后,在页面中显示数据时可能导航到另外一个对象,比如页面显示人员信息时需要把人员所在的部门也显示出来,但是部门是在另外一个实体保存的。显示人员对于的部门信息时会出现session 已经关闭的错误。解决办法:在web.xml 中加入过滤器:

    <filter>

       <filter-name>opensession</filter-name>

    <filter-class>

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

</filter-class>

    </filter>

    <filter-mapping>

       <filter-name>opensession</filter-name>

       <url-pattern>/*</url-pattern>

</filter-mapping>

 

 

也可以扩展OpenSessionInViewFilter 参考代码如下:

public class MyOpenSessionView extends OpenSessionInViewFilter {

protected void closeSession(Session session, SessionFactory sessionFactory) {

       session.flush();

       super.closeSession(session, sessionFactory);

    }

    protected Session getSession(SessionFactory sessionFactory)

           throws DataAccessResourceFailureException {

    Session session = SessionFactoryUtils.getSession(sessionFactory, true);

       session.setFlushMode(FlushMode.COMMIT);

       return session;

    }

}

把扩展的类加入到web.xml中替换OpenSessionInViewFilter

 

 

 

事务类型

说明

PROPAGATION_REQUIRED

如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

PROPAGATION_SUPPORTS

支持当前事务,如果当前没有事务,就以非事务方式执行。(不需要事务处理环境,如果有事务正在运行的话,这个方法也可以运行在这个事务里)

PROPAGATION_MANDATORY

使用当前的事务,如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW

新建事务,如果当前存在事务,把当前事务挂起。

PROPAGATION_NOT_SUPPORTED

以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

PROPAGATION_NEVER

以非事务方式执行,如果当前存在事务,则抛出异常。

PROPAGATION_NESTED

如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP(面向切面编程)是Spring框架中的一个重要特性,它允许我们在应用程序中通过将横切关注点(如日志记录、事务管理等)与核心业务逻辑分离开来,从而提高代码的模块化和复用性。 在Spring中,AOP通过代理模式实现。当一个被AOP代理的方法被调用时,代理会拦截方法的执行并执行附加的横切逻辑。这种拦截和执行的方式称为通知(Advice)。在Spring中,常见的通知类型有前置通知(Before)、后置通知(After)、返回通知(AfterReturning)和异常通知(AfterThrowing)等。 事务控制是AOP的一个重要应用场景之一。在Spring中,我们可以通过AOP配置来实现声明式事务管理,即通过在方法或类上添加事务注解来控制事务的行为。 为了使用Spring AOP配置事务控制,我们需要进行以下步骤: 1. 配置数据源和事务管理器:在Spring配置文件中配置数据源和事务管理器,以便应用程序可以连接到数据库并管理事务。 2. 配置事务通知:使用AOP配置来定义事务通知,可以将通知与特定的方法或类关联起来。例如,可以使用`@Transactional`注解将事务通知应用于需要进行事务管理的方法或类。 3. 配置切入点:切入点定义了哪些方法或类将被AOP代理。我们可以使用通配符表达式来匹配方法或类的名称或特征。 4. 配置通知类型:根据需求,选择适当的通知类型,如前置通知、后置通知、返回通知或异常通知。 5. 配置AOP代理:通过配置AOP代理,确保代理能够拦截并应用事务通知。 总结来说,Spring AOP提供了一种简洁而强大的方式来实现横切关注点的模块化,并且在事务控制方面有着重要的应用。通过合理配置AOP,我们可以将事务管理与核心业务逻辑解耦,提高代码的可维护性和可读性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值