PooledDataSource--mybatis-3-mybatis-3.2.3

简介:   org.apache.ibatis.executor.SimpleExecutor public List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler r...

 

 

org.apache.ibatis.executor.SimpleExecutor

  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Statement stmt = null;
    try {
      Configuration configuration = ms.getConfiguration();
      StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, rowBounds, resultHandler, boundSql);
      stmt = prepareStatement(handler, ms.getStatementLog());
      return handler.<E>query(stmt, resultHandler);
    } finally {
      closeStatement(stmt);
    }
  }

  public List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
    return Collections.emptyList();
  }

  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    Connection connection = getConnection(statementLog);
    stmt = handler.prepare(connection);
    handler.parameterize(stmt);
    return stmt;
  }

 

 

org.apache.ibatis.executor.BaseExecutor

  protected Connection getConnection(Log statementLog) throws SQLException {
    Connection connection = transaction.getConnection();
    if (statementLog.isDebugEnabled()) {
      return ConnectionLogger.newInstance(connection, statementLog);
    } else {
      return connection;
    }
  }

 

 

org.apache.ibatis.transaction.jdbc.JdbcTransaction

  protected void openConnection() throws SQLException {
    if (log.isDebugEnabled()) {
      log.debug("Opening JDBC Connection");
    }
    connection = dataSource.getConnection();
    if (level != null) {
      connection.setTransactionIsolation(level.getLevel());
    }
    setDesiredAutoCommit(autoCommmit);
  }

 

 

org.apache.ibatis.datasource.pooled.PooledDataSource

  public Connection getConnection() throws SQLException {
    return popConnection(dataSource.getUsername(), dataSource.getPassword()).getProxyConnection();
  }

 

  private PooledConnection popConnection(String username, String password) throws SQLException {
    boolean countedWait = false;
    PooledConnection conn = null;
    long t = System.currentTimeMillis();
    int localBadConnectionCount = 0;

    while (conn == null) {
      synchronized (state) {
        if (state.idleConnections.size() > 0) {
          // Pool has available connection
          conn = state.idleConnections.remove(0);
          if (log.isDebugEnabled()) {
            log.debug("Checked out connection " + conn.getRealHashCode() + " from pool.");
          }
        } else {
          // Pool does not have available connection
          if (state.activeConnections.size() < poolMaximumActiveConnections) {
            // Can create new connection
            conn = new PooledConnection(dataSource.getConnection(), this);
            @SuppressWarnings("unused")
            //used in logging, if enabled
            Connection realConn = conn.getRealConnection();
            if (log.isDebugEnabled()) {
              log.debug("Created connection " + conn.getRealHashCode() + ".");
            }
          } else {
            // Cannot create new connection
            PooledConnection oldestActiveConnection = state.activeConnections.get(0);
            long longestCheckoutTime = oldestActiveConnection.getCheckoutTime();
            if (longestCheckoutTime > poolMaximumCheckoutTime) {
              // Can claim overdue connection
              state.claimedOverdueConnectionCount++;
              state.accumulatedCheckoutTimeOfOverdueConnections += longestCheckoutTime;
              state.accumulatedCheckoutTime += longestCheckoutTime;
              state.activeConnections.remove(oldestActiveConnection);
              if (!oldestActiveConnection.getRealConnection().getAutoCommit()) {
                oldestActiveConnection.getRealConnection().rollback();
              }
              conn = new PooledConnection(oldestActiveConnection.getRealConnection(), this);
              oldestActiveConnection.invalidate();
              if (log.isDebugEnabled()) {
                log.debug("Claimed overdue connection " + conn.getRealHashCode() + ".");
              }
            } else {
              // Must wait
              try {
                if (!countedWait) {
                  state.hadToWaitCount++;
                  countedWait = true;
                }
                if (log.isDebugEnabled()) {
                  log.debug("Waiting as long as " + poolTimeToWait + " milliseconds for connection.");
                }
                long wt = System.currentTimeMillis();
                state.wait(poolTimeToWait);
                state.accumulatedWaitTime += System.currentTimeMillis() - wt;
              } catch (InterruptedException e) {
                break;
              }
            }
          }
        }
        if (conn != null) {
          if (conn.isValid()) {
            if (!conn.getRealConnection().getAutoCommit()) {
              conn.getRealConnection().rollback();
            }
            conn.setConnectionTypeCode(assembleConnectionTypeCode(dataSource.getUrl(), username, password));
            conn.setCheckoutTimestamp(System.currentTimeMillis());
            conn.setLastUsedTimestamp(System.currentTimeMillis());
            state.activeConnections.add(conn);
            state.requestCount++;
            state.accumulatedRequestTime += System.currentTimeMillis() - t;
          } else {
            if (log.isDebugEnabled()) {
              log.debug("A bad connection (" + conn.getRealHashCode() + ") was returned from the pool, getting another connection.");
            }
            state.badConnectionCount++;
            localBadConnectionCount++;
            conn = null;
            if (localBadConnectionCount > (poolMaximumIdleConnections + 3)) {
              if (log.isDebugEnabled()) {
                log.debug("PooledDataSource: Could not get a good connection to the database.");
              }
              throw new SQLException("PooledDataSource: Could not get a good connection to the database.");
            }
          }
        }
      }

    }

    if (conn == null) {
      if (log.isDebugEnabled()) {
        log.debug("PooledDataSource: Unknown severe error condition.  The connection pool returned a null connection.");
      }
      throw new SQLException("PooledDataSource: Unknown severe error condition.  The connection pool returned a null connection.");
    }

    return conn;
  }

 

