mybatis 连接池配置

xml形式配置DataSource

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
  <properties resource="/com/cn/cs/config.properties"/>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${oracle.driver}"/>
        <property name="url" value="${oracle.url}"/>
        <property name="username" value="${oracle.username}"/>
        <property name="password" value="${oracle.password}"/>
        <property name="poolMaximumActiveConnections" value="10"/> <!--最大活跃连接数 -->
        <property name="poolMaximumIdleConnections" value="5"/>  <!--最大空闲连接数-->
        <property name="poolTimeToWait" value="20000"/>  <!--创建连接时最大等待时间20s-->
      </dataSource>
    </environment>
  </environments>
  <mappers>
      <mapper resource="/com/cn/cs/mapping/UserCsMapper.xml"/>
  </mappers>
</configuration>
 

mybatis中DataSource节点有一个 type属性. 有三个可选值: pooled(连接池), unpooled(非连接池), jndi

mybatis内部定义了一个DataSourceFactory接口

public interface DataSourceFactory {

  void setProperties(Properties props);

  DataSource getDataSource();

}

根据type 属性值分别对应实现类.

    pooled --> PooledDataSourceFactory

unpooled --> UnpooledDataSourceFactory

  jndi--> JndiDataSourceFactory

这三个实现类都实现了 DataSourceFactory 的 DataSource getDataSource()方法.

PooledDataSourceFactory.getDataSource() 返回   PooledDataSource.

UnpooledDataSourceFactory.getDataSource() 返回  UnpooledDataSource

JndiDataSourceFactory.getDataSource() 返回   环境中的 DataSource

DataSource 将会保存在Environment 的静态内部类Builder中.

连接池的初始化

dataSource 此时只会保存好配置信息.连接池此时并没有创建好连接.只有当程序在调用操作数据库的方法时,才会出示化连接.

管理连接池的类为PooledDateSource  这个类中有一个获取连接的方法.

这个方法会先判断是否有空闲连接,有的话会直接将连接取出来.如果没有则判断当前活跃的连接是否大于最大空闲连接数.如果小于则创建一个新的连接.

否则将旧的连接删掉一个,并创建一个新的连接.

 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.isEmpty()) {
          // 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);
            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()) {
                try {
                  oldestActiveConnection.getRealConnection().rollback();
                } catch (SQLException e) {
                  /*
                     Just log a message for debug and continue to execute the following
                     statement like nothing happened.
                     Wrap the bad connection with a new PooledConnection, this will help
                     to not interrupt current executing thread and give current thread a
                     chance to join the next competition for another valid/good database
                     connection. At the end of this loop, bad {@link @conn} will be set as null.
                   */
                  log.debug("Bad connection. Could not roll back");
                }
              }
              conn = new PooledConnection(oldestActiveConnection.getRealConnection(), this);
              conn.setCreatedTimestamp(oldestActiveConnection.getCreatedTimestamp());
              conn.setLastUsedTimestamp(oldestActiveConnection.getLastUsedTimestamp());
              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) {
          // ping to server and check the connection is valid or not
          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 + poolMaximumLocalBadConnectionTolerance)) {
              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/ldspeace/p/10854422.html

时间: 2024-10-28 14:00:53

mybatis 连接池配置的相关文章

MyBatis连接池分析

连接池在实际的开发当中,我们很多的对数据库的操作的时候都是用到连接池的,数据库的连接池它可以减少我们获取数据库连接的时间. MyBatis连接池在mybatis中给我们提供了三种数据库的连接方式 POOLED 用的是DataSource规范的连接池UNPOOLED 使用的传统的获取连接的方式,没有使用池子的思想JNDI 使用的是服务器提供的技术,来获取datasource对象,不同的服务器拿到的对象是不一样的,只能使用在web和maven的工程在配置文件中配置数据库连接池的位置:在主配置文件中的

DBCP连接池配置参数说明

<!-- 数据源1 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property n

C3P0连接池配置

C3P0是一个开源的JDBC连接池,详情请google. 在spring中,C3P0的一些配置,介绍如下(只列了一部分,不是全部) C3P0更详细的配置项及其含义,请参考:http://www.mchange.com/projects/c3p0/index.html <!-- c3p0连接池配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&g

web 连接池配置

TOMCAT J2EE项目连接池配置 web 项目的 web.xml <web-app> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/oracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Contain

DruidDataSource连接池配置

数据源连接池配置 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"

[JavaEE] Hibernate连接池配置测试

转载自51CTO http://developer.51cto.com/art/200906/129914.htm Hibernate支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以及DBCP.在Hibernate连接池配置时需要注意的有三点: 一.Apche的DBCP在Hibernate2中受支持,但在Hibernate3中已经不再推荐使用,官方的解释是这个连接池存在缺陷.如果你因为某种原因需要在Hibernate3中使用DBCP,建议采用JNDI方式. 二.默认情况下(即没

tomcat oracle 连接池配置

<?xml version='1.0' encoding='utf-8'?> <Context displayName="zcgl" docBase="C:\tjzc\tongji" path="/zcgl" reloadable="true" privileged="true"> <Resource auth="Container" description

数据层spring-dao.xml配置文件中C3P0连接池配置详解

数据层spring-dao.xml配置文件中C3P0连接池配置详解 先放上两种配置文件bean的相关代码,文章下面会有详细的配置属性的讲解,大家可以自己添加配置和修改配置普通单一数据库连接池配置: <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接池属性 --> <property name="driverClas

【Mysql】SpringBoot阿里Druid数据源连接池配置

一.pom.xml添加 <!-- 配置数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.0</version> </dependency> 二.application.properties 添加 # 连接池配置spring.datasource.type