spring事務測試2,為了解決spring事務測試1
controller
@RequestMapping(value = "/test.do")
public void test(Integer[] ids,HttpServletResponse response,Integer type,Boolean isTrx){
System.out.println("是否走事務方法:"+isTrx);
if(!isTrx){//沒有事物
userInfoService2.testNotTrx(type);
}else{//有事務
userInfoService2.test(type );
}
}
service
public interface UserInfoService2 {
//當前方法有事務
public void test(Integer type);
//當前方法有事務
public void testNotTrx(Integer type);
}
public interface UserInfoService3 {
//當前方法有事務
public void test1();
//當前方法有事務
public void test2();
//當前方法有事務
public void test3();
//當前方法有事務
public void test4();
//當前方法有事務
public void test5();
//當前方法有事務
public void test6();
//當前方法有事務
public void test71();
}
serviceImpl
**
* @ClassName UserInfoServiceImpl
* @Description TODO(使用者的基礎資訊)
* @author Administrator
* @Date 2017 年8月3日 下午1:43:49
* @version 1.0.0
*/
@Service
public class UserInfoServiceImpl2 implements UserInfoService2{
/**
* @Field @serialVersionUID : TODO(這裡用一句話描述這個類的作用)
*/
@Autowired
private UserInfoDao userInfoDao;
@Autowired
private UserInfoService3 userInfoService3;
@Transactional//有事務
@Override
public void test(Integer type) {
//資料庫有條資料庫:使用者ID:204,使用者名稱稱userInfoService3.testName1
UserInfo2 u1 = getUser1();
userInfoDao.update2(u1);
System.out.println("事務測試開始");
System.out.println(userInfoDao.findUser2(u1).toString());
// userInfoService3.test當前事務ID
String trx_id=userInfoDao.selectTRX_ID();
System.out.println("test 事務ID:"+trx_id);
if(type==1){
userInfoService3.test1();
}
if(type==2){
userInfoService3.test2();
}
if(type==3){
userInfoService3.test3();
}
if(type==4){
userInfoService3.test4();
}
if(type==5){
userInfoService3.test5();
}
if(type==6){
userInfoService3.test6();
}
if(type==7){
userInfoService3.test71();
}
System.out.println("事務測試結束");
}
@Override//沒有事務
public void testNotTrx(Integer type) {
System.out.println("事務測試開始");
//資料庫有條資料庫:使用者ID:204,使用者名稱稱userInfoService3.testName1
// userInfoService3.test當前事務ID
String trx_id=userInfoDao.selectTRX_ID();
UserInfo2 u1 = getUser1();
userInfoDao.update2(u1);
System.out.println("test事務ID:"+trx_id);
System.out.println(userInfoDao.findUser2(u1));
if(type==1){
userInfoService3.test1();
}
if(type==2){
userInfoService3.test2();
}
if(type==3){
userInfoService3.test3();
}
if(type==4){
userInfoService3.test4();
}
if(type==5){
userInfoService3.test5();
}
if(type==6){
userInfoService3.test6();
}
if(type==7){
userInfoService3.test71();
}
System.out.println("事務測試結束");
}
private UserInfo2 getUser1(){
//資料庫有條資料庫:使用者ID:204,使用者名稱稱userName:getName1
UserInfo2 u1=new UserInfo2();
u1.setUserID(204);
u1.setUserName("getUser1");
return u1;
}
}
/**
* @ClassName UserInfoServiceImpl
* @Description TODO(使用者的基礎資訊)
* @author Administrator
* @Date 2017年8月3日 下午1:43:49
* @version 1.0.0
*/
@Service
public class UserInfoServiceImpl3 implements UserInfoService3{
/**
* @Field @serialVersionUID : TODO(這裡用一句話描述這個類的作用)
*/
@Autowired
private UserInfoDao userInfoDao;
// 事務傳播行為
@Transactional(propagation=Propagation.MANDATORY)
////如果當前存在事務,則加入該事務;如果當前沒有事務,則丟擲異常。
// //適用於更新資料的操作
public void test1(){
UserInfo2 u2 = getUser2();
userInfoDao.update2(u2);
String trx_id=userInfoDao.selectTRX_ID();
System.out.println("test1事務ID:"+trx_id);
System.out.println(userInfoDao.findUser2(u2).toString());
}
@Transactional(propagation=Propagation.REQUIRED)
//如果當前存在事務,則加入該事務;如果當前沒有事務,則建立一個新的事務。這是預設值。
//適用於更新資料的操作
public void test2(){
UserInfo2 u2 = getUser2();
userInfoDao.update2(u2);
String trx_id=userInfoDao.selectTRX_ID();
System.out.println("test2事務ID:"+trx_id);
System.out.println(userInfoDao.findUser2(u2));
}
@Transactional(propagation=Propagation.SUPPORTS)
//如果當前存在事務,則加入該事務;如果當前沒有事務,則以非事務的方式繼續執行。
//適用於不需要事務的環境
public void test3(){
UserInfo2 u2 = getUser2();
userInfoDao.update2(u2);
String trx_id=userInfoDao.selectTRX_ID();
System.out.println("test3事務ID:"+trx_id);
System.out.println(userInfoDao.findUser2(u2));
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
//以非事務方式執行,如果當前存在事務,則把當前事務掛起。
//試用於:當前方法是獨立的事務,注意:當前方法執行完,外部方法的事務是否還存在?
public void test4(){
UserInfo2 u2 = getUser2();
userInfoDao.update2(u2);
String trx_id=userInfoDao.selectTRX_ID();
System.out.println("test4事務ID:"+trx_id);
System.out.println(userInfoDao.findUser2(u2));
}
@Transactional(propagation=Propagation.NEVER)
//以非事務方式執行,如果當前存在事務,則丟擲異常。
public void test5(){
UserInfo2 u2 = getUser2();
userInfoDao.update2(u2);
String trx_id=userInfoDao.selectTRX_ID();
System.out.println("test5事務ID:"+trx_id);
System.out.println(userInfoDao.findUser2(u2));
}
@Transactional(propagation=Propagation.NESTED)
//如果當前存在事務,則建立一個事務作為當前事務的巢狀事務來執行;
//如果當前沒有事務,則該取值等價於Propagation.REQUIRED
public void test6(){
UserInfo2 u2 = getUser2();
userInfoDao.update2(u2);
String trx_id=userInfoDao.selectTRX_ID();
System.out.println("test6事務ID:"+trx_id);
System.out.println(userInfoDao.findUser2(u2));
}
@Transactional(propagation=Propagation.REQUIRES_NEW)
// 方法執行在事務中,則掛起原事務,建立一個獨立的新事物。否則也是建立一個獨立的事務
public void test71(){
UserInfo2 u2 = getUser2();
userInfoDao.update2(u2);
String trx_id=userInfoDao.selectTRX_ID();
System.out.println("test71事務ID:"+trx_id);
System.out.println(userInfoDao.findUser2(u2));
}
private UserInfo2 getUser2(){
//資料庫有條資料庫:使用者ID:204,使用者名稱稱testName1
UserInfo2 u2=new UserInfo2();
u2.setUserID(204);
u2.setUserName("getUser2");
return u2;
}
}
測試 結果
測試:
// 事務傳播行為
@Transactional(propagation=Propagation.MANDATORY)
////如果當前存在事務,則加入該事務;如果當前沒有事務,則丟擲異常。
// //適用於更新資料的操作
public void test1(){
是否走事務方法:true
事務測試開始
UserInfo2 [userID=null, userName=getUser1]
test 事務ID:1344068
test1事務ID:1344068
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
是否走事務方法:false
事務測試開始
test事務ID:null
UserInfo2 [userID=null, userName=getUser1]
2018-04-24 11:52:53,979 cn.xxx.aop.ExceptionLogAspect.afterThrowing(ExceptionLogAspect.java:20) cn.xxx.aop.ExceptionLogAspect
ERROR: at No existing transaction found for transaction marked with propagation 'mandatory'
@Transactional(propagation=Propagation.REQUIRED)
//如果當前存在事務,則加入該事務;如果當前沒有事務,則建立一個新的事務。這是預設值。
//適用於更新資料的操作
public void test2(){
是否走事務方法:true
事務測試開始
UserInfo2 [userID=null, userName=getUser1]
test 事務ID:1344072
test2事務ID:1344072
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
是否走事務方法:false
事務測試開始
test事務ID:null
UserInfo2 [userID=null, userName=getUser1]
test2事務ID:1344076
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
@Transactional(propagation=Propagation.SUPPORTS)
//如果當前存在事務,則加入該事務;如果當前沒有事務,則以非事務的方式繼續執行。
//適用於不需要事務的環境
public void test3(){
是否走事務方法:true
事務測試開始
UserInfo2 [userID=null, userName=getUser1]
test 事務ID:1344082
test3事務ID:1344082
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
是否走事務方法:false
事務測試開始
test事務ID:null
UserInfo2 [userID=null, userName=getUser1]
test3事務ID:null
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
@Transactional(propagation=Propagation.NOT_SUPPORTED)
//以非事務方式執行,如果當前存在事務,則把當前事務掛起。
//試用於:當前方法是獨立的事務,注意:當前方法執行完,外部方法的事務是否還存在?
public void test4(){
是否走事務方法:true
事務測試開始
UserInfo2 [userID=null, userName=getUser1]
test 事務ID:1344084
2018-04-24 11:57:42,951 cn.xxx.aop.ExceptionLogAspect.afterThrowing(ExceptionLogAspect.java:20) cn.xxx.aop.ExceptionLogAspect
ERROR: at
### Error updating database. Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
### The error may involve cn.xxx.core.dao.basicUser.UserInfoDao.update2-Inline
### The error occurred while setting parameters
### SQL: update UserInfo SET UserName=? where UserID=?
### Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
注意:掛起事務失敗
是否走事務方法:false
事務測試開始
test事務ID:null
UserInfo2 [userID=null, userName=getUser1]
test4事務ID:null
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
@Transactional(propagation=Propagation.NEVER)
//以非事務方式執行,如果當前存在事務,則丟擲異常。
public void test5(){
是否走事務方法:true
事務測試開始
UserInfo2 [userID=null, userName=getUser1]
test 事務ID:1344095
2018-04-24 12:00:58,694 cn.xiniu.aop.ExceptionLogAspect.afterThrowing(ExceptionLogAspect.java:20) cn.xiniu.aop.ExceptionLogAspect
ERROR: at Existing transaction found for transaction marked with propagation 'never'
是否走事務方法:false
事務測試開始
test事務ID:null
UserInfo2 [userID=null, userName=getUser1]
test5事務ID:null
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
@Transactional(propagation=Propagation.NESTED)
//如果當前存在事務,則建立一個事務作為當前事務的巢狀事務來執行;
//如果當前沒有事務,則該取值等價於Propagation.REQUIRED
public void test6(){
是否走事務方法:true
事務測試開始
UserInfo2 [userID=null, userName=getUser1]
test 事務ID:1344097
test6事務ID:1344097
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
是否走事務方法:false
事務測試開始
test事務ID:null
UserInfo2 [userID=null, userName=getUser1]
test6事務ID:1344101
UserInfo2 [userID=null, userName=getUser2]
事務測試結束
@Transactional(propagation=Propagation.REQUIRES_NEW)
// 方法執行在事務中,則掛起原事務,建立一個獨立的新事物。否則也是建立一個獨立的事務
public void test71(){
是否走事務方法:true
事務測試開始
UserInfo2 [userID=null, userName=getUser1]
test 事務ID:1344095
2018-04-24 12:00:58,694 cn.xiniu.aop.ExceptionLogAspect.afterThrowing(ExceptionLogAspect.java:20) cn.xiniu.aop.ExceptionLogAspect
ERROR: at Existing transaction found for transaction marked with propagation 'never'
注意:掛起事務失敗
是否走事務方法:false
事務測試開始
test事務ID:null
UserInfo2 [userID=null, userName=getUser1]
test71事務ID:null
UserInfo2 [userID=null, userName=getUser2]
事務測試結束