编程式事务之手动提交
package com.csw.shuanfa.CodeImprove.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*/
@RestController
@RequestMapping("Transation")
public class PersonTransation {
@Autowired
private PlatformTransactionManager platformTransactionManager;
@RequestMapping("show")
public void show() {
TransactionStatus transactionStatus = platformTransactionManager.getTransaction(new DefaultTransactionDefinition());
try {
System.out.println(">>>>>>数据库操作");
} catch (Exception e) {//放开异常范围
platformTransactionManager.rollback(transactionStatus);
throw e;
}
//全部成功在提交
platformTransactionManager.commit(transactionStatus);
}
}
编程式事务自动提交
package com.csw.shuanfa.CodeImprove.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 【编程式事务】
*/
@RestController
@RequestMapping("TransactionAuto")
public class PersonTransationAutoCommit {
@Autowired
private TransactionTemplate transactionTemplate;
@RequestMapping("show")
public void show(String name) {
System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
System.out.println("======>show...");
transactionTemplate.executeWithoutResult(status -> {//无返回值
System.out.println(">>>>>>数据库操作");
transactionTemplate.execute(status1 -> {//有返回值
System.out.println(">>>>>>数据库操作");
return null;
});
});
}
}
声明式事务
@Transactional(rollbackFor = Exception.class)
如何解决发生的死锁可以看这个//www.greatytc.com/p/5dbcc9ebfd7f