JNDI数据源指引外文翻译资料

 2022-08-14 02:08

文献翻译

资料来源:tomcat.9.0.doc

10)JDBC DataSources

JNDI Datasource How-To

Table of Contents

Introduction

JNDI Datasource configuration is covered extensively in the JNDI-Resources-HOWTO. However, feedback from tomcat-user has shown that specifics for individual configurations can be rather tricky.

Here then are some example configurations that have been posted to tomcat-user for popular databases and some general tips for db usage.

You should be aware that since these notes are derived from configuration and/or feedback posted to tomcat-user YMMV :-). Please let us know if you have any other tested configurations that you feel may be of use to the wider audience, or if you feel we can improve this section in anyway.

Please note that JNDI resource configuration changed somewhat between Tomcat 7.x and Tomcat 8.x as they are using different versions of Apache Commons DBCP library. You will most likely need to modify older JNDI resource configurations to match the syntax in the example below in order to make them work in Tomcat 9. See Tomcat Migration Guide for details.

Also, please note that JNDI DataSource configuration in general, and this tutorial in particular, assumes that you have read and understood the Context and Host configuration references, including the section about Automatic Application Deployment in the latter reference.

DriverManager, the service provider mechanism and memory leaks

java.sql.DriverManager supports the service provider mechanism. This feature is that all the available JDBC drivers that announce themselves by providing a META-INF/services/java.sql.Driver file are automatically discovered, loaded and registered, relieving you from the need to load the database driver explicitly before you create a JDBC connection. However, the implementation is fundamentally broken in all Java versions for a servlet container environment. The problem is that java.sql.DriverManager will scan for the drivers only once.

The JRE Memory Leak Prevention Listener that is included with Apache Tomcat solves this by triggering the driver scan during Tomcat startup. This is enabled by default. It means that only libraries visible to the common class loader and its parents will be scanned for database drivers. This include drivers in $CATALINA_HOME/lib, $CATALINA_BASE/lib, the class path and (where the JRE supports it) the endorsed directory. Drivers packaged in web applications (in WEB-INF/lib) and in the shared class loader (where configured) will not be visible and will not be loaded automatically. If you are considering disabling this feature, note that the scan would be triggered by the first web application that is using JDBC, leading to failures when this web application is reloa

剩余内容已隐藏,支付完成后下载完整资料


文献翻译

资料来源:Tomcat 9.0 .doc

10)JDBC数据源

JNDI数据源指引

目录

  • 简介
  • 驱动管理器、服务提供程序机制和内存泄露
  • 数据库连接池(DBCP 2)配置
  1. 配置
  2. 防止数据库连接池泄露
  3. MySQL数据库连接池举例
  4. Oracle 8i,9i和10g的区别
  5. PostgreSQL
  • 非DBCP解决方法
  • Oracle 8i与OCI客户端
  1. 简介
  2. 综合分析
  • 常见问题
  1. 间歇性数据库连接故障
  2. 随机连接关闭异常
  3. Context和GlobalNamingResources
  4. JNDI资源命名和领域交互

简介

JNDI数据源配置在JNDI资源HOWTO中有详细介绍。然而,tomcat用户的反馈显示,个别配置的细节可能有些严重。

下面是一些已经发布到tomcat-user上的配置示例和一些关于db使用的一般技巧。

您应该知道,因为这些注释来自于发布给tomcat用户YMMV的配置或反馈。如果你有任何其他测试配置,你觉得可能有助于更广泛的观众,或者如果你觉得我们可以改善这一部分,请让我们知道。

请注意,在Tomcat 7.x和Tomcat 8.x之间,JNDI资源配置发生了一些变化,因为它们使用不同版本的Apache Commons DBCP库。您很可能需要修改旧的JNDI资源配置以匹配下面示例中的语法,以便使它们在Tomcat 9中工作。有关详细信息,请参阅Tomcat迁移指南。

另外请注意,JNDI数据源配置,特别是本教程,通常假设您已经阅读并理解了上下文和主机配置引用,包括后一个引用中关于自动应用程序部署的部分。

驱动管理器、服务提供者机制和内存泄漏

java.sql.DriverManager支持服务提供者机制。这个特性是所有可用的JDBC驱动程序都通过提供一个META-INF/services/java.sql来声明它们自己。驱动程序文件将自动被发现、加载和注册,从而使您不必在创建JDBC连接之前显式地加载数据库驱动程序。但是,在servlet容器环境的所有Java版本中,实现基本上都是不完整的。问题是java.sql.DriverManager将只扫描驱动程序一次。

Apache Tomcat中包含的JRE内存泄漏预防侦听器通过在Tomcat启动期间触发驱动程序扫描来解决这个问题。这是默认启用的.意味着只有对公共类装入器及其父类可见的库才会被扫描以寻找数据库驱动程序。它包括$CATALINA_HOME/lib中的驱动程序、$CATALINA_BASE/lib中的驱动程序、类路径和(JRE支持的)已通过的目录。打包在web应用程序(在web-inf/lib中)和共享类装入器(在配置的地方)中的驱动程序将不可见,也不会自动装入。如果您正在考虑禁用此功能,请注意扫描将由使用JDBC的第一个web应用程序触发,从而导致在重新加载此web应用程序和其他依赖此功能的web应用程序时出现故障。

