Jndi method for defining Oracle data sources

Source: Internet
Author: User
Tags auth connection pooling valid tomcat

The method for defining a Jndi data source is as follows:

Content Summary:

1. Introduction

2, the data connection pool configuration

3, does not use the connection pool the solution

4, Oracle data source configuration examples

First, the general introduction introdution

Configuring a Jndi data source is a special case of configuring Jndi resources, both processes and methods are similar.

Note that the configuration of the Jndi resource has changed between Tomcat 5.0.x and Tomcat 5.5.x versions, so if you are using the above version, you need to modify the configuration syntax.

This article assumes that you have understood the context and host configuration, if not, please learn by yourself.

Ii. configuration of the data connection pool database Connection pool (DBCP) configurations

DBCP provides support for JDBC2.0, if you use the 1.4 version JVM,DBCP will support JDBC3.0.

2.1 Installation Installation

DBCP uses the jakarta-commons Database Connection Pool, which relies on the following three packages:

Jakarta-commons DBCP

Jakarta-commons Collections

Jakarta-commons Pool

The Tomcat installation directory provides an integrated Jar pack $CATALINA _home/lib/tomcat-dbcp.jar.

Third, to avoid data connection pool leakage preventing DB connection pool leaks

As the name implies, the data connection pool is responsible for creating and managing database connections. Reusing an existing data connection is much more efficient than opening a new connection each time.

But connection pooling is a common problem: a Web application needs to shut down resources such as resultset ' S,statement ' s,connections ' s. If the application shuts down the resource, it causes the resources to be reused. That is, the data connection pool is compromised. Eventually, if there are not enough valid connections, the application connection fails.

There is a solution to this problem; Jakarta-commons's DBCP can be configured to track and recover those failed connections. Not only can you recover, but you can also generate stack trajectories.

To shut down and reuse the abandoned resource, you can add attributes to the resource configuration of the DBCP data source:

Removeabandoned= "true"

When a valid data connection is lower than the DBCP setting, the discarded resource is reused. Default is false;

removeabandonedtimeout= "60"

Set how many seconds after the discarded data connection can be deleted ... The default is 300 seconds.

Logabandoned= "true"

If you want to record that the stack trajectory of the discarded data connection can be set to true ... Default false;

Iv. Examples of Oracle configuration

Tomcat can only recognize *.jar files, and if the database driver is *.zip, you need to modify the extension to jar. Oracle.jdbc.OracleDriver should be used for oracle9i because Oracle.jdbc.driver.OracleDriver is not recommended for use. The future is not necessarily supported.

4.1 Configuration Server.xml

<Resource name="jdbc/myoracle" auth="Container"
 type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
 url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
 username="scott" password="tiger" maxActive="20" maxIdle="10"
 maxWait="-1"/>

4.2 Configuration Web.xml

<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

4.3 code example

Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
//etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.