MyBatis之PooledDataSource

MyBatis官方文档

MyBatis数据源种类

  • JNDI
  • POOLED
  • UNPOOLED

介绍

  • This is a simple, synchronous, thread-safe database connection pool.

源码

成员变量

 private static final Log log = LogFactory.getLog(PooledDataSource.class);

 private final PoolState state = new PoolState(this);

 private final UnpooledDataSource dataSource;

 // OPTIONAL CONFIGURATION FIELDS
 protected int poolMaximumActiveConnections = 10;
 protected int poolMaximumIdleConnections = 5;
 protected int poolMaximumCheckoutTime = 20000;
 protected int poolTimeToWait = 20000;
 protected int poolMaximumLocalBadConnectionTolerance = 3;
 protected String poolPingQuery = "NO PING QUERY SET";
 protected boolean poolPingEnabled;
 protected int poolPingConnectionsNotUsedFor;

 private int expectedConnectionTypeCode;

成员方法

 /*
  * Closes all active and idle connections in the pool
  */
 public void forceCloseAll() {
   // synchronized给对象加锁
   synchronized (state) {
     expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
     for (int i = state.activeConnections.size(); i > 0; i--) {
       try {
         PooledConnection conn = state.activeConnections.remove(i - 1);
         conn.invalidate();

         Connection realConn = conn.getRealConnection();
         if (!realConn.getAutoCommit()) {
           realConn.rollback();
         }
         realConn.close();
       } catch (Exception e) {
         // ignore
       }
     }
     for (int i = state.idleConnections.size(); i > 0; i--) {
       try {
         PooledConnection conn = state.idleConnections.remove(i - 1);
         conn.invalidate();

         Connection realConn = conn.getRealConnection();
         if (!realConn.getAutoCommit()) {
           realConn.rollback();
         }
         realConn.close();
       } catch (Exception e) {
         // ignore
       }
     }
   }
   if (log.isDebugEnabled()) {
     log.debug("PooledDataSource forcefully closed/removed all connections.");
   }
 }

protected void pushConnection(PooledConnection conn) throws SQLException {

  synchronized (state) {
    state.activeConnections.remove(conn);
    if (conn.isValid()) {
      if (state.idleConnections.size() < poolMaximumIdleConnections && conn.getConnectionTypeCode() == expectedConnectionTypeCode) {
        state.accumulatedCheckoutTime += conn.getCheckoutTime();
        if (!conn.getRealConnection().getAutoCommit()) {
          conn.getRealConnection().rollback();
        }
        PooledConnection newConn = new PooledConnection(conn.getRealConnection(), this);
        state.idleConnections.add(newConn);
        newConn.setCreatedTimestamp(conn.getCreatedTimestamp());
        newConn.setLastUsedTimestamp(conn.getLastUsedTimestamp());
        conn.invalidate();
        if (log.isDebugEnabled()) {
          log.debug("Returned connection " + newConn.getRealHashCode() + " to pool.");
        }
        state.notifyAll();
      } else {
        state.accumulatedCheckoutTime += conn.getCheckoutTime();
        if (!conn.getRealConnection().getAutoCommit()) {
          conn.getRealConnection().rollback();
        }
        conn.getRealConnection().close();
        if (log.isDebugEnabled()) {
          log.debug("Closed connection " + conn.getRealHashCode() + ".");
        }
        conn.invalidate();
      }
    } else {
      if (log.isDebugEnabled()) {
        log.debug("A bad connection (" + conn.getRealHashCode() + ") attempted to return to the pool, discarding connection.");
      }
      state.badConnectionCount++;
    }
  }
}

PooledDataSourceFactory源码

package org.apache.ibatis.datasource.pooled;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;

/**
 * @author Clinton Begin
 */
public class PooledDataSourceFactory extends UnpooledDataSourceFactory {

  public PooledDataSourceFactory() {
    this.dataSource = new PooledDataSource();
  }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值