因此,在web-inf/lib目录中包含数据库驱动程序的web应用程序不能依赖于服务提供者机制,应该显式地注册这些驱动程序。

驱动程序列表java.sql.DriverManager也是一个已知的内存泄漏源。web应用程序注册的任何驱动程序必须在web应用程序停止时注销。当web应用程序停止时,Tomcat将尝试自动发现并注销由web应用程序类加载器加载的任何JDBC驱动程序。但是,应用程序应该通过ServletContextListener来实现这一点。

数据库连接池(DBCP 2)配置

Apache Tomcat中的默认数据库连接池实现依赖于Apache Commons项目中的库使用下列图书馆:

bull;commons DBCP 2

bull;共用资源池2

这些库位于$CATALINA_HOME/lib/tomcat-dbcp.jar的单个JAR中。但是,只包含了连接池所需的类,并且对包进行了重命名,以避免干扰应用程序。

DBCP 2提供了对JDBC 4.1的支持。

  1. 配置

有关配置参数的完整列表,请参阅DBCP 2文档。

  1. 防止数据库连接池泄露

数据库连接池创建并管理到数据库的连接池,循环和重用到数据库的现有连接比打开新连接更有效。

连接池有一个问题:web应用程序必须显式地关闭ResultSet、语句和连接。web应用程序如果不能关闭这些资源,就会导致这些资源再也不能重用,从而导致数据库连接池“泄漏”。如果没有更多可用的连接,这最终可能导致web应用程序数据库连接失败。

这个问题有一个解决办法。可以配置Apache Commons DBCP 2来跟踪和恢复这些废弃的数据库连接。它不仅可以恢复这些资源,还可以为打开这些资源而从不关闭它们的代码生成堆栈跟踪。

若要配置DBCP 2数据源,以便删除和回收废弃的数据库连接,请将以下一个或两个属性添加到DBCP 2数据源的资源配置中:

配置参数:

removeAbandonedOnBorrow=true

removeAbandonedOnMaintenance=true

这两个属性的默认值都是false。注意,removeonedonmaintenance不起作用,除非通过将timeBetweenEvictionRunsMillis设置为正值来启用池维护。有关这些属性的完整文档,请参阅DBCP 2文档。

使用removeonedtimeout属性设置一个数据库连接在被认为被放弃之前空闲的秒数:

removeAbandonedTimeout='60'

删除废弃连接的默认超时时间是300秒。

如果希望DBCP 2记录放弃数据库连接资源的代码的堆栈跟踪,可以将log属性设置为true:

logAbandoned='true'

默认为false。

  1. MySQL数据库连接池举例

(1) 介绍

MySQL和JDBC驱动程序的工作报告:

MySQL 3.23.47, MySQL 3.23.47使用InnoDB, MySQL 3.23.58, MySQL 4.0.1alpha连接器/J 3.0.11-stable(官方的JDBC驱动程序)

mysql 2.0.14(一个老的第三方JDBC驱动程序)

在继续之前,不要忘记将JDBC驱动程序的jar复制$CATALINA_HOME/lib中。

(2) MySQL配置

请确保您遵循以下说明,因为变化可能会导致问题。

创建一个新的测试用户、一个新的数据库和一个单独的测试表。您的MySQL用户必须有一个指定的密码。如果您尝试使用空密码连接,则驱动程序将失败:

mysqlgt; GRANT ALL PRIVILEGES ON *.* TO javauser@localhost

-gt; IDENTIFIED BY javadude WITH GRANT OPTION;

mysqlgt; create database javatest;

mysqlgt; use javatest;

mysqlgt; create table testdata (

-gt; id int not null auto_increment primary key,

-gt; foo varchar(25),

-gt; bar int);

注: 一旦测试完成,上述用户应删除!

接下来,将一些测试数据插入到testdata表中:

mysqlgt; insert into testdata values(null, hello, 12345);

Query OK, 1 row affected (0.00 sec)

mysqlgt; select * from testdata;

---- ------- -------

| ID | FOO | BAR |

---- ------- -------

| 1 | hello | 12345 |

---- ------- -------

1 row in set (0.00 sec)

mysqlgt;

(3)上下文配置

通过将资源的声明添加到上下文来在Tomcat中配置JNDI数据源。例如:

lt;Contextgt;

lt;!-- maxTotal: 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.

--gt;

lt;!-- maxIdle: Maximum number of idle database connections to retain in pool.

Set to -1 for no limit. See also the DBCP 2 documentation on this

and the minEvictableIdleTimeMillis configuration parameter.

--gt;

lt;!-- maxWaitMillis: 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.

--gt;

lt;!-- username and password: MySQL username and password for database connections --gt;

lt;!-- 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.

--gt;

lt;!-- url: The JDBC connection url for connecting to your MySQL database.

--gt;

lt;Resource name='jdbc/TestDB' auth='Container' type='javax.sql.DataSource'

maxTotal='100' maxIdle='30' maxWaitMillis='10000'

username='javauser' password='javadude' driverClassName='com.mysql.jdbc.Driver'

url='jdbc:mysql://localhost:3306/javatest'/gt;

lt;/Contextgt;

(4)web . xml配置

现在为这个测试应用程序创建一个WEB-INF/web.xml文件:

lt;web-app xmlns='http://java.sun.com/xml/ns/j2ee'

xmlns:xsi='http://www.w3

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[235627],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。