1. Spring uses the form of annotations to manage things. The main code is as follows, the code of the entire project can be downloaded here .
The service code is as follows:
package com.junlenet.spring.transactional.service; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.junlenet.spring.transactional.Util; import com.junlenet.spring.transactional.mappers.RoleMapper; import com.junlenet.spring.transactional.model.Role; /** * spring service * @author huweijun *@date August 10, 2016 at 8:19:49 PM */ @Service public class SpringTransactionalService { @Autowired private RoleMapper roleMapper; @Autowired private AnotherSpringTransactionalService anotherTxService; /** * No transaction annotation (commit to database) * @author huweijun *@date Aug 10, 2016 at 8:37:57 PM */ public void noTransAnnotation() { Role role = new Role(); role.setName("not trans annotation"); role.setCreateTime(new Date()); roleMapper.insert(role); throw new RuntimeException("not trans annotation exception"); } /** * Normal submission (thing submission) * @author huweijun *@date August 10, 2016 at 8:39:46 PM */ @Transactional public void normalCommit() { Role role = new Role(); role.setCreateTime(new Date()); role.setName("normal commit"); roleMapper.insert(role); } /** * throw an exception (transaction commit) * @throws Exception * @author huweijun *@date August 10, 2016 at 8:40:15 PM */ @Transactional public void commitOnCheckedException() throws Exception { Role role = new Role(); role.setName("commit on checked exception"); role.setCreateTime(new Date()); roleMapper.insert(role); throw new Exception("rollback checked exception"); } /** * Rollback for an exception (things are rolled back) * @throws Exception * @author huweijun *@date August 10, 2016 at 8:41:13 PM */ @Transactional(rollbackFor = Exception.class ) public void rollbackOnCheckedException() throws Exception { Role role = new Role(); role.setName("roll back on checked exception"); role.setCreateTime(new Date()); roleMapper.insert(role); throw new Exception("rollback checked exception"); } /** * An exception is thrown in the transaction method (the transaction is not committed) * @author huweijun *@date August 10, 2016 at 8:42:14 PM */ @Transactional public void throwExceptionInMethod() { Role role = new Role(); role.setName("throwExceptionInTransMethod"); role.setCreateTime(new Date()); roleMapper.insert(role); throw new RuntimeException("throw exception in transaction method"); } /** * Get uncommitted data in the same transaction (things are not committed) * @author huweijun *@date August 10, 2016 at 8:49:39 PM */ @Transactional public void getNotCommitDataInTheSameTransaction(){ Role role = new Role(); role.setName("not commit role"); role.setCreateTime(new Date()); roleMapper.insert(role); List<Role> roles = roleMapper.selectRoleByName("not commit role"); System.out.println(roles.get(0).getName());//with data throw new RuntimeException("not commit data"); } /** * The transaction method calls another transaction method in the same class (things are not committed) * @author huweijun *@date August 10, 2016 at 8:50:06 PM */ @Transactional public void invokeRequireNewTransMethodInSameClass() { requireNewTransactionMethod();//Thing not submitted throw new RuntimeException("throw trans method in same class exception."); } /** * Calling another transaction method in another class in a transaction method * @author huweijun * @date August 10, 2016 at 8:56:26 PM */ @Transactional public void invokeRequireNewTransMethodInAnotherService(){ Role role = new Role(); role.setName("require_new_transaction_in_another_service"); role.setCreateTime(new Date()); roleMapper.insert(role);// Things are not submitted anotherTxService.requireNewTrans(); //thing submitted throw new RuntimeException("throw invoke require new trans method in another service"); } /** * Open new things * @author huweijun *@date August 10, 2016 at 8:57:54 PM */ @Transactional(propagation = Propagation.REQUIRES_NEW) public void requireNewTransactionMethod() { Util.printStackInfo(); Role role = new Role(); role.setCreateTime(new Date()); role.setName("require_new_transaction"); roleMapper.insert(role); } }
another service
package com.junlenet.spring.transactional.service; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.junlenet.spring.transactional.Util; import com.junlenet.spring.transactional.mappers.RoleMapper; import com.junlenet.spring.transactional.model.Role; /** * another spring service * @author huweijun *@date August 10, 2016 at 8:20:23 PM */ @Service public class AnotherSpringTransactionalService { @Autowired private RoleMapper roleMapper; /** * Another opens a new thing * @author huweijun *@date Aug 10, 2016 at 8:57:41 PM */ @Transactional(propagation = Propagation.REQUIRES_NEW) public void requireNewTrans(){ Util.printStackInfo(); Role role = new Role(); role.setCreateTime(new Date()); role.setName("anotherTxService require new trans"); roleMapper.insert(role); } }