spring事物

spring事物管理分为两种,编程式事物和声明式事物

一、 编程式事物

编程式事物配置相对繁琐,而且不常用,就不在这里详细说明,需要的c友可以参考
http://jinnianshilongnian.iteye.com/blog/1441271
http://www.cnblogs.com/EasonJim/p/6911208.html

#数据源配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" scope="singleton">
    <property name="driverClassName" value="${databaseDriverClassName}" />
    <property name="url" value="${databaseUrl}" />
    <property name="username" value="${databaseUsername}" />
    <property name="password" value="${databasePasswd}" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="true" />
    <property name="testOnReturn" value="true" />
    <property name="validationQuery" value="SELECT NOW()" />
    <property name="validationQueryTimeout" value="1" />
        <!-- 连接初始值,连接池启动时创建的连接数量的初始值 -->
    <property name="initialSize" value="10" />
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 ,0时无限制-->
    <property name="maxIdle" value="10" />
        <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
    <property name="minIdle" value="5" />
        <!-- 连接池的最大值,同一时间可以从池分配的最多连接数量,0时无限制 -->
    <property name="maxActive" value="1000" />
        <!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
    <property name="maxWait" value="1000" />
        <!-- 是否在自动回收超时连接的时候打印连接的超时错误 -->
    <property name="logAbandoned" value="true" />
        <!-- 是否自动回收超时连接 -->
    <property name="removeAbandoned" value="true" />
        <!-- 超时时间(以秒数为单位) -->
    <property name="removeAbandonedTimeout" value="120" />
        <!-- 是否对已备语句进行池管理(布尔值),是否对PreparedStatement进行缓存 -->
    <property name="poolPreparedStatements" value="false" />
        <!-- 是否对sql进行自动提交 -->
    <property name="defaultAutoCommit" value="true" />
</bean>

声明式事物

声明式1:基于@Trasactional注解

#声明式事物配置1

<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 引用数据源 -->
    <property name="dataSource" ref="dataSource" />
</bean> 
<!-- 注解驱动,对@Transactional这个注解进行的驱动,就可以使用@Trasactional注解了 -->
<tx:annotation-driven transaction-manager="transactionManager" />   
//声明式java代码1  
@Service
@Transactional(readOnly = true)
public class AppErrorLogServiceImpl implements AppErrorLogService {

    @Override
    @Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,readOnly = false)
    public Integer saveAppErrorLog(AppErrorLog appErrorLog) {
        int result = appErrorLogMapper.save(log);
        int i = 8/0; //运行时异常
        return result;
    }
}

声明式2:基于<tx:advice>、<aop:config >

#声明式事物配置2

<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 引用数据源 -->
    <property name="dataSource" ref="dataSource" />
</bean> 
<tx:advice id="advice" transactionmanager="transactionManager">
    <tx:attributes>
        <!-- 拦截save开头的方法,配置事物的隔离级别和事物的传播行为,read-only=false表示可以修改 -->
        <tx:method name="save*" isolation="READ_COMMITTED" propagation="REQUIRED" read-only="false"/>
    </tx:attributes>
</tx:advice>
<!-- 定义切入点,expression为切人点表达式,如下是指定impl包下的所有方法,具体以自身实际要求自定义  -->  
<aop:config >
    <aop:pointcut id="pointcut" expression="execution(* com.lping.tx.service.impl.*.*(..))"/>
    <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
//java代码  
@Service
public class AppErrorLogServiceImpl implements AppErrorLogService {
    @Override
    public Integer saveAppErrorLog(AppErrorLog appErrorLog) {
        int result = appErrorLogMapper.save(log);
        int i = 8/0; //运行时异常
        return result;
    }
}

老铁们可以参考 http://blog.csdn.net/liaohaojian/article/details/70139151
http://blog.csdn.net/liaohaojian/article/details/68488150 事物的隔离级别、传播行为、以及spring+mybatis+atomikos实现分布式事物管理

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值