Spring transaction事务之roll back回滚

测试controler

@Controller
public class DemoController2 {
	
 
	@Autowired
    private DemoService demoService;
 
	
	/**
	 * 
	 * @param response
	 * @param resquest
	 */
	@RequestMapping(value = "/demoController2/selectList.do")
	public void selectList(HttpServletResponse response,HttpServletRequest resquest){
		Customer customer=new Customer();
		customer.setApplicationNO("D00021807040005");
		customer.setId_card("11111");
		System.out.println("更新数据开始");
		demoService.update(customer);
		System.out.println("更新数据结束:"+customer.getId_card());
		
	}
	
 
}

情况1

		@Override
	public void update(Customer customer) {
		// TODO Auto-generated method stub
			customerDao.update(customer);
			int i=7/0;
	}	@Override
	public void update(Customer customer) {
		// TODO Auto-generated method stub
			customerDao.update(customer);
			int i=7/0;
	}
 
结果:抛异常,事务由回滚

2018-07-16 10:42:27,628 [http-bio-8080-exec-5] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - ==> Preparing: update ap_customer SET id_card=? where applicationNO=? 2018-07-16 10:42:27,629 [http-bio-8080-exec-5] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - ==> Parameters: 11111(String), D00021807040005(String) 2018-07-16 10:42:27,632 [http-bio-8080-exec-5] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - <== Updates: 1 2018-07-16 10:42:27,632 [http-bio-8080-exec-5] DEBUG [org.mybatis.spring.SqlSessionUtils] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4198553e] 2018-07-16 10:42:27,632 [http-bio-8080-exec-5] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4198553e] 2018-07-16 10:42:27,632 [http-bio-8080-exec-5] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4198553e] 2018-07-16 10:42:27,632 [http-bio-8080-exec-5] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Initiating transaction rollback 2018-07-16 10:42:27,633 [http-bio-8080-exec-5] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Rolling back JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/test_zhou?characterEncoding=UTF-8, UserName=root@localhost, MySQL-AB JDBC Driver]

 

情况2

	@Override
	public void update(Customer customer) throws Exception{//受检异常(非运行异常)必须抛出
		// TODO Auto-generated method stub
			try{
				customerDao.update(customer);
		           int i = 4/0;//人为产生异常
		       }catch(Exception e){
		           throw new Exception ("人为产生异常");//抛出Exception异常
		       }
	}

 

结果:抛出异常,事务没有回滚
2018-07-16 10:59:53,616 [http-bio-8080-exec-6] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - ==>  Preparing: update ap_customer SET id_card=? where applicationNO=? 
  2018-07-16 10:59:53,616 [http-bio-8080-exec-6] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - ==> Parameters: 11111(String), D00021807040005(String)
  2018-07-16 10:59:53,617 [http-bio-8080-exec-6] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - <==    Updates: 1
  2018-07-16 10:59:53,617 [http-bio-8080-exec-6] DEBUG [org.mybatis.spring.SqlSessionUtils] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c87b6f2]
  2018-07-16 10:59:53,617 [http-bio-8080-exec-6] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c87b6f2]
  2018-07-16 10:59:53,618 [http-bio-8080-exec-6] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c87b6f2]
  2018-07-16 10:59:53,618 [http-bio-8080-exec-6] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c87b6f2]
  2018-07-16 10:59:53,618 [http-bio-8080-exec-6] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Initiating transaction commit

情况3

	@Override
	public void update(Customer customer) throws RuntimeException{//运行异常(非受检异常)可以不抛出
		// TODO Auto-generated method stub
			try{
				customerDao.update(customer);
		           int i = 4/0;//人为产生异常
		       }catch(Exception e){
		           throw new RuntimeException ("人为产生异常");//抛出Exception异常
		       }
	}

 

结果:异常抛出,事务回滚
 2018-07-16 11:04:37,304 [http-bio-8080-exec-4] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - ==>  Preparing: update ap_customer SET id_card=? where applicationNO=? 
  2018-07-16 11:04:37,305 [http-bio-8080-exec-4] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - ==> Parameters: 11111(String), D00021807040005(String)
  2018-07-16 11:04:37,306 [http-bio-8080-exec-4] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - <==    Updates: 1
  2018-07-16 11:04:37,307 [http-bio-8080-exec-4] DEBUG [org.mybatis.spring.SqlSessionUtils] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d520a8d]
  2018-07-16 11:04:37,307 [http-bio-8080-exec-4] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d520a8d]
  2018-07-16 11:04:37,307 [http-bio-8080-exec-4] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d520a8d]
  2018-07-16 11:04:37,307 [http-bio-8080-exec-4] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Initiating transaction rollback
  2018-07-16 11:04:37,308 [http-bio-8080-exec-4] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Rolling back JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/test_zhou?characterEncoding=UTF-8, UserName=root@localhost, MySQL-AB JDBC Driver]

情况4

	@Transactional(rollbackFor=Exception.class)
	public void update(Customer customer) throws  Exception{//受检异常(非运行异常)必须抛出
		// TODO Auto-generated method stub
			try{
				customerDao.update(customer);
		           int i = 4/0;//人为产生异常
		       }catch(Exception e){
		           throw new Exception ("人为产生异常");//抛出Exception异常
		       }
	}@Transactional(rollbackFor=Exception.class)
	public void update(Customer customer) throws  Exception{//受检异常(非运行异常)必须抛出
		// TODO Auto-generated method stub
			try{
				customerDao.update(customer);
		           int i = 4/0;//人为产生异常
		       }catch(Exception e){
		           throw new Exception ("人为产生异常");//抛出Exception异常
		       }
	}

 

 结果:抛出异常,事务回滚 2018-07-16 11:21:36,618 [http-bio-8080-exec-2] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - ==>  Preparing: update ap_customer SET id_card=? where applicationNO=? 
  2018-07-16 11:21:36,635 [http-bio-8080-exec-2] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - ==> Parameters: 11111(String), D00021807040005(String)
  2018-07-16 11:21:36,636 [http-bio-8080-exec-2] DEBUG [com.xx.core.dao.pushBusiness.CustomerDao.update] - <==    Updates: 1
  2018-07-16 11:21:36,636 [http-bio-8080-exec-2] DEBUG [org.mybatis.spring.SqlSessionUtils] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5ab43a31]
  2018-07-16 11:21:36,637 [http-bio-8080-exec-2] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5ab43a31]
  2018-07-16 11:21:36,637 [http-bio-8080-exec-2] DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5ab43a31]
  2018-07-16 11:21:36,637 [http-bio-8080-exec-2] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Initiating transaction rollback
  2018-07-16 11:21:36,637 [http-bio-8080-exec-2] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Rolling back JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/test_zhou?characterEncoding=UTF-8, UserName=root@localhost, MySQL-AB JDBC Driver]
  2018-07-16 11:21:36,647 [http-bio-8080-exec-2] DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/test_zhou?characterEncoding=UTF-8, UserName=root@localhost, MySQL-AB JDBC Driver] after transaction
  2018-07-16 11:21:36,647 [http-bio-8080-exec-2] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - 

 

 情况5

Spring transaction事务之roll back回滚_第1张图片

Spring transaction事务之roll back回滚_第2张图片

数据库有数据,没有回滚

 

事务回滚情况:

1运行时异常(untimeException 或其子类例的实例

2unchecked exceptions异常

不回滚情况

1Checked exceptions异常(情况2)

2 Transactional修饰的类,直接new 获取也不回滚

对不回滚的处理方案:

1:手动指定回滚

2 :使用了Springrollback-for配置

 

参考

https://blog.csdn.net/lovejavaydj/article/details/7635848

 

 

 

你可能感兴趣的:(事务)