相关文章
EMQ
|
存储 人工智能 边缘计算
云边协同架构助力智能工厂视觉 AI 缺陷检测应用构建
打破检测系统和产线自动化设备之间的信息孤岛,构建数据高速通道,为视觉AI缺陷检测算法模型提供数据支撑,实现工厂生产智慧优化。
EMQ
785 1
云边协同架构助力智能工厂视觉 AI 缺陷检测应用构建
|
机器学习/深度学习 缓存 PyTorch
Yolov5如何训练自定义的数据集,以及使用GPU训练,涵盖报错解决
Yolov5如何训练自定义的数据集,以及使用GPU训练,涵盖报错解决
1926 0
|
存储 安全 虚拟化
云数据中心专用处理器CIPU正式发布
"在峰会上正式发布的云数据中心专用处理器CIPU(Cloud infrastructure Processing Units), 作为一款专门为新型云数据中心设计的专用处理器,CIPU的使命就是替代传统CPU,成为云时代数据中心的处理核心。在这个全新体系架构下,CIPU向下对数据中心的计算、存储、网络资源快速云化并进行硬件加速,向上接入飞天云操作系统,将全球数百万台服务器连成一台超级计算机。"
1814 2
云数据中心专用处理器CIPU正式发布
|
缓存 Unix Linux
详解C语言中getchar()和putchar()的使用方法
详解C语言中getchar()和putchar()的使用方法
1766 0
详解C语言中getchar()和putchar()的使用方法
|
存储 弹性计算 编解码
阿里云架构师解读四大主流游戏架构
游戏行业是阿里云最早聚焦的行业之一,近年来游戏行业的变化、云计算产品技术的变化都与日俱进。随着行业业务的变化、技术架构的演进以及阿里云产品的迭代演进,整体的产品技术选型在不同的游戏场景、业务场景也不尽相同。本文将聚焦阿里云弹性计算产品在游戏行业的方案实践经验。
1679 1
阿里云架构师解读四大主流游戏架构
|
Oracle 关系型数据库 MySQL
MySQL数据库简介
MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
4219 3
MySQL数据库简介
|
存储 运维 监控
一个好的持续交付流水线是怎样的?| 研发效能提升36计
有了好的持续部署实践,如何才能规模化落地?承载实践最好的方式就是工具,持续部署过程的最好载体就是流水线,因为持续部署本身就是一个逐级递进的流水线,那么一个好的持续交付流水线是怎样的?
1283 1
一个好的持续交付流水线是怎样的?| 研发效能提升36计
|
Kubernetes 程序员 应用服务中间件
Kubernetes深入学习之二:编译和部署镜像(api-server)
在k8s的源码包中,除了kubectl这样的可执行程序,还有api-server、controller-manager这些docker容器,今天的实战是修改这些容器镜像的源码,再部署新的镜像,验证我们修改的代码是否生效
833 0
Kubernetes深入学习之二:编译和部署镜像(api-server)

热门文章

最新文章