Tomcat5 database connection pool configuration

xiaoxiao2021-03-06  47

Tomcat5 database connection pool configuration

This article mainly introduces this version of the TOMCAT5.0.25, the configuration of the database connection pool, and the JNDI lookup of the connection pool, and provides the corresponding test code. Finally, the common problems and solutions in the configuration and application process are pointed out.

1. Configuration Environment of this article: Tomcat5.0.25 JDK1.4 SQL Server 2000 Win2000

2. Configuration steps:

first step:

Start Tomcat, open IE Enter http: // localhost: 8080 / admin to enter Tomcat management interface in the address bar; click on the drop-down menu on the right side to select Create New Data Source in the lower right input box. Configuration information.

JNDI NAME: JDBC / XXX (XXX is the name yourself ")

Data Source URL: JDBC: Microsoft: SQLSERVER: // localhost: 1433; DatabaseName = Tempdb (connected data name)

JDBC Driver Class: com.microsoft.jdbc.sqlserver.sqlserverdriver

User name: User name to connect to the database

Password: Database password

Max. Active Connections: Maximum connection

Max. Idle Connections: is the maximum number of idle connections

Max. Wait for Connection: Maximum Waiting Connection

Step 2: Configure Web.xml Open Web.xml under WebApps / Root / Web-INF, add the following: SQLServer Datasource Example JDBC / SQLServerDB javax.sql.datasource Container

Step 3: Configure Tomcat (add classes)

First you want to download the installation of SQLSerVer-JDBC-driver, then put the three JAR files under its lib.

Under Tomcat / Common / LIB.

Precautions:

If we are a single JSP page we put it under Tomcat_Home / WebApps / root, but also need to modify the root.xml file under Tomcat_Home / Conf / Catalina / LocalHost /

Add:

To reference the configured JNDI data source. At the same time, Comment If we are going to deploy a web application, we will apply The program is placed in the Tomcat_Home / WebApps directory, there are two ways to reference the JNDI data source we have configured.

A: You can create a new xxx.xml file under Tomcat_Home / CONF / CATALINA / LOCALHOST, XXX is the name of the released web application, such as the web application I posted is jnditest, .xml file named jnditest.xml in this file plus:

factory

org.apache.commons.dbcp.basicDataSourceFactory

Password

jsjrj

maxactive

4

maxwait

5000

URL

JDBC: Microsoft: SQLServer: // localhost: 1433; DatabaseName = Tempdb

driverclassname

com.microsoft.jdbc.sqlser.sqlserverdriver

Username

sa

maxidle

2

Where E: / Tomcat 5.0 / WebApps / Jnditest is a directory for your publishing application, so you can test it.

Doing this is that every web application must create a file like JNDITEST.XML.

B; To avoid each web application to create a xxx.xml file, we can modify the server.xml file under Tomcat_Home / Conf, modify the following:

Join between :

URL

JDBC: Microsoft: SQLServer: // localhost: 1433; DatabaseName = Tempdb

driverclassname

com.microsoft.jdbc.sqlser.sqlserverdriver

maxwait

5000

maxactive

4

Password

jsjrj

maxidle

2

Username

sa

factory

org.apache.commons.dbcp.basicDataSourceFactory

"JDBC / TEST" TYPE = "javax.sql.datasource" />

At the same time, the paragraph on the upper side: (can also be removed)

URL

JDBC: Microsoft: SQLServer: // localhost: 1433; DatabaseName = Tempdb

driverclassname

com.microsoft.jdbc.sqlser.sqlserverdriver

maxwait

5000

maxactive

4

Password

jsjrj

maxidle

2

Username

sa

factory

org.apache.commons.dbcp.basicDataSourceFactory

Note To join in the web.xml file of each web application

SQLServer Datasource Example jdbc / sqlserverdb javax.sql.datasource < Res-auth> container

3. test

Write a JSP file:

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ Page Import = "Java.sql. *"%>

<% @ Page Import = "javax.sql. *"%>

<% @ Page Import = "javax.naming. *"%>

<%

Context ctx = new initialContext ();

Connection conn = NULL;

CTX = New InitialContext ();

DataSource DS = (Datasource) CTX.lookup ("Java: Comp / Env / JDBC / TEST");

CONN = ds.getConnection ();

Statement Stmt = Conn.createStatement (ResultSet.Type_Scroll_Sensitive, ResultSet.concur_Updata);

String SQL = "SELECT * from T_Student";

ResultSet RS = Stmt.executeQuery (SQL);

While (rs.next ()) {%>

<%

Out.print (Rs.getstring (1));

Out.print (Rs.getstring (2));

Out.print (Rs.getstring (3));

}%>

<%

Out.print ("Database Success, Congratulations on You");

Rs.close ();

Stmt.close ();

CONN.CLOSE ();

%>

转载请注明原文地址:https://www.9cbs.com/read-117381.html

New Post(0)