Spring _事务

来源:互联网 发布:淘宝上电脑主机 编辑:程序博客网 时间:2024/05/16 08:15

事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制.而且主要变化的是代理机制。

第一种,使用tx标签方式

第二种,使用代理方式:

<!-- 第二种配置事务的方式 ,代理--><bean id="transactionProxy"    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"    abstract="true">    <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="userDao" parent="transactionProxy">    <property name="target">        <!-- 用bean代替ref的方式-->        <bean class="com.dao.UserDaoImpl">            <property name="sessionFactory" ref="sessionFactory"></property>        </bean>    </property></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>        </list>    </property>    <property name="beanNames">        <list>            <value>*Dao</value>        </list>    </property></bean>
事务传播属性:

PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。

PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。

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

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

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

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

编程式事务

编程式即采用注解的方式,需要注意的是,使用注解的方式需要在Spring的配置文件中加入一句话:<context:annotation-config />,其作用是开启注解的方式。具体配置如下:

<!--开启注解方式--><context:annotation-config /><!-- 配置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><!-- 第四种配置事务的方式,注解 --><tx:annotation-driven transaction-manager="transactionManager"/>
复制代码

0 0
原创粉丝点击