/** * 源码解析: 从数据源创建会话 * * @param execType 执行类型 * @param level 数据库隔离级别 * @param autoCommit 是否自动提交 * @return */ private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { 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); // 源码解析: 创建一个新的sql会话 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(); } }
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { 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); //然后产生一个DefaultSqlSession 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(); } }
protected int batchUpdate(List<T> updateObjects , String statement , Integer batchSize){ SqlSession sqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH , TransactionIsolationLevel.NONE); if (batchSize == null || batchSize == 0){ batchSize = BATCH_SIZE ; } int execCount = 0; for (int i = 0 , size = updateObjects.size(); i < size ; i++){ sqlSession.update(statement, updateObjects); if ((i != 0 && i % batchSize == 0) || i == size -1){ List<BatchResult> batchResults = sqlSession.flushStatements(); execCount = batchResults.size() ; sqlSession.commit(); sqlSession.clearCache(); LOGGER.debug("batch insert , current commit size : {} " , execCount ); } } sqlSession.close(); return execCount; }
protected int batchDelete(List<T> delObjects , String statement , Integer batchSize){ SqlSession sqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH , TransactionIsolationLevel.NONE); if (batchSize == null || batchSize == 0){ batchSize = BATCH_SIZE ; } int execCount = 0; for (int i = 0 , size = delObjects.size(); i < size ; i++){ sqlSession.delete(statement, delObjects); if ((i != 0 && i % batchSize == 0) || i == size -1){ List<BatchResult> batchResults = sqlSession.flushStatements(); execCount = batchResults.size() ; sqlSession.commit(); sqlSession.clearCache(); LOGGER.debug("batch insert , current commit size : {} " , execCount ); } } sqlSession.close(); return execCount; }
@Override public void verify() { SqlSessionManager sqlSessionManager = getInjector().getInstance(SqlSessionManager.class); if (sqlSessionManager != null) { try (SqlSessionManager ssm = sqlSessionManager) { if (!ssm.isManagedSessionStarted()) { ssm.startManagedSession(ExecutorType.SIMPLE, (TransactionIsolationLevel) null); } Connection connection = ssm.getConnection(); if (connection != null) { try (Connection conn = connection) { DatabaseMetaData dmd = conn.getMetaData(); if (dmd != null) { if (!compareBeanToTable(dmd, getBeanClasses(getConfig()))) { throw new IllegalStateException("compareBeanToTable returns false."); } } ssm.rollback(true); } catch (SQLException e) { throw new RuntimeException(e); } } } } }
@Override public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { // Silently ignores autocommit and isolation level, as managed transactions are entirely // controlled by an external manager. It's silently ignored so that // code remains portable between managed and unmanaged configurations. return new ManagedTransaction(ds, level, closeConnection); }
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { 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(); } }
@Transactional( executorType = ExecutorType.BATCH, isolationLevel = TransactionIsolationLevel.READ_UNCOMMITTED, exceptionMessage = "Something went wrong while inserting nub relations batch for dataset {0}" ) private void insertNubRelationBatch(UUID datasetKey, Map<Integer, Integer> relations, Iterable<Integer> usageKeyBatch) { for (Integer usageKey : usageKeyBatch) { Set<NameUsageIssue> issues = nameUsageMapper.getIssues(usageKey).getIssues(); if (relations.get(usageKey) == null) { // no match, add issue if not existing yet if (!issues.contains(NameUsageIssue.BACKBONE_MATCH_NONE)) { issues.add(NameUsageIssue.BACKBONE_MATCH_NONE); nameUsageMapper.updateIssues(usageKey, issues); } } else { if (issues.remove(NameUsageIssue.BACKBONE_MATCH_NONE) || issues.remove(NameUsageIssue.BACKBONE_MATCH_FUZZY)) { nameUsageMapper.updateIssues(usageKey, issues); } nubRelMapper.insert(datasetKey, usageKey, relations.get(usageKey)); // for CoL with its instable ids update source key if (Constants.COL_DATASET_KEY.equals(datasetKey)) { usageMapper.updateSourceTaxonKey(relations.get(usageKey), usageKey); } } } }
@Override public TransactionIsolationLevel isolationLevel() { TransactionIsolationLevel ret = null; if (particular != null) { ret = particular.isolationLevel(); } if ((ret == null) && (setting != null)) { ret = setting.isolationLevel(); } if (ret == null) { ret = def.isolationLevel(); } return ret; }
public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) { dataSource = ds; level = desiredLevel; autoCommmit = desiredAutoCommit; }
@Override public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { return new JdbcTransaction(ds, level, autoCommit); }
public ManagedTransaction(DataSource ds, TransactionIsolationLevel level, boolean closeConnection) { this.dataSource = ds; this.level = level; this.closeConnection = closeConnection; }
@Override public SqlSession openSession(TransactionIsolationLevel level) { return openSessionFromDataSource(configuration.getDefaultExecutorType(), level, false); }
@Override public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) { return openSessionFromDataSource(execType, level, false); }
@Override public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) { return new ReadWriteManagedTransaction(dataSource); }
@Override public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { // 源码解析: 返回JDBC事务 return new JdbcTransaction(ds, level, autoCommit); }
/** * {@inheritDoc} */ public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) { return new SpringManagedTransaction(dataSource); }
/** * 根据DataSource、隔离级别和是否自动提交创建Transacion */ @Override public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { return new JdbcTransaction(ds, level, autoCommit); }
public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) { return new MybatisTransaction(dataSource); }
@Override public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) { return new RoutingSpringManagedTransaction(dataSource); }