JNDI-j2ee

 

Database Connection Pool (DBCP) Configurations

The default database connection pool implementation in Apache Tomcat relies on the libraries from the Apache Commons project. The following libraries are used:

  • Commons DBCP
  • Commons Pool

These libraries are located in a single JAR at $CATALINA_HOME/lib/tomcat-dbcp.jar. However, only the classes needed for connection pooling have been included, and the packages have been renamed to avoid interfering with applications.

DBCP 1.3 provides support for JDBC 3.0.

Installation

See the DBCP documentation for a complete list of configuration parameters.

Preventing database connection pool leaks

A database connection pool creates and manages a pool of connections to a database. Recycling and reusing already existing connections to a database is more efficient than opening a new connection.

There is one problem with connection pooling. A web application has to explicitly close ResultSet's, Statement's, and Connection's. Failure of a web application to close these resources can result in them never being available again for reuse, a database connection pool "leak". This can eventually result in your web application database connections failing if there are no more available connections.

There is a solution to this problem. The Apache Commons DBCP can be configured to track and recover these abandoned database connections. Not only can it recover them, but also generate a stack trace for the code which opened these resources and never closed them.

To configure a DBCP DataSource so that abandoned database connections are removed and recycled add the following attribute to the Resource configuration for your DBCP DataSource:

removeAbandoned="true"

When available database connections run low DBCP will recover and recycle any abandoned database connections it finds. The default is false.

Use the removeAbandonedTimeout attribute to set the number of seconds a database connection has been idle before it is considered abandoned.

removeAbandonedTimeout="60"

The default timeout for removing abandoned connections is 300 seconds.

The logAbandoned attribute can be set to true if you want DBCP to log a stack trace of the code which abandoned the database connection resources.

logAbandoned="true"

The default is false.

MySQL DBCP Example

0. Introduction

Versions of MySQL and JDBC drivers that have been reported to work:

  • MySQL 3.23.47, MySQL 3.23.47 using InnoDB,, MySQL 3.23.58, MySQL 4.0.1alpha
  • Connector/J 3.0.11-stable (the official JDBC Driver)
  • mm.mysql 2.0.14 (an old 3rd party JDBC Driver)

Before you proceed, don't forget to copy the JDBC Driver's jar into $CATALINA_HOME/lib.

1. MySQL configuration

Ensure that you follow these instructions as variations can cause problems.

Create a new test user, a new database and a single test table. Your MySQL user must have a password assigned. The driver will fail if you try to connect with an empty password.

mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@localhost
    ->   IDENTIFIED BY 'javadude' WITH GRANT OPTION;
mysql> create database javatest;
mysql> use javatest;
mysql> create table testdata (
    ->   id int not null auto_increment primary key,
    ->   foo varchar(25),
    ->   bar int);

Note: the above user should be removed once testing is complete!

Next insert some test data into the testdata table.

mysql> insert into testdata values(null, 'hello', 12345);
Query OK, 1 row affected (0.00 sec)

mysql> select * from testdata;
+----+-------+-------+
| ID | FOO   | BAR   |
+----+-------+-------+
|  1 | hello | 12345 |
+----+-------+-------+
1 row in set (0.00 sec)

mysql>

2. Context configuration

 META-INF/context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
    <!-- maxActive: Maximum number of database connections in pool. Make sure
        you configure your mysqld max_connections large enough to handle all of your
        db connections. Set to -1 for no limit. -->

    <!-- maxIdle: Maximum number of idle database connections to retain in pool.
        Set to -1 for no limit. See also the DBCP documentation on this and the minEvictableIdleTimeMillis
        configuration parameter. -->

    <!-- maxWait: Maximum time to wait for a database connection to become available
        in ms, in this example 10 seconds. An Exception is thrown if this timeout
        is exceeded. Set to -1 to wait indefinitely. -->

    <!-- username and password: MySQL username and password for database connections -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is org.gjt.mm.mysql.Driver
        - we recommend using Connector/J though. Class name for the official MySQL
        Connector/J driver is com.mysql.jdbc.Driver. -->

    <!-- url: The JDBC connection url for connecting to your MySQL database. -->

    <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="javauser"
        password="javadude"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/javatest" />

</Context>

3. web.xml configuration

Now create a WEB-INF/web.xml for this test application.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

4. Test code

Now create a simple test.jsp page for use later.

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from testdata
</sql:query>

<html>
  <head>
    <title>DB(JNDI) Test</title>
  </head>
  <body>

  <h2>Results</h2>

<c:forEach var="row" items="${rs.rows}">
    Foo: ${row.foo}<br/>
    Bar: ${row.bar}<br/>
</c:forEach>

  </body>
</html>

That JSP page makes use of JSTL's SQL and Core taglibs. You can get it from Apache Tomcat Taglibs - Standard Tag Library project — just make sure you get a 1.1.x release. Once you have JSTL, copyjstl.jar and standard.jar t(

Standard 1.1 JSTL 1.1 Servlet 2.4, JavaServer Pages 2.0 download

)o your web app's WEB-INF/lib directory.

Finally deploy your web app into $CATALINA_BASE/webapps either as a warfile called DBTest.war or into a sub-directory called DBTest

Once deployed, point a browser at http://localhost:8080/DBTest/test.jsp to view the fruits of your hard work.

 

时间: 2025-01-17 07:52:01

JNDI-j2ee的相关文章

将JNDI用于WebSphere Application Server J2EE瘦客户端应用程序

引言 命名服务管理一组命名空间,解析数据对象的名称绑定问题.Java命名和目录接口(Java Namingand Directory Interface,JNDI) 是一组 API,它提供对命名和目录服务的访问.许多 Java 应用程序使用 JNDI 来定位资源,比如数据源和在 Java 2 企业版(Java 2 Enterprise Edition, J2EE)中注册的企业JavaBeans(Enterprise JavaBeans,EJB). IBM Java 瘦客户端应用程序提供了一种轻量

J2EE or J2SE? JNDI works with both(from javaworld)

欢迎大家指正!-----------------------------J2EE or J2SE? JNDI works with both  JNDI(Java Naming and Directory Interface), 允许应用通过一个通用的接口来访问各种命名和目录服务.下面的特征表征了JNDI的架构.如同JDBC(Java Database Connectivity),JNDI并不是一种服务,而是一套接口:它允许应用使用标准API来访问很多不同的目录服务供应商.尽管Sun公司提供了适

JNDI 笔记(二) J2EE下使用JNDI

在J2EE环境下使用JNDI是非常简单的事,因为所有的J2EE容器都要实现JNDI服务,所以,在J2EE环境下使用JNDI,与使用Hashtable也没有什么太大区别.只有一点限制,那就是绑定对象时,对象所属的类必须实现java.io.Serializable接口,这一点也实在一点也不困难,几乎所有用到的Java类都实现了这个接口,对于自定义的类,在接口实现列表里把这个接口加进去也就是了. 下面,我将演示一下如何在J2EE环境下使用JNDI,为了保证代码的通用性,我不使用struts之类的框架,

J2EE全面介绍

一.J2EE的概念 目前,Java 2平台有3个版本,它们是适用于小型设备和智能卡的Java 2平台Micro版(Java 2 Platform Micro Edition,J2ME).适用于桌面系统的Java 2平台标准版(Java 2 Platform Standard Edition,J2SE).适用于创建服务器应用程序和服务的Java 2平台企业版(Java 2 Platform Enterprise Edition,J2EE). J2EE是一种利用Java 2平台来简化企业解决方案的开

J2EE中使用Spring AOP框架和EJB组件

j2ee 快速发展的开发人员社区.对各种后端技术(包括JMS.JTA.JDO.Hibernate.iBATIS等等)的支持,以及(更为重要的)非侵入性的轻量级IoC容器和内置的AOP运行时,这些因素使得Spring Framework对于J2EE应用程序开发十分具有吸引力.Spring托管的组件(POJO)可以与EJB共存,并允许使用AOP方法来处理企业应用程序中的横切方面--从监控和审计.缓存及应用程序级的安全性开始,直到处理特定于应用程序的业务需求. 本文将向您介绍Spring的AOP框架在

J2EE初学者到精通必须需要理解的问题

j2ee|初学|问题 J2EE体系结构简单介绍 一.J2EE提出的背景 1. 企业级应用框架的需求 在许多企业级应用中,例如数据库连接.邮件服务.事务处理等都是一些通用企业需求模块,这些模块如果每次再开发中都由开发人员来完成的话,将会造成开发周期长和代码可靠性差等问题.于是许多大公司开发了自己的通用模块服务.这些服务性的软件系列同陈为中间件. 2. 为了通用必须要提出规范,不然无法达到通用 在上面的需求基础之上,许多公司都开发了自己的中间件,但其与用户的沟通都各有不同,从而导致用户无法将各个公司

J2EE学习的过程

j2ee|过程 在这里我谈谈我在学习j2ee流程,并谈到在此过程中领会的经验和教训.以便后来者少走弯路.  Java发展到现在,按应用来分主要分为三大块:J2SE,J2ME和J2EE.这三块相互补充,应用范围不同.  J2SE就是Java2的标准版,主要用于桌面应用软件的编程:  J2ME主要应用于嵌入是系统开发,如手机和PDA的编程:  J2EE是Java2的企业版,主要用于分布式的网络程序的开发,如电子商务网站和ERP系统.  先学习j2se  要学习j2ee就要先学习j2se,刚开始学习j

初学者如何开发出高质量的J2EE系统

j2ee|初学 J2EE学习者越来越多,J2EE本身技术不断在发展,涌现出各种概念,本文章试图从一种容易理解的角度对这些概念向初学者进行解释,以便掌握学习J2EE学习方向. 首先我们需要知道Java和J2EE是两个不同概念,Java不只是指一种语言,已经代表与微软不同的另外一个巨大阵营,所以Java有时是指一种软件系统的流派,当然目前主要是.NET和Java两大主流体系. J2EE可以说指Java在数据库信息系统上实现,数据库信息系统从早期的dBase.到Delphi/VB等C/S结构,发展到B

利用Spring框架改进J2EE编程

j2ee|编程 摘要 J2EE编程正在变得越来越复杂.J2EE已经发展为一个API.复杂化的编程和配置的复杂网络.为了应对这种复杂性,新的框架和方法不断涌现.这些框架高度依赖于一个称为IoC(Inversion of Control,反向控制)的概念.本文将探讨这种方法的一些特性和优点,因为这种方法与J2EE编程相关,而且可以使J2EE编程变得更轻松. 简介 马克·吐温的一句话常被引用:"--关于我死亡的报道是一种夸张."现在已经出现了很多关于.Net的流言,以及认为J2EE API的

j2ee常见错误(放到这里备查)--节选自j2ee tutorial

j2ee|错误 常见问题及其解决办法 不能启动J2EE服务器 命名目录服务端口冲突 症状:当你使用-verbose选择启动J2EE服务器时,显示以下信息: J2EE server listen port: 1050 RuntimeException: Could not initialize server. . . 解决办法:其它程序使用了1050端口.如果J2EE服务已经运行了,你可以通过输入 j2ee -stop停止这个服务.如果其它程序使用了这个端口,你可以通过修改你的J2EE SDK安装