Title: Tomcat4 / 5 Connection Pool Settings and Simple Application Examples
Keywords: Tomcat connection pool JDBC driver Context
Author: jrq
content:
First, the configuration of the connection pool of Tomcat4 under Win2k
1. Install JDK1.4 and Jakarta-Tomcat-4.1.27.exe.
The paths are D: /JDK1.4 and D: / Tomcat 4.1, respectively.
When you install Tomcat 4, you will be required to fill in the web access port, user name, password, etc.
Web Access Port Minimal thinks 8080, username is Default as admin, password yourself.
Start Tomcat4, access
Http://127.0.0.1:8080/ You can see the welcome access page of Apache Tomcat / 4.1.27, some related introduction information, etc. on the page.
2. Set the driver of the database
Take Oracle's JDBC driver settings as an example:
Oracle8i driver in / oracle / ora81 / jdbc / lib / directory. The name is classes12.zip.
Oracle9i driver In / Oracle9i / JDBC / LIB / Directory, the name is ojdbc14.jar.
Copy this file under D: / Tomcat 4.1 / Common / LIB.
Note that if you use Oracle8i driver, you need to rename the class 12.zip to class 12.jar in the D: / Tomcat 4.1 / Common / Lib directory, because Tomcat4 does not know the Zip's suffix name.
3. Set the web application.
Create a new folder, the name is MyPool, the location is d: / mypool. This d: / mypool path will be the main directory of the web application.
Open the D: / Tomcat 4.1 / conf / server.xml file, find the following line:
->
Below below, add the following:
After completing, becomes this:
->
Save the D: / Tomcat 4.1 / conf / server.xml file.
After the configuration is complete, restart Tomcat, you can access this web app:
Http://127.0.0.1:8080/mypool/.
4. Set the Tomcat connection pool data source.
access
Http://127.0.0.1:8080/admin page, log in with the username and password fill in the Tomcat4 just installed Tomcat4.
After the login is successful, Tomcat Web Server Administration Tool will appear. Then click on the "Data Sources" item on the left side, and the page of the JNDI configuration can be seen on the right.
Select "CREATE New Data Source" in the dropboard in the upper right corner, follow the configuration information:
JNDI NAME: JDBC / MyorCl
Data Source URL: JDBC: Oracle: Thin: @ 192.168.6.40: 1521: DBServer
JDBC Driver Class: Oracle.jdbc.driver.Orcledriver
User name: myname
Password: mypassword
Max. Active Connections: Available default values
Max. Idle Connections: Available default values
Max. Wait for Connection: Available default values
Validation Query: You can not fill it.
Then, press the "SAVE" button to save.
Then press the "Commit" button to complete the configuration change.
If you look at the Tomcat4 window, you will find that there is a line of text tips at the bottom:
Debugging - Changes Saved to Conf / Server.xml
The configuration of the D: / Tomcat 4.1 / conf / server.xml file has changed.
If you open the D: / Tomcat 4.1 / conf / server.xml file at this time, you will find a lot of content of the server.xml file.
Some comments are removed.
It will also find the following text to configure the web application above.
->
It is automatically modified to this:
parameter>
parameter>
parameter>
parameter>
parameter>
parameter>
parameter>
Resourceparams>
parameter>
Resourceparams>
Globalnamingresources>
5. Set a web application to the connection pool.
Open the D: / Tomcat 4.1 / conf / server.xml file, find the web application text just configured, because the server.xml file is automatically modified, and removed a lot of comments, the web application just configured
Automatic modification is this:
After the setting is complete, the description of the web application is approximately as follows:
Context>
Save the D: / Tomcat 4.1 / conf / server.xml file.
Note that the reference description of this connection pool is a very important step.
Some friends configure the connection pool failure in Tomcat4, most of them. There are also some online articles that this is a bug of Tomcat4.
I used it for two days and two nights to understand the mystery. [:(]
I only checked a little bit at that time. Because it is simple to configure a connection pool. [:(]
6. Usually, here, the setting of the connection pool is completed.
Then you need to restart Tomcat so you can use the connection pool to work.
However, I saw a lot of articles that I also need to set up Web.xml to apply. I didn't set up Web.xml, and the connection pool can also be fully used.
Now let's talk about the configuration method of web.xml.
Built a new folder in D: / MyPool, named Web-INF, pay attention to the uppercase letters.
Then create a new file web.xml in D: / MyPool / Web-INF / under:
resource-ref>
web-app>
Save the D: /MyPool/web-inf/web.xml file.
7. A simple application example.
Write a simple JSP page that accesses the database with the connection pool.
The content is as follows:
--------------------- <% @ Page ContentType = "text / html; charset = GBK"%>
<% @ Page Import = "javax.naming. *"%>
<% @ Page Import = "javax.sql. *"%>
<% @ Page Import = "java.sql. *"%>
<%
Try
{
CONTEXT INITCTX = New InitialContext ();
IF (InitctX == Null)
Throw New Exception ("There is no matching environment");
Context CTX = (Context) INitctX.lookup ("Java: Comp / ENV");
/ / Get the connection pool object
Object obj = (object) CTX.lookup ("JDBC / MyorCl");
// Type conversion
Javax.sql.datasource DS = (javax.sql.datasource) OBJ;
IF (ds == null)
Throw New Exception ("No Matching Database");
Connection conn = ds.getConnection ();
Statement Stmt = conn.createstatement ();
String strsql = "select * from grade"; // SQL, to ensure that there is a record in the grade data table.
ResultSet RS = Stmt.executeQuery (strsql);
IF (rs.next ())
{
Out.println (Rs.getstring (1));
Out.println (rs.getstring (2));
}
rs.close (); // Close ResultSet
Stmt.close (); // Close Statement
Conn.close (); // put the connection back to the connection pool
}
Catch (Exception EX)
{
EX.PrintStackTrace ();
Throw New Sqlexception ("Cannot Get Connection Pool." EX);
}
%>
--------------------
Remember, use the database connection to be released.
Otherwise when the open connection is too much or maliciously brush screen, the number of connections to the connection pool will be used.
When the number of connections of the connection pool is used, when the JSP page is accessed, the following error message will appear on the page, and the connection to the connection pool is exhausted:
Java.sql.sqlexception: DBCP COULD NOT OBTAIN An iDLE DB Connection, Pool Exhausted
Second, Win2k's Tomcat5 connection pool configuration Install jakarta-tomcat-5.0.13.exe, if Tomcat5 installation directory is D: / Tomcat 5.0.
Don't forget to put Oracle's driver Classes12.jar file COPY to the directory D: / tomcat 5.0 / circon / lib / under.
Tomcat5's connection pool settings and Tomcat4 are similar, and only different points or particular attention is required.
1. Tomcat5 is started, visit
http://127.0.0.1:8080/admin page, when setting the Tomcat connection pool data source, open the D: / Tomcat 5.0 / conf / server.xml file, it will see the configuration connection pool related information is approximately as follows :
parameter>
parameter>
parameter>
parameter>
parameter>
parameter>
parameter>
Resourceparams>
parameter>
parameter>
Resourceparams>
Globalnamingresources>
It can be seen that some connection pools have been different from Tomcat4.
However, the rough configuration steps and methods are the same.
2. Set the reference to the connection pool.
Open the D: / Tomcat 5.0 / conf / catalina / localhost / directory, find the XML file (MyPool.xml) with the web app, open this file,
Repair this description of the
Make the content of mypool.xml files rough this:
Context>
Then save MyPool.xml, you need to re-Tomcat5 service to take effect.
3. JSP page Test file is also the same, no longer described again.
4. Some supplements:
If the web application does not configure reference to the connection pool, no
NameNotFoundException: Name Jdbc is not bound in this context
If the Oracle's JDBC driver is missing, the following error occurs when the JSP page is accessed:
Java.sql.sqlexception: Cannot Load JDBC Driver Class 'Oracle.jdbc.driver.OracleDriver'