数据库链接池--简单的理解

commons-logging这个包可以不要

package rocky.pool.dbcp;

import java.sql.Connection;
import java.sql.SQLException;


import org.apache.commons.dbcp.BasicDataSource;


public class TestDbcp
{
    
   public static void main(String[] args) throws SQLException
{
       //获得数据源对象
       //dataSource(标准接口) basicDataSource(apache的实现)
       BasicDataSource ds = new BasicDataSource();
       ds.setUrl("jdbc:mysql://localhost:3306/hibernate");
       ds.setDriverClassName("com.mysql.jdbc.Driver");
       ds.setUsername("root");
       ds.setPassword("2011211961");
       
       //ds.setMaxActive(4);//最大的活动数
       //ds.setMaxIdle(2);//最大保存数
       //ds.setMaxWait(10000);//最大等待时间
       Connection conn1 = ds.getConnection();
       System.out.println("conn ->"+conn1);
       Connection conn2 = ds.getConnection();
       System.out.println("conn ->"+conn2);
       Connection conn3 = ds.getConnection();
       System.out.println("conn ->"+conn3);
       
       conn1.close();
       conn2.close();
       conn3.close();
       System.out.println("----------------------");
       Connection conn4 = ds.getConnection();
       System.out.println("conn ->"+conn4);
       Connection conn5 = ds.getConnection();
       System.out.println("conn ->"+conn5);
       Connection conn6 = ds.getConnection();
       System.out.println("conn ->"+conn6);
              conn4.close();
       conn5.close();
       conn6.close(); } }

这段代码的运行结果

conn ->org.apache.commons.dbcp.PoolableConnection@4f8e5cde
conn ->org.apache.commons.dbcp.PoolableConnection@45fe3ee3
conn ->org.apache.commons.dbcp.PoolableConnection@73a8dfcc
----------------------
conn ->org.apache.commons.dbcp.PoolableConnection@73a8dfcc
conn ->org.apache.commons.dbcp.PoolableConnection@45fe3ee3
conn ->org.apache.commons.dbcp.PoolableConnection@4f8e5cde

现在来看看,前三个就像是不是关于中间的虚线轴对称啊,有没有想到栈啊

程序先创建三个链接,

其次,打印conn

执行相关操作忽略

再次,关闭conn,

第二次获取连接,打印

第二次打印出来的第1个连接是上次关闭 之前的第3个

第二次打印出来的第2个连接是上次关闭 之前的第2个

第二次打印出来的第3个连接是上次关闭 之前的第1个

,连接池就像一个堆栈.先创建的现存,后关闭的存放在栈顶

 

public static void main(String[] args) throws SQLException
{
       //获得数据源对象
       //dataSource(标准接口) basicDataSource(apache的实现)
       BasicDataSource ds = new BasicDataSource();
       ds.setUrl("jdbc:mysql://localhost:3306/hibernate");
       ds.setDriverClassName("com.mysql.jdbc.Driver");
       ds.setUsername("root");
       ds.setPassword("2011211961");
       
       ds.setMaxActive(4);//最大的活动数
       ds.setMaxIdle(2);//最大保存数
       ds.setMaxWait(10000);//最大等待时间
       Connection conn1 = ds.getConnection();
       System.out.println("conn ->"+conn1);
       Connection conn2 = ds.getConnection();
       System.out.println("conn ->"+conn2);
       Connection conn3 = ds.getConnection();
       System.out.println("conn ->"+conn3);
       
       conn1.close();
       conn2.close();
       conn3.close();
       System.out.println("----------------------");
       Connection conn4 = ds.getConnection();
       System.out.println("conn ->"+conn4);
       Connection conn5 = ds.getConnection();
       System.out.println("conn ->"+conn5);
       Connection conn6 = ds.getConnection();
       System.out.println("conn ->"+conn6);
       conn4.close();
       conn5.close();
       conn6.close(); }

运行结果

conn ->org.apache.commons.dbcp.PoolableConnection@4f8e5cde
conn ->org.apache.commons.dbcp.PoolableConnection@45fe3ee3
conn ->org.apache.commons.dbcp.PoolableConnection@73a8dfcc
----------------------
conn ->org.apache.commons.dbcp.PoolableConnection@45fe3ee3
conn ->org.apache.commons.dbcp.PoolableConnection@4f8e5cde
conn ->org.apache.commons.dbcp.PoolableConnection@1c655221

最大保存数

这里设置了最大保存数,运行结果就变了,其中只有两个是相同的,如果获取次数 大于最大保存数时就不能从数据库连接池里面拿来,只能重新创建一个

 

最大活动数和最大等待时间数,

当最大保存数已经达到了,并且设置最大活动数,同时设置了最大连接时间,当数据库连接池里面没有可以用conn时就等待其他地方关闭的conn,当超过最大等待时间数时就报异常了

public static void main(String[] args) throws SQLException
{
       //获得数据源对象
       //dataSource(标准接口) basicDataSource(apache的实现)
       BasicDataSource ds = new BasicDataSource();
       ds.setUrl("jdbc:mysql://localhost:3306/hibernate");
       ds.setDriverClassName("com.mysql.jdbc.Driver");
       ds.setUsername("root");
       ds.setPassword("2011211961");
       
       ds.setMaxActive(3);//最大的活动数
       ds.setMaxIdle(2);//最大保存数
       ds.setMaxWait(5000);//最大等待时间
       Connection conn1 = ds.getConnection();
       System.out.println("conn ->"+conn1);
       Connection conn2 = ds.getConnection();
       System.out.println("conn ->"+conn2);
       Connection conn3 = ds.getConnection();
       System.out.println("conn ->"+conn3);
       
       conn1.close();
       conn2.close();
       conn3.close();
       System.out.println("----------------------");
       Connection conn4 = ds.getConnection();
       System.out.println("conn ->"+conn4);
       Connection conn5 = ds.getConnection();
       System.out.println("conn ->"+conn5);
       Connection conn6 = ds.getConnection();
       System.out.println("conn ->"+conn6);
       Connection conn7 = ds.getConnection();
       System.out.println("conn ->"+conn7);
       conn4.close();
       conn5.close();
       conn6.close();
       conn7.close();
       
}

等待超过5000毫秒时就会报异常

conn ->org.apache.commons.dbcp.PoolableConnection@4f8e5cde
conn ->org.apache.commons.dbcp.PoolableConnection@45fe3ee3
conn ->org.apache.commons.dbcp.PoolableConnection@73a8dfcc
----------------------
conn ->org.apache.commons.dbcp.PoolableConnection@45fe3ee3
conn ->org.apache.commons.dbcp.PoolableConnection@4f8e5cde
conn ->org.apache.commons.dbcp.PoolableConnection@1c655221
Exception in thread "main" org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
    at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:103)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:544)
    at rocky.pool.dbcp.TestDbcp.main(TestDbcp.java:43)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
    at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:756)
    at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
    ... 2 more

 

如何在tomcat里面配置数据库连接池,

1.在tomcat\conf\context.xml文件里面添加

2.将上述的三个jar包放进去tomcat的lib

 

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

<Resource name="ds_mysql"
      type="javax.sql.DataSource"
      maxActive="100" maxIdle="50" maxWait="5000"
      username="root" password="root"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://127.0.0.1:3306/tableName"/>
</Context>

 

posted @ 2016-01-02 12:04  牵牛花  阅读(313)  评论(0编辑  收藏  举报