Spring的事务管理

<?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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 事务有一系列操作组成,这些操作是一个整体,密不可分,也就是说这些操作要么都执行成功,要么都不会执行。 事务的CAI...
    luoxn28阅读 523评论 0 0
  • 事务概念 1.什么是事务 事务是对数据库操作中最基本的单元,对数据库操作一组数据时,要么都成功,有一个失败,都失败...
    维乾阅读 187评论 0 0
  • 1.事务概念 1. 什么是事务 2. 事务特性 原子性 一致性 隔离性 持久性 3. 不考虑隔离性产生读问题 脏读...
    暖熊熊阅读 342评论 0 0
  • 这个主题的参考文章没找到特别好的,http://blog.csdn.net/trigl/article/detai...
    鱼仔_1625阅读 184评论 0 0
  • 1、时间无法弥补你的过错 人们总说:把一切交给时间,时间会冲淡一切。可是有些错误造成的遗憾,早已在你内心的某个角落...
    青墨QINGMO阅读 562评论 11 12