博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PooledDataSource--mybatis-3-mybatis-3.2.3
阅读量:6173 次
发布时间:2019-06-21

本文共 6373 字,大约阅读时间需要 21 分钟。

 

 

org.apache.ibatis.executor.SimpleExecutor

public 
List
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.
query(stmt, resultHandler); } finally { closeStatement(stmt); } } public List
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;  }

 

转载于:https://www.cnblogs.com/softidea/p/5770864.html

你可能感兴趣的文章
Nginx 编译安装
查看>>
git Unstaged changes after reset
查看>>
一些时兴的IT技术
查看>>
我的友情链接
查看>>
从helloworld开始
查看>>
Spring + JUNIT4 + JPA/Hibernate + H2 集成测试
查看>>
AIX 简单维护手册
查看>>
rtmfp、p2p聊天工具
查看>>
一次故障排查经过
查看>>
php环境变化引起的"syntax error unexpected $end"
查看>>
rpm包管理以及前端工具yum
查看>>
Perl 学习笔记
查看>>
我可能遇到了假的父母
查看>>
通过iscsi配置在aix上挂载存储设备
查看>>
旧IO(java.io.*)和新IO(java.nio.*)的主要区别
查看>>
String不可变类
查看>>
开发秘籍——单元测试的迷惑与思考
查看>>
运算符笔记
查看>>
安卓工业平板电脑的WIFI怎么样使用
查看>>
java 面向对象之多态
查看>>