Transaction
Based on the new features of YMPv2.0, the JDBC module is more flexible in processing database transactions. Any object managed by the class object manager can support transactions through the @Transaction annotation;
@Transaction annotation:
Parameter description:
value: transaction type (refer to JDBC transaction type), the default is JDBC.TRANSACTION.READ_COMMITTED ;
Usage:
First, the class object that requires database transaction support must declare the @Transaction annotation;
Then , add the @Transaction annotation on the class method that specifically needs to enable transaction processing;
- ## Transaction sample code:
public interface IUserService { User doGetUser(String username, String pwd); boolean doLogin(String username, String pwd); } @Bean @Transaction public class UserService implements IUserService { public User doGetUser(final String username, final String pwd) { return JDBC.get().openSession(new ISessionExecutor<User>() { public User execute(ISession session) throws Exception { Cond _cond = Cond.create().eq("username").param(username).eq("pwd").param(pwd); return session.findFirst(EntitySQL.create(User.class), Where.create(_cond)); } }); } @Transaction public boolean doLogin(String username, String pwd) { User _user = doGetUser(username, pwd); if (_user != null) { _user.setLastLoginTime(System.currentTimeMillis()); _user.update(); // return true; } return false; } } @Bean public class TransDemo { @Inject private IUserService __userService; public boolean testTrans() { return __userService.doLogin("suninformation", "123456"); } public static void main(String[] args) throws Exception { YMP.get().init(); try { TransDemo _demo = YMP.get().getBean(TransDemo.class); _demo.testTrans(); } finally { YMP.get().destroy(); } } }