1,spring Use the form of annotations to manage things. The main code is as follows, the whole project code click here to download.
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 afternoon 8:19:49 * * @Service public class Springtransactionalservi
Ce {@Autowired private rolemapper rolemapper;
@Autowired private Anotherspringtransactionalservice Anothertxservice; /** * No transaction annotations (submitted to database) * @author Huweijun * @date August 10, 2016 afternoon 8:37:57/public void Notransannotat
Ion () {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 (things submitted) * @author Huweijun * @date August 10, 2016 afternoon 8:39:46 * * @Transactional public VO
ID Normalcommit () {Role role = new role ();
Role.setcreatetime (New Date ());
Role.setname ("normal commit");
Rolemapper.insert (role);
/** * Throw exception (things submitted) * @throws Exception * @author Huweijun * @date August 10, 2016 8:40:15
@Transactional public void Commitoncheckedexception () throws Exception {The role role = the new role ();
Role.setname ("Commit on checked exception");
Role.setcreatetime (New Date ());
Rolemapper.insert (role);
throw new Exception ("Rollback checked Exception");
/** * For an exception rollback (things rolled back) * @throws Exception * @author Huweijun * @date August 10, 2016 8:41:13 @Transactional (rollbackfor = Exception.class The public void Rollbackoncheckedexception () throws Exception {The role role = the new role ();
Role.setname ("Roll back on checked exception");
Role.setcreatetime (New Date ());
Rolemapper.insert (role);
throw new Exception ("Rollback checked Exception");
/** * Transaction Method Throw exception (thing not submitted) * @author Huweijun * @date August 10, 2016 afternoon 8:42:14/@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 not submitted) * @author Huweijun * @date August 10, 2016 afternoon 8:49:39/@Transactional
public void Getnotcommitdatainthesametransaction () {Role role = new role ();
Role.setname ("not commits role");
Role.setcreatetime (New Date ()); Rolemapper.inSERT (role);
list<role> roles = Rolemapper.selectrolebyname ("Not commit role");
System.out.println (Roles.get (0). GetName ());//throw new runtimeexception with data ("not commit data"); /** * Transaction method calls another transaction method in the same class (things are not committed) * @author Huweijun * @date August 10, 2016 afternoon 8:50:06/@Transactio nal public void Invokerequirenewtransmethodinsameclass () {Requirenewtransactionmethod ();//Things not submitted throw
New RuntimeException ("Throw trans method in same class exception."); /** * The other transaction method in the other class is invoked in the * method * @author Huweijun * @date August 10, 2016 afternoon 8:56:26 * * * @Transactiona
L public void Invokerequirenewtransmethodinanotherservice () {Role role = new role ();
Role.setname ("Require_new_transaction_in_another_service");
Role.setcreatetime (New Date ()); Rolemapper.insert (role)//Things Not submitted Anothertxservice.requirenewtrans (); Things submitted throw new RuntimeException ("Throw Invoke requIre new trans method in another service "); /** * Open new things * @author Huweijun * @date August 10, 2016 afternoon 8:57:54 * * * @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 afternoon 8:20:23 * * @Service public class Anotherspringtransact
Ionalservice {@Autowired private rolemapper rolemapper; /** * Open a new thing * @author Huweijun * @date August 10, 2016 afternoon 8:57:41/@Transactional (propagation = Pr Opagation.
requires_new) public void Requirenewtrans () {util.printstackinfo ();
Role role = new role ();
Role.setcreatetime (New Date ()); Role.setname ("Anothertxservice RequirE new trans ");
Rolemapper.insert (role);
}
}