`
isiqi
  • 浏览: 16538815 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Database Connection Pooling with Tomcat

阅读更多
Software object pooling is not a new concept. There are many scenarios where some type of object pooling technique is employed to improve application performance, concurrency, and scalability. After all, having your database code create a new Connection object on every client request is an expensive process. Moreover, with today's demanding applications, creating new connections for data access from scratch, maintaining them, and tearing down the open connection can lead to massive load on the server.

We can configure a maximum number of DB connections in the pool. Make sure you choose a maximum connection count large enough to handle all of your database connections--alternatively, you can set 0 for no limit. Further, we can set the maximum number of idle database connections to be retained in the pool. Set this value to -1 for no limit. The most optimal performance is attained when the pool in its steady state contains just enough connections to service all concurrent connection requests, without having to create new physical database connections at runtime. We can also specify the maximum time (in milliseconds) to wait for a database connection to become available, which in this example is 10 seconds. An exception is thrown if this timeout is exceeded. You can set this value to -1 to wait indefinitely. Please make sure your connector driver, such as mysql.jar, is placed inside the /common/lib directory of your Tomcat installation.

To achieve performance and high throughput, we also need to fine-tune the container to work under heavy traffic. Here's how we'll configure the Connector element for the maxProcessors and acceptCount parameters in the server.xml file:

<!--  Configuring the request and response endpoints -->
<Connector port="80" maxHttpHeaderSize="8192" maxProcessors="150"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="150"
connectionTimeout="20000" disableUploadTimeout="true" />

Configuring JNDI Reference

In order for JNDI to resolve the reference, we have to insert a <resource-ref> tag into the web.xml deployment descriptor file. We first begin by setting a <listener> tag for registering a ServletContextListener as shown below:


<listener>
<listener-class> com.onjava.dbcp.DBCPoolingListener</listener-class>
</listener>

<!-- This component has a dependency on an external resource-->
<resource-ref>
<description> DB Connection Pooling</description>
<res-ref-name> jdbc/TestDB</res-ref-name>
<res-type> javax.sql.DataSource</res-type>
<res-auth> Container</res-auth>
</resource-ref>

<servlet>
<servlet-name> EnrolledStudents</servlet-name>
<servlet-class> com.onjava.dbcp.CourseEnrollmentServlet</servlet-class>
<load-on-startup> 1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name> EnrolledStudents</servlet-name>
<url-pattern> /enrollment.do</url-pattern>
</servlet-mapping>

This binding is vendor-specific, and every container has its own mechanism for setting data sources. Please note that this is just a declaration for dependency on an external resource, and doesn't create the actual resource. Comprehending the tags is pretty straightforward: this indicates to the container that the local reference name jdbc/TestDB should be set by the app deployer, and this should match with the resource name, as declared in server.xml file.


打算改天把这篇文章给翻译一下,留个记号先


分享到:
评论

相关推荐

    tomcat 数据库连接池配置的jar包

    `commons-dbcp.jar`是Apache Commons Database Connection Pooling(DBCP)的实现,它提供了一个数据库连接池服务。这个库包含了一个名为`BasicDataSource`的类,它是符合Java的JDBC `javax.sql.DataSource`接口的...

    tomcat-7_API_帮助文档

    * tomcat-jdbc.jar (Tomcat's database connection pooling solution) * tomcat-util.jar (Various utilities) * websocket-api.jar (WebSocket 1.1 API) You can make additional APIs available to all of your ...

    tomcat-8_API

    * tomcat-jdbc.jar (Tomcat's database connection pooling solution) * tomcat-jni.jar (Interface to the native component of the APR/native connector) * tomcat-spdy.jar (SPDY implementation) * tomcat-util...

    Hibernate使用Tomcat连接池.doc

    本文将详细介绍如何在Hibernate中使用Tomcat的内置连接池DBCP(Database Connection Pool)。 首先,Tomcat的连接池配置是在`server.xml`文件中进行的。在`&lt;Context&gt;`标签内,我们需要添加一个名为`jdbc/quickstart...

    Tomcat6下JNDI的配置

    这两个库分别是Apache Commons Database Connection Pooling和Apache Commons Pool,它们提供了数据库连接池的功能。 接下来,我们需要在项目的`WEB-INF/web.xml`中进行配置。在`&lt;web-app&gt;`节点下添加一个监听器(`...

    Tomcat数据库连接池的配置和实例

    &lt;title&gt;SqlServer_POOLING_TEST try { Context initCtx = new InitialContext(); Context ctx = (Context)initCtx.lookup("java:comp/env"); Object obj = ctx.lookup("jdbc/contest"); javax.sql.DataSource...

    log4j日志的数据库管理

    因此,本文将深入探讨如何利用数据库对Log4j日志进行高效管理,特别是通过DBCP(Database Connection Pooling)和MySQL数据库的结合,实现日志的集中式存储和查询,同时添加用户名等附加信息,增强日志的可追溯性和...

    restful webservice构建小例子(restlet).pdf

    3. 数据库访问:使用DBCP(Database Connection Pooling)数据连接池,通过单例模式封装在`DbcpBean`类中,确保高效、稳定的数据库操作。数据库连接池的相关配置存储在`database.properties`资源文件中。 4. 资源...

    c3p0 还有其他连接池

    c3p0是一款开源的Java数据库连接池实现,由Miquel Arquero创建,它是Comprehensive Database Connection Pooling的缩写。c3p0提供了对JDBC数据库连接的池化管理,其主要功能包括: 1. **连接初始化**:c3p0允许配置...

    commons-dbcp-1.2.2.jar &commons-pool-1.3.jar

    `commons-dbcp`(Apache Commons Database Connection Pooling)是Apache组织提供的一个数据库连接池实现。这个库允许开发者高效地管理和复用数据库连接,减少创建和销毁连接的开销,从而提高应用程序的性能。`...

    spring核心jar

    3. **commons-dbcp-1.2.jar**:Apache Commons DBCP(Database Connection Pooling)是一个数据库连接池实现。在Spring应用中,数据库连接池可以有效地管理和复用数据库连接,减少创建和关闭连接的开销,提高应用...

    dbcp_mysql

    本篇将深入探讨如何在Struts应用中获取并使用`DataSource`,以及相关的DBCP(Basic Database Connection Pooling)库。 首先,`DataSource`是Java的JDBC API中的一个接口,它提供了一种标准的方式来管理和共享...

    mysql-connector-java-5.1.6-bin.jar

    7. **性能优化**:通过配置参数,如使用连接池(Connection Pooling),可以显著提升性能。 8. **兼容性**:此版本5.1.6针对MySQL 5.1.x版本进行了优化,确保了良好的兼容性。 在使用`mysql-connector-java-5.1.6-...

    常用的两个jar包

    4. **Connection Pooling**:为了提高性能和资源利用率,`connector.jar`通常包含连接池实现,它可以缓存和重用数据库连接,避免频繁的创建和销毁连接带来的开销。 这两个`jar`包在不同的场景下各有其重要作用。`...

    DBCP连接池DBCP和C3P0配置

    Apache DBCP(Database Connection Pool)和C3P0都是流行的开源数据库连接池实现。 - **DBCP**:Apache DBCP是Apache Commons Pool项目中的一个子项目,它提供了对数据库连接池的实现。 - **C3P0**:C3P0(Concise ...

    BaseDatosBoyacaApp:不

    此外,为了更好地组织和管理数据库连接,可能会使用Connection Pooling技术,例如C3P0、HikariCP或Apache DBCP。 如果这个项目是Web应用程序,那么它可能基于Spring Framework,因为它提供了方便的JDBC抽象层和数据...

    Geoserver用户手册

    7 Working with Raster Data 165 7.1 GeoTIFF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 7.2 GTOPO30 . . . . . . . . . . . . . . . . . . . . . ....

Global site tag (gtag.js) - Google Analytics