`

spring连接池配置详解

阅读更多

一、连接池概述

 数据库连接池概述:

  数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出。对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标。数据库连接池正是针对这个问题提出来的。

  数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而再不是重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。

  数据库连接池在初始化时将创建一定数量的数据库连接放到连接池中,这些数据库连接的数量是由最小数据库连接数来设定的。无论这些数据库连接是否被使用,连接池都将一直保证至少拥有这么多的连接数量。连接池的最大数据库连接数量限定了这个连接池能占有的最大连接数,当应用程序向连接池请求的连接数超过最大连接数量时,这些请求将被加入到等待队列中。数据库连接池的最小连接数和最大连接数的设置要考虑到下列几个因素:

  1) 最小连接数是连接池一直保持的数据库连接,所以如果应用程序对数据库连接的使用量不大,将会有大量的数据库连接资源被浪费;

  2) 最大连接数是连接池能申请的最大连接数,如果数据库连接请求超过此数,后面的数据库连接请求将被加入到等待队列中,这会影响之后的数据库操作。

  3) 如果最小连接数与最大连接数相差太大,那么最先的连接请求将会获利,之后超过最小连接数量的连接请求等价于建立一个新的数据库连接。不过,这些大于最小连接数的数据库连接在使用完不会马上被释放,它将被放到连接池中等待重复使用或是空闲超时后被释放。

目前常用的连接池有:C3P0、DBCP、Proxool

网上的评价是:

C3P0比较耗费资源,效率方面可能要低一点。

DBCP在实践中存在BUG,在某些种情会产生很多空连接不能释放,Hibernate3.0已经放弃了对其的支持。

Proxool的负面评价较少,现在比较推荐它,而且它还提供即时监控连接池状态的功能,便于发现连接泄漏的情况。

配置如下:

1、在spring配置文件中,一般在applicationContext.xml中

     <bean id="DataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="shutdown">
  <property name="driver">
   <value>oracle.jdbc.driver.OracleDriver</value>
  </property>
  <property name="driverUrl">
    <value>jdbc:oracle:thin:xxxx/xxxx@192.168.0.XX:1521:server</value>
  </property>
  <property name="user">
   <value>xxxx</value>
  </property>
  <property name="password">
   <value>xxxx</value>
  </property>
  <property name="alias">
   <value>server</value>
  </property>
  <property name="houseKeepingSleepTime">
   <value>30000</value>
  </property>
  <property name="houseKeepingTestSql">
   <value>select 1 from dual</value>
  </property>
  <property name="testBeforeUse">
   <value>true</value>
  </property>
  <property name="testAfterUse">
   <value>true</value>
  </property>
  <property name="prototypeCount">
   <value>5</value>
  </property>
  <property name="maximumConnectionCount">
   <value>400</value>
  </property>
  <property name="minimumConnectionCount">
   <value>10</value>
  </property>
  <property name="statistics">
   <value>1m,15m,1d</value>
  </property>
  <property name="statisticsLogLevel">
   <value>ERROR</value>
  </property>
  <property name="trace">
   <value>true</value>
  </property>
  <property name="verbose">
   <value>false</value>
  </property>
  <property name="simultaneousBuildThrottle">
   <value>1600</value>
  </property>
  <property name="maximumActiveTime">
   <value>600000</value>
  </property>
  <property name="jmx">
   <value>false</value>
  </property>
 </bean>

     然后注入到sessionFactory中

     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="dataSource" ref="DataSource"/>

     </bean>

摘自:http://hi.baidu.com/pj19830204/blog/item/4ddf6a344a3406305ab5f5d4.html

属性列表说明:

fatal-sql-exception: 它是一个逗号分割的信息片段.当一个SQL异常发生时,他的异常信息将与这个信息片段进行比较.如果在片段中存在,那么这个异常将被认为是个致命错误(Fatal SQL Exception ).这种情况下,数据库连接将要被放弃.无论发生什么,这个异常将会被重掷以提供给消费者.用户最好自己配置一个不同的异常来抛出.

fatal-sql-exception-wrapper-class:正如上面所说,你最好配置一个不同的异常来重掷.利用这个属性,用户可以包装SQLException,使他变成另外一个异常.这个异常或者继承QLException或者继承字RuntimeException.proxool自带了2个实现:'org.logicalcobwebs.proxool.FatalSQLException'和'org.logicalcobwebs.proxool.FatalRuntimeException'.后者更合适.

house-keeping-sleep-time: house keeper 保留线程处于睡眠状态的最长时间,house keeper 的职责就是检查各个连接的状态,并判断是否需要销毁或者创建.

house-keeping-test-sql:  如果发现了空闲的数据库连接.house keeper 将会用这个语句来测试.这个语句最好非常快的被执行.如果没有定义,测试过程将会被忽略。

injectable-connection-interface: 允许proxool实现被代理的connection对象的方法.

injectable-statement-interface: 允许proxool实现被代理的Statement 对象方法.

injectable-prepared-statement-interface: 允许proxool实现被代理的PreparedStatement 对象方法.

injectable-callable-statement-interface: 允许proxool实现被代理的CallableStatement 对象方法.

jmx: 如果属性为true,就会注册一个消息Bean到jms服务,消息Bean对象名: "Proxool:type=Pool, name=<alias>". 默认值为false.

jmx-agent-id: 一个逗号分隔的JMX代理列表(如使用MBeanServerFactory.findMBeanServer(String agentId)注册的连接池。)这个属性是仅当"jmx"属性设置为"true"才有效。所有注册jmx服务器使用这个属性是不确定的

jndi-name: 数据源的名称

maximum-active-time: 如果housekeeper 检测到某个线程的活动时间大于这个数值.它将会杀掉这个线程.所以确认一下你的服务器的带宽.然后定一个合适的值.默认是5分钟.

maximum-connection-count: 最大的数据库连接数.

maximum-connection-lifetime: 一个线程的最大寿命.

minimum-connection-count: 最小的数据库连接数

overload-without-refusal-lifetime: 这可以帮助我们确定连接池的状态。如果我们已经拒绝了一个连接在这个设定值(毫秒),然后被认为是超载。默认为60秒。

prototype-count: 连接池中可用的连接数量.如果当前的连接池中的连接少于这个数值.新的连接将被建立(假设没有超过最大可用数).例如.我们有3个活动连接2个可用连接,而我们的prototype-count是4,那么数据库连接池将试图建立另外2个连接.这和 minimum-connection-count不同. minimum-connection-count把活动的连接也计算在内.prototype-count 是spare connections 的数量.

recently-started-threshold: 这可以帮助我们确定连接池的状态,连接数少还是多或超载。只要至少有一个连接已开始在此值(毫秒)内,或者有一些多余的可用连接,那么我们假设连接池是开启的。默认为60秒

simultaneous-build-throttle: 这是我们可一次建立的最大连接数。那就是新增的连接请求,但还没有可供使用的连接。由于连接可以使用多线程,在有限的时间之间建立联系从而带来可用连接,但是我们需要通过一些方式确认一些线程并不是立即响应连接请求的,默认是10。

statistics:  连接池使用状况统计。 参数“10s,1m,1d”

statistics-log-level: 日志统计跟踪类型。 参数“ERROR”或 “INFO”

test-before-use: 如果为true,在每个连接被测试前都会服务这个连接,如果一个连接失败,那么将被丢弃,另一个连接将会被处理,如果所有连接都失败,一个新的连接将会被建立。否则将会抛出一个SQLException异常。

test-after-use: 如果为true,在每个连接被测试后都会服务这个连接,使其回到连接池中,如果连接失败,那么将被废弃。

trace: 如果为true,那么每个被执行的SQL语句将会在执行期被log记录(DEBUG LEVEL).你也可以注册一个ConnectionListener (参看ProxoolFacade)得到这些信息.

verbose: 详细信息设置。 参数 bool 值

英文文档:
fatal-sql-exception:
A comma separated list of message fragments. When an SQLException occurs its message is compared to each of these fragments. If it contains any of them (case sensitive) then it is detected as a Fatal SQL Exception. This causes that connection to be discarded. Regardless of what happens, the exception is thrown again so that the user knows what has happened. You can optionally configure a different exception to be thrown (see fatal-sql-exception-wrapper-class property) Default is null.
fatal-sql-exception-wrapper-class:
If you have configured fatal-sql-exception then the default behaviour is to discard the exception that causes the fatal SQLException and then just throw the original exception to the user. Using this property you can wrap the SQLException up inside another exception. This exception can be anything you want as long as it either extends SQLException or RuntimeException. Proxool provides two classes which you can use if you don't want to build your own: FatalSQLException and FatalRuntimeException. To make use of those you should set this property to either 'org.logicalcobwebs.proxool.FatalSQLException' or 'org.logicalcobwebs.proxool.FatalRuntimeException' as appropriate. Default is null (fatal SQLExceptions are not wrapped). Default is null.
house-keeping-sleep-time:
How long the house keeping thread sleeps for (milliseconds). The house keeper is responsible for checking the state of all the connections and tests whether any need to be destroyed or created. Default is 30 seconds.
house-keeping-test-sql:
If the house keeping thread finds and idle connections it will test them with this SQL statement. It should be very quick to execute. Something like checking the current date or something. If not defined then this test is omitted.
injectable-connection-interface:
Allows Proxool to implement methods defined in the delegate Connection object. See Injectable Interfaces.
injectable-statement-interface:
Allows Proxool to implement methods defined in the delegate Statement object. See Injectable Interfaces.
injectable-prepared-statement-interface:
Allows Proxool to implement methods defined in the delegate PreparedStatement object. See Injectable Interfaces.
injectable-callable-statement-interface:
Allows Proxool to implement methods defined in the delegate CallableStatement object. See Injectable Interfaces.
jmx:
If true the pool will be registered as an MBean to a JMX server with the following object name: "Proxool:type=Pool, name=<alias>". Default is false.
jmx-agent-id:
A comma separated list of JMX agent ids (as used by MBeanServerFactory.findMBeanServer(String agentId) ) to register the pool to. This property is only used if the "jmx" property is set to "true". All registered JMX servers will be used if this property is not set.
jndi-name:
See DataSource.
maximum-active-time:
If the housekeeper comes across a thread that has been active for longer than this then it will kill it. So make sure you set this to a number bigger than your slowest expected response! Default is 5 minutes.
maximum-connection-count:
The maximum number of connections to the database. Default is 15.
maximum-connection-lifetime:
The maximum amount of time that a connection exists for before it is killed (milliseconds). Default is 4 hours.
maximum-new-connections:
Deprecated. Use simultaneous-build-throttle instead.
minimum-connection-count:
The minimum number of connections we will keep open, regardless of whether anyone needs them or not. Default is 5.
overload-without-refusal-lifetime:
This helps us determine the pool status. If we have refused a connection within this threshold (milliseconds) then we are overloaded. Default is 60 seconds.
prototype-count:
If there are fewer than this number of connections available then we will build some more (assuming the maximum-connection-count is not exceeded). For example. Of we have 3 active connections and 2 available, but our prototype-count is 4 then it will attempt to build another 2. This differs from minimum-connection-count because it takes into account the number of active connections. minimum-connection-count is absolute and doesn't care how many are in use. prototype-count is the number of spare connections it strives to keep over and above the ones that are currently active. Default is 0.
recently-started-threshold:
This helps us determine whether the pool status is up, down or overloaded. As long as at least one connection was started within this threshold (milliseconds) or there are some spare connections available then we assume the pool is up. Default is 60 seconds.
simultaneous-build-throttle:
This is the maximum number of connections we can be building at any one time. That is, the number of new connections that have been requested but aren't yet available for use. Because connections can be built using more than one thread (for instance, when they are built on demand) and it takes a finite time between deciding to build the connection and it becoming available we need some way of ensuring that a lot of threads don't all decide to build a connection at once. (We could solve this in a smarter way - and indeed we will one day) Default is 10.
statistics:
The sample length when taking statistical information, comma-delimited. For example: '10s,15m' would mean take samples every 10 seconds and every 15 minutes. Valid units are s(econds), m(inutes), h(ours) and d(ays). Default is null (no statistics).
statistics-log-level:
Whether statistics are logged as they are produced. Range: DEBUG, INFO, WARN, ERROR, FATAL. Not to be confused with the level used with the general log. You have to configure that separately. Default is null (no logging).
test-before-use:
If you set this to true then each connection is tested (with whatever is defined in house-keeping-test-sql) before being served. If a connection fails then it is discarded and another one is picked. If all connections fail a new one is built. If that one fails then you get an SQLException saying so.
test-after-use:
If you set this to true then each connection is tested (with whatever is defined in house-keeping-test-sql) after it is closed (that is, returned to the connection pool). If a connection fails then it is discarded.
trace:
If true then each SQL call gets logged (DEBUG level) along with the execution time. You can also get this information by registering a ConnectionListener (see ProxoolFacade). Default is false.
verbose:
Either false (quiet) or true (loud). Default is false.

摘自:http://wallimn.iteye.com/blog/486550

分享到:
评论

相关推荐

    spring连接池配置

    ### Spring框架中C3P0连接池配置详解 在Java Web开发中,数据库连接管理是一项重要的任务。不当的连接管理可能会导致性能问题甚至系统崩溃。Spring框架提供了多种配置数据库连接池的方式,其中C3P0是一种广泛使用的...

    Tomcat连接池配置.doc

    Tomcat 连接池配置详解 Tomcat 连接池配置是 Web 应用程序中一个非常重要的组件,它负责管理和维护数据库连接,确保数据访问的高速和安全性。本文将详细介绍 Tomcat 连接池配置的步骤和原理,帮助读者快速掌握 ...

    Spring下配置几种常用连接池及配置详解

    本篇文章将详细探讨在Spring环境下配置几种常用的数据库连接池,并提供配置属性的详尽解析和实例应用。 1. **HikariCP** HikariCP是目前非常流行的一款高性能连接池,以其轻量级、高效而受到广泛赞誉。在Spring中...

    Spring Boot如何使用HikariCP连接池详解

    通过简单的依赖引入和配置,开发者可以快速地在Spring Boot应用中启用HikariCP连接池,提升数据库操作的性能和稳定性。HikariCP的诸多特性,如高效的连接管理和并发控制,使其成为现代Java应用的首选连接池实现。在...

    spring-连接池配置

    ### Spring 数据库连接池配置详解 #### 一、数据库连接池依赖库 为了在Spring框架中使用数据库连接池,我们需要引入以下两个依赖库: 1. **commons-dbcp.jar**:这是Apache Commons DBCP(Database Connection ...

    jsp连接池详解及配置

    **三、连接池配置方法** 常见的JSP连接池实现有DBCP、C3P0、HikariCP等。以下以HikariCP为例,介绍配置步骤: 1. **引入依赖**:在项目中添加HikariCP的依赖库,例如在Maven的pom.xml文件中添加对应的依赖项。 2....

    Springboot多连接池+websocket

    配置多个连接池意味着可以根据不同的业务需求,为不同的数据源设置不同的连接池,例如,对于读写频繁的操作可以配置高性能的连接池,而对于低频操作则可以选择更节省资源的连接池。这需要在`application.properties`...

    JDBC数据源连接池的配置和使用示例

    **JDBC数据源连接池配置与使用详解** 在Java Web应用中,数据库连接的管理是至关重要的。为了提高性能和资源利用率,开发人员通常会使用数据源连接池。本篇文章将详细讲解JDBC数据源连接池的配置和使用,以帮助你更...

    JSP Spring中Druid连接池配置详解

    本文将详细介绍如何在JSP Spring项目中配置和使用Druid连接池。 首先,我们来了解Druid连接池的配置参数。在Spring配置文件中,通过定义一个dataSource bean来配置Druid连接池的实例。这个bean继承自Druid...

    详解SpringBoot配置连接池

    在SpringBoot中配置连接池是优化数据库访问性能的重要环节,本文将详细解析如何在SpringBoot中配置连接池。 首先,SpringBoot默认支持的连接池包括dbcp、dbcp2、tomcat和hikari。其中,Tomcat数据源连接池因其优秀...

    spring 配置文件详解

    Spring 配置文件详解 Spring 配置文件是指-guide Spring 工厂进行 Bean 生产、依赖关系注入(装配)及 Bean 实例分发的“图纸”。Java EE 程序员必须学会并灵活应用这份“图纸”准确地表达自己的“生产意图”。...

    c3p0连接池参数配置

    在IT行业的数据库管理领域,C3P0是一个...以上参数是C3P0连接池配置中最关键的部分,它们直接影响着数据库访问的性能和稳定性。在实际应用中,应根据业务需求和系统环境对这些参数进行细致调整,以达到最佳的运行效果。

    spring boot配置druid连接池的完整步骤

    Spring Boot 配置 Druid 连接池的完整步骤 Spring Boot 是一个基于 Java 的开源框架,用于构建微服务架构的应用程序。 Druid 是一个功能强大且高效的数据库连接池,由阿里巴巴开发,已经在生产环境中广泛应用。 ...

    Hibernate2和Hibernate3连接池配置.doc

    《Hibernate2与Hibernate3连接池配置详解》 在Java的持久层框架中,Hibernate是一个广泛使用的ORM(对象关系映射)工具,它允许开发者将数据库操作转化为对Java对象的操作,极大地提高了开发效率。然而,为了优化...

    Spring Boot与HikariCP:性能卓越的数据库连接池

    ### Spring Boot与HikariCP:性能卓越的数据库连接池 #### 一、引言 在现代软件开发中,数据库连接池技术对于提高应用程序的性能至关重要。尤其在使用Java进行后端开发时,选择合适的数据库连接池对于确保系统稳定...

Global site tag (gtag.js) - Google Analytics