`
guanpanpan
  • 浏览: 16710 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

代码实例-spring声明式事务

阅读更多

1.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" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="   
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    
http://www.springframework.org/schema/util classpath:/org/springframework/beans/factory/xml/spring-util-3.0.xsd
 "
	default-autowire="byName">
	<bean id="paymentSqlSqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="classpath:sqlmap/sqlserver/sqlserver-consume.xml" />
		<property name="dataSource" ref="paymentSqlDataSource" />
	</bean>

	<!-- ============================ payment ======================================= -->
	<bean id="paymentSqlDataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${sqlserver.jdbc.driver}" />
		<property name="url" value="${payment.sqlserver.jdbc.url}" />
		<property name="username" value="${payment.sqlserver.jdbc.username}" />
		<property name="password" value="${payment.sqlserver.jdbc.password}" />
		<property name="maxActive" value="30" />
		<property name="initialSize" value="2" />
		<property name="maxWait" value="60000" />
		<property name="maxIdle" value="30" />
		<property name="minIdle" value="1" />
		<property name="testOnBorrow" value="false"></property>
		<property name="testWhileIdle" value="true"></property>
		<property name="validationQuery" value="select 1 "></property>
		<property name="timeBetweenEvictionRunsMillis">
			<value>300000</value>
		</property>
		<property name="numTestsPerEvictionRun">
			<value>10</value>
		</property>
		<property name="minEvictableIdleTimeMillis" value="300000"></property>
	</bean>
	 <bean id="consumeTemplate"  class="org.springframework.orm.ibatis.SqlMapClientTemplate" >  
        <property name="sqlMapClient" ref="paymentSqlSqlMapClient" />  
    </bean> 
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="paymentSqlDataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

 2.代码中

package com.jd.consume.domain.consume.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jd.consume.domain.consume.ConsumeChange;
import com.jd.consume.domain.model.ConsumeDetail;
import com.jd.consume.domain.model.OrderTask;
import com.jd.consume.infrastructure.repository.ConsumeRepository;
import com.jd.consume.infrastructure.repository.OrderTaskRepository;

/**
 * 消费额度变更服务
 * @author guanpanpan
 *
 */
@Service(value = "consumeChange")
@Transactional(value = "transactionManager", readOnly = true)
public class ConsumeChangeImpl implements ConsumeChange {
	protected final static Logger log = LoggerFactory.getLogger(ConsumeChangeImpl.class);
	@Resource
	private OrderTaskRepository orderTaskRepository;
	@Resource
	private ConsumeRepository consumeRepository;
	public static boolean throwRuntimeExceptionInTransation = false;

	@Transactional(value = "transactionManager", readOnly = false, rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void addConsumeWithSingleTask(OrderTask orderTask) {
		ConsumeDetail consumeDetail = orderTask.genAddConsumeDetail();
		consumeRepository.addConsumeDetail(consumeDetail);
		consumeRepository.addConsumeAmount(consumeDetail);
		//设置状态为已处理,如果失败会在回滚中置好状态
		orderTaskRepository.updateCusumeStatus(orderTask.getId(), OrderTask.SYN_COMPLETE, OrderTask.SYN_LOCK);
		if (throwRuntimeExceptionInTransation) {
			throw new RuntimeException("rollBack in Test");
		}
	}

	@Override
	@Transactional(value = "transactionManager", readOnly = false, rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void reduceConsumeWithSingleTask(OrderTask orderTask) {
		ConsumeDetail consumeDetail = orderTask.genPastOrderTask();
		//设置状态为已处理,如果失败会在回滚中置好状态
		orderTaskRepository.updatePastCusumeStatus(orderTask.getId(), OrderTask.PAST_DUE_YET, OrderTask.PAST_LOCK);
		consumeRepository.addConsumeDetail(consumeDetail);
		consumeRepository.addConsumeAmount(consumeDetail);
		if (throwRuntimeExceptionInTransation) {
			throw new RuntimeException("rollBack in Test");
		}

	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics