美文网首页我爱编程
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的事务管理

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