配置数据源文件spring-config-datasource.xml
<bean id="jrRiskSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${mysql.jdbc.driver}"/>
<property name="url" value="${jdbc.mysql.url}"/>
<property name="username" value="${jdbc.mysql.username}"/>
<property name="password" value="${jdbc.mysql.password}"/>
<property name="maxActive" value="200"/>
<property name="maxIdle" value="30"/>
<property name="validationQueryTimeout" value="2400"/>
<property name="maxWait" value="2000"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="180000"/>
<property name="validationQuery" value="SELECT 1" />
<property name="testOnBorrow" value="true"/>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="jrRiskTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jrRiskSource"/>
</bean>
<tx:annotation-driven transaction-manager="jrRiskTransactionManager" proxy-target-class="true" />
注解使用方式
@Transactional(value = "jrRiskTransactionManager", rollbackFor = {Exception.class}, readOnly = false)
代码实现方式
TransactionStatus trans = null;
int up = -1;
try {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();// 事务定义类
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);//新建事务,如果当前存在事务,把当前事务挂起
trans = transactionManager.getTransaction(def);
up = ruleAtomicDao.update(updates);// 处理业务代码
transactionManager.commit(trans);//提交事务
} catch (Exception e) {
log.error("[doUpdate] update ruleatomic err. updates:{}", JsonUtil.objToStr(updates), e);
transactionManager.rollback(trans);//回滚
throw e;
}
事务的传播机制
https://blog.csdn.net/joenqc/article/details/77141915