美文网首页我爱编程
Spring的事务管理

Spring的事务管理

作者: 打死你的小白兔 | 来源:发表于2018-03-31 04:30 被阅读0次
<?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" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:jdbc.properties" />

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="initialPoolSize" value="5"></property>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="10" />
        <property name="maxIdleTime" value="600" />
        <property name="maxStatements" value="0" />
    </bean>


    <!--1.配置事物管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--2.配置事物增强 -->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 告诉spring容器什么样的目标方法采用什么样的事务策略
             name 用来说明目标方法
              save* 以save开头的目标方法 
              isolation 隔离属性 
              propagation 传播属性 是解决事务嵌套问题的 
              read-only 为true:只读事务 只能读 为false:读写事务 -->
            <tx:method name="save*" />
        </tx:attributes>
    </tx:advice>
    <!--3.配置切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.hw.entity.*.*(..))"
            id="perform" />
        <aop:advisor advice-ref="txadvice" pointcut-ref="perform" />
    </aop:config>
</beans>

注解事务配置

<?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" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:jdbc.properties" />

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="initialPoolSize" value="5"></property>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="10" />
        <property name="maxIdleTime" value="600" />
        <property name="maxStatements" value="0" />
    </bean>


    <!--1.配置事物管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--2.配置事物注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
</beans>

相关文章

  • Spring中的AOP事务

    【目录】1 Spring的事务管理机制2 Spring事务管理两种方式 1 Spring的事务管理机制 Sprin...

  • Spring事务管理只对出现运行期异常进行回滚

    使用spring难免要用到spring的事务管理,要用事务管理又会很自然的选择声明式的事务管理,在spring的文...

  • spring事务

    1、spring事务管理器PlatformTransactionManager 1.1、没有spring事务管理器...

  • Spring之事务管理

    Spring事务管理(详解+实例)Spring详解(八)------事务管理 一. 概念 事务(Transacti...

  • Spring-事务机制

    一、Spring事务 事务管理概述 Spring事务管理分为编程式事务管理和声明式事务管理两种 编程式事务:允许用...

  • 19、Spring-事务机制-使用

    一、简介 Spring事务管理分为编程式事务管理和声明式事务管理两种, 声明式事务管理:底层是建立在Spring ...

  • Spring声明式事务管理之一:五大属性分析

    1.Spring事务管理概述 Spring事务管理分为编程式事务管理和声明式事务管理两种。编程式事务允许用户在实现...

  • Spring基础(三)

    11. 事务管理 11.1 Spring Framework事务管理介绍 广泛的事务支持是Spring Frame...

  • 【济南中心】 Spring事务管理的方式

    【济南中心】 Spring事务管理的方式 1.spring支持编程式事务管理和声明式事务管理两种方式 编程...

  • Spring事务的种类

    Spring事务的种类: spring支持编程式事务管理和声明式事务管理两种方式: ①编程式事务管理使用Trans...

网友评论

    本文标题:Spring的事务管理

    本文链接:https://www.haomeiwen.com/subject/etlbcftx.html