版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/y41992910/article/details/88819719
spring和mybatis在自动事物管理下遇错等不会自动回退,需要手动指定回退的条件或者手动事物管理回退等
经过验证情况1:
自动事物管理的情况下,遇到异常try -catch进行处理即可,已经执行的sql修改命令不会回退.
经过验证的情况2
自动事物管理的情况下,抛出异常,已经执行的sql修改命令不会回退.
经过验证3.使用注解式事物管理的情况@Transactional(rollbackFor=Exception.class),抛出异常不会回滚
经过验证,在public方法上用@Transactional(rollbackFor=Exception.class)不起作用
经过验证,在类上用@Transactional(rollbackFor=Exception.class)不起作用
//经过验证4.在使用自动事物管理的情况下,可以手动控制遇错全部回滚;或者正常的提交,错误的回滚
DefaultTransactionDefinition dtd = new DefaultTransactionDefinition();
// 事物的传播行为,使用同一个事物
dtd.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = transactionManager.getTransaction(dtd);
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<value>classpath*:com/xx/portal/db/*.xml</value>
<value>classpath*:com/xx/portal/sql/*.xml</value>
<value>classpath*:com/xx/portal/evidence/sql/*.xml</value>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--给出扫描Dao接口包-->
<property name="basePackage" value="com.itrus.portal.db"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH" />
</bean>
package com.itrus.portal.task;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.itrus.portal.db.Bill;
import com.itrus.portal.service.AgentServiceImpl;
import com.itrus.portal.service.BusinessServiceImpl;
import com.itrus.portal.service.EnterpriseServiceImpl;
import com.itrus.portal.service.OrgCodeServiceImpl;
import com.itrus.portal.service.ProjectServiceImpl;
import com.itrus.portal.utils.LogUtil;
@Service("yihaiKerryService")
@Lazy(false)
public class YihaiKerryServiceTask implements Runnable {
public static final String YI_HAI_KERRY_SERVICE_TASK_LOCK = "YI_HAI_KERRY_SERVICE_TASK_LOCK";
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private SqlSession sqlSession;
@Autowired
OrgCodeServiceImpl orgCodeServiceImpl;
@Autowired
private AgentServiceImpl agentService;
@Autowired
private BusinessServiceImpl businessService;
@Autowired
private EnterpriseServiceImpl enterpriseServiceImpl;
@Autowired
private ProjectServiceImpl projectServiceImpl;
private static Long YI_HAI_JIA_LI_PROJECT_ID = null;
@Scheduled(fixedRate = 1000 * 10)
public void user() {
run();
}
@Override
public void run(){
synchronized (YI_HAI_KERRY_SERVICE_TASK_LOCK) {
List<Bill> bills = sqlSession.selectList("com.itrus.portal.db.BillMapper.selectByExample", null);
int i = 1;
for (Bill bill : bills) {
try {
//验证自动事物的情况下,遇到异常是否会回滚之前的修改
i++;
if (i == 3) {
int j = 1/0;
}
LogUtil.syslog(sqlSession, "推送益海嘉里成功", Thread.currentThread().getName() + bill.getBillId());
bill.setMcstatus(1);
bill.setBillStatus(8);
//经过验证1.在自动事物的管理之下,执行了sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKey", bill);语句之后,对应的数据库的字段属性马上就做了修改了.遇错不会照成全局回滚
//
sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKey", bill);
sqlSession.flushStatements();
continue;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
}
情况2
package com.itrus.portal.task;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.itrus.portal.db.Bill;
import com.itrus.portal.service.AgentServiceImpl;
import com.itrus.portal.service.BusinessServiceImpl;
import com.itrus.portal.service.EnterpriseServiceImpl;
import com.itrus.portal.service.OrgCodeServiceImpl;
import com.itrus.portal.service.ProjectServiceImpl;
import com.itrus.portal.utils.LogUtil;
@Service("yihaiKerryService")
@Lazy(false)
public class YihaiKerryServiceTask implements Runnable {
public static final String YI_HAI_KERRY_SERVICE_TASK_LOCK = "YI_HAI_KERRY_SERVICE_TASK_LOCK";
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private SqlSession sqlSession;
@Autowired
OrgCodeServiceImpl orgCodeServiceImpl;
@Autowired
private AgentServiceImpl agentService;
@Autowired
private BusinessServiceImpl businessService;
@Autowired
private EnterpriseServiceImpl enterpriseServiceImpl;
@Autowired
private ProjectServiceImpl projectServiceImpl;
private static Long YI_HAI_JIA_LI_PROJECT_ID = null;
@Scheduled(fixedRate = 1000 * 10)
public void user() {
run();
}
@Override
public void run(){
synchronized (YI_HAI_KERRY_SERVICE_TASK_LOCK) {
List<Bill> bills = sqlSession.selectList("com.itrus.portal.db.BillMapper.selectByExample", null);
int i = 1;
for (Bill bill : bills) {
//验证自动事物的情况下,遇到异常是否会回滚之前的修改
i++;
if (i == 3) {
int j = 1/0;
}
LogUtil.syslog(sqlSession, "推送益海嘉里成功", Thread.currentThread().getName() + bill.getBillId());
bill.setMcstatus(1);
bill.setBillStatus(8);
//经过验证1.在自动事物的管理之下,执行了sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKey", bill);语句之后,对应的数据库的字段属性马上就做了修改了.遇错不会照成全局回滚
//验证2.抛出异常的情况下,之前的修改是否会回滚.
sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKey", bill);
sqlSession.flushStatements();
continue;
}
}
}
}
情况3
package com.itrus.portal.task;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itrus.portal.db.Bill;
import com.itrus.portal.service.AgentServiceImpl;
import com.itrus.portal.service.BusinessServiceImpl;
import com.itrus.portal.service.EnterpriseServiceImpl;
import com.itrus.portal.service.OrgCodeServiceImpl;
import com.itrus.portal.service.ProjectServiceImpl;
import com.itrus.portal.utils.LogUtil;
@Service("yihaiKerryService")
@Lazy(false)
@Transactional(rollbackFor=Exception.class)
public class YihaiKerryServiceTask{
public static final String YI_HAI_KERRY_SERVICE_TASK_LOCK = "YI_HAI_KERRY_SERVICE_TASK_LOCK";
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private SqlSession sqlSession;
@Autowired
OrgCodeServiceImpl orgCodeServiceImpl;
@Autowired
private AgentServiceImpl agentService;
@Autowired
private BusinessServiceImpl businessService;
@Autowired
private EnterpriseServiceImpl enterpriseServiceImpl;
@Autowired
private ProjectServiceImpl projectServiceImpl;
private static Long YI_HAI_JIA_LI_PROJECT_ID = null;
@Scheduled(fixedRate = 1000 * 10)
public void run(){
synchronized (YI_HAI_KERRY_SERVICE_TASK_LOCK) {
List<Bill> bills = sqlSession.selectList("com.itrus.portal.db.BillMapper.selectByExample", null);
int i = 1;
for (Bill bill : bills) {
//验证自动事物的情况下,遇到异常是否会回滚之前的修改
i++;
if (i == 3) {
int j = 1/0;
}
LogUtil.syslog(sqlSession, "推送益海嘉里成功", Thread.currentThread().getName() + bill.getBillId());
bill.setMcstatus(1);
bill.setBillStatus(8);
//经过验证1.在自动事物的管理之下,执行了sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKey", bill);语句之后,对应的数据库的字段属性马上就做了修改了.遇错不会照成全局回滚
//验证2.抛出异常的情况下,之前的修改是否会回滚.
//经过验证3.使用注解式事物管理的情况@Transactional(rollbackFor=Exception.class),抛出异常不会回滚
//经过验证,在public方法上用@Transactional(rollbackFor=Exception.class)不起作用
//经过验证,在类上用@Transactional(rollbackFor=Exception.class)不起作用
sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKey", bill);
//sqlSession.flushStatements();
continue;
}
}
}
}
情况4
package com.itrus.portal.task;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.itrus.portal.db.Bill;
import com.itrus.portal.service.AgentServiceImpl;
import com.itrus.portal.service.BusinessServiceImpl;
import com.itrus.portal.service.EnterpriseServiceImpl;
import com.itrus.portal.service.OrgCodeServiceImpl;
import com.itrus.portal.service.ProjectServiceImpl;
import com.itrus.portal.utils.LogUtil;
@Service("yihaiKerryService")
@Lazy(false)
@Transactional(rollbackFor=Exception.class)
public class YihaiKerryServiceTask{
public static final String YI_HAI_KERRY_SERVICE_TASK_LOCK = "YI_HAI_KERRY_SERVICE_TASK_LOCK";
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private SqlSession sqlSession;
@Autowired
OrgCodeServiceImpl orgCodeServiceImpl;
@Autowired
private AgentServiceImpl agentService;
@Autowired
private BusinessServiceImpl businessService;
@Autowired
private EnterpriseServiceImpl enterpriseServiceImpl;
@Autowired
private ProjectServiceImpl projectServiceImpl;
private static Long YI_HAI_JIA_LI_PROJECT_ID = null;
@Autowired
private DataSourceTransactionManager transactionManager;
@Scheduled(fixedRate = 1000 * 10)
public void run(){
synchronized (YI_HAI_KERRY_SERVICE_TASK_LOCK) {
List<Bill> bills = sqlSession.selectList("com.itrus.portal.db.BillMapper.selectByExample", null);
int i = 1;
DefaultTransactionDefinition dtd = new DefaultTransactionDefinition();
// 事物的传播行为,使用同一个事物
dtd.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = transactionManager.getTransaction(dtd);
for (Bill bill : bills) {
//验证自动事物的情况下,遇到异常是否会回滚之前的修改
i++;
if (i == 4) {
int j = 1/0;
}
LogUtil.syslog(sqlSession, "推送益海嘉里成功", Thread.currentThread().getName() + bill.getBillId());
bill.setMcstatus(1);
bill.setBillStatus(8);
//经过验证1.在自动事物的管理之下,执行了sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKey", bill);语句之后,对应的数据库的字段属性马上就做了修改了.遇错不会照成全局回滚
//验证2.抛出异常的情况下,之前的修改是否会回滚.
//经过验证3.使用注解式事物管理的情况@Transactional(rollbackFor=Exception.class),抛出异常不会回滚
//经过验证,在public方法上用@Transactional(rollbackFor=Exception.class)不起作用
//经过验证,在类上用@Transactional(rollbackFor=Exception.class)不起作用
//经过验证4.在使用自动事物管理的情况下,可以手动控制遇错全部回滚;或者正常的提交,错误的回滚
sqlSession.update("com.itrus.portal.db.BillMapper.updateByPrimaryKey", bill);
//sqlSession.flushStatements();
continue;
}
transactionManager.commit(status);
}
}
}