SpringBoot 수동 트랜잭션 관리

3299 단어 SpringBoot
springboot 사무 오픈 및 수동 제출
서비스 클래스에 두 개의 주석을 추가해야 한다
@Autowired DataSourceTransactionManager dataSourceTransactionManager; @Autowired TransactionDefinition transactionDefinition;
트랜잭션 TransactionStatus transactionStatus = dataSourceTransactionManager를 수동으로 엽니다.getTransaction(transactionDefinition); 트랜잭션 데이터 SourceTransactionManager를 수동으로 커밋합니다.commit(transactionStatus);//수동 롤백 트랜잭션 데이터 SourceTransactionManager를 커밋합니다.rollback(transactionStatus);//가장 좋은 것은catch에 넣어서 프로그램의 이상을 방지하고, 업무가 어디에 계속 걸려서 제출되지 않는 것을 방지하는 것이다
package com.java.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import com.java.entity.User;
import com.java.service.impl.UserServiceImpl;
@Controller
@RequestMapping(value = "/p")
public class tt {
	@Autowired
	public UserServiceImpl t;
	@Autowired
	DataSourceTransactionManager dataSourceTransactionManager;
	@Autowired
	TransactionDefinition transactionDefinition;
	@RequestMapping(value = "/test")
	public   void  jj(){
		TransactionStatus transactionStatus=null;
		try {
			 transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
			User user=new User();
			user.setId("12");
			user.setUsername("nihao");
			user.setPassword("12346");
			t.insert(user);
			System.out.println(" ");
			dataSourceTransactionManager.commit(transactionStatus);// 
			TransactionStatus transactionStatus2 = dataSourceTransactionManager.getTransaction(transactionDefinition);
			User user2=new User();
			user2.setId("12");
			user2.setUsername("nihao");
			user2.setPassword("12346");
			t.insert(user2);
			dataSourceTransactionManager.commit(transactionStatus2);// 
			
		} catch (Exception e) {
			if(transactionStatus!=null){
				dataSourceTransactionManager.rollback(transactionStatus);
			}
			
			e.printStackTrace();
		}
	}
	@RequestMapping(value = "/test2")
	public   void  jj2(){
		TransactionStatus transactionStatus=null;
		try {
			 transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
			User user=new User();
			user.setId("12");
			user.setUsername("nihao");
			user.setPassword("12346");
			t.insert(user);
			System.out.println(" ");
			/*dataSourceTransactionManager.commit(transactionStatus);// 
			TransactionStatus transactionStatus2 = dataSourceTransactionManager.getTransaction(transactionDefinition);*/
			User user2=new User();
			user2.setId("12");
			user2.setUsername("nihao");
			user2.setPassword("12346");
			t.insert(user2);
			dataSourceTransactionManager.commit(transactionStatus);// 
			
		} catch (Exception e) {
			if(transactionStatus!=null){
				dataSourceTransactionManager.rollback(transactionStatus);
			}
			
			e.printStackTrace();
		}
	}
}

좋은 웹페이지 즐겨찾기