事务配置

ApplicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
 </bean>
 <!--事务管理器-->
 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
 </bean>
 <!--用注解配置事务,就不需要配置下面的 事务增强 和 配置切面, java方法用@Transactional注解-->
    <tx:annotation-driven transaction-manager="txManager"/>



  <!--事务增强,采取什么样的事务策略-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!--所有find开头的方法,timeout:设置事务超时时间,-1就是不超时,秒为单位;read-only="true":事务为只读,查询才设置,提高查询性能,默认为false-->
            <tx:method name="set*" timeout="1000" propagation="SUPPORTS" read-only="true"/>
            <!--其它没想到的方法用*表示,propagation="REQUIRED" 表示有事务就用当前事务,没有就自动创建一个事务-->
            <!--propagation="SUPPORTS" 表示 有事务就用当前事务,没事务就不用事务,一般用于查询语句-->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--配置切面,就是对那一些方法增强-->
    <aop:config>
        <!--execution(* com.big3.service.impl.*.*(..)) 第一个*表示方法返回类型-->
        <aop:pointcut id="myPoint" expression="execution(* cn.kgc.service..*.*(..))"/>
        <!--advice-ref: 增强引用 pointcut-ref:切入点引用 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint"/>
    </aop:config>

 </beans>
 

 java代码
 @Transactional(timeout = 10,readOnly = false) //事务控制,可以写在类前面
    public int setDetail(Bbs_detail bdsl) {
        int countOne=detailDao.setDetail(bdsl);
        //int i=1/0;
        int countTwo=detailDao.setDetail(bdsl);
        if(countOne>0 && countTwo>0) {
            return 1;
        }else {
            return 0;
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值