執行Mybatis的時候發現的有趣現象
阿新 • • 發佈:2020-11-05
先上程式碼
@Test public void testAddUser() throws InterruptedException{ for (int i = 0;i<20;i++) { User user = new User(); user.setUserId(String.valueOf(i)); // user.setUserName("xdp_gacl_白虎神皇"); user.setUserBirthday(new Date().toString()); user.setUserSalary((double) (10 + i)); userService.addUser(user); } Thread.sleep(5000); }
@Override @Transactional(propagation = Propagation.REQUIRED) public void addUser(User user) { // try { // ((UserServiceI) AopContext.currentProxy()).addUserScore(); // }catch(Exception e) {//// // } userMapper.insert(user); // int t = 1/0; }
有趣的現象就是 我發現每次都會走到 ,注意是每次,說明每次執行userService.addUser(user); 都會重新搞一次SqlSessioin,我猜測是userMapper的insert 都會重新獲取執行
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, booleanautoCommit) { Transaction tx = null; try { final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); final Executor executor = configuration.newExecutor(tx, execType); return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }