http://blog.163.com/magicc_love/blog/static/185853662201111101130969/
Oracle Driver Pack Tomcat 6.0 configuration Oracle database connection pool
After installing Tomcat, add the following environment variable (assuming your Tomcat is installed in C:\tomcat) to the environment variable system variable, my computer-Properties--
Catalina_home:c:\tomcat
Catalina_base:c:\tomcat
Tomcat_home:c:\tomcat
Then modify the environment variables in the CLASSPATH, the Tomcat installation directory under \lib Servlet-api.jar and Jsp-api.jar appended to the CLASSPATH, the modified classpath as follows:
Classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar;%catalina_home%\lib\servlet-api.jar;%catalina_home%\lib\ Jsp-api.jar
TomCat6.0 database Connection Pool Configuration instance (Oracle 11g database)
TomCat6.0 database Connection Pool configuration There are several ways, here I introduce a common method for everyone to refer to:
The configuration process is divided into three steps to complete (global data source):
First step: Locate the Context.xml configuration file in the TomCat6.0 directory. (Example: D:program files/apache-tomcat-6.0.14/conf/context.xml) then open Context.xml, in the label <context>/</context> Add the following: (For ease of understanding, I have made a more detailed comment on the configuration)
Resource name= "Jdbc/dbpool"!--the name of the data source, preferably a meaningful name--
Auth= "Container"!--This default without modification--
Type= "Javax.sql.DataSource"!--This default without modification--
Driverclassname= "Oracle.jdbc.driver.OracleDriver"!--here is the driver package for the Oracle database. --
Url= "Jdbc:oracle:thin: @localhost: 1521:orcl"!--here is the URL to connect to the Oracle database, where url= "jdbc:oracle:thin:@" is fixed, Zygora is the SID of my Oracle database if accessing the Oracle database is not on this computer should change localhost to the IP address of the machine where the Oracle database resides. --
Username= "Scott"!--user name of the login database-
password= "Tiger"!--Login Database Password-
Maxidle= "5"-Maximum idle number, set to 0 means no limit-
maxwait= "-1"-Maximum connection wait time, if timeout will be received exception, set to 1 means no limit-
Maxactive= "10"-the maximum number of database connections for the connection pool, set to 0 for unlimited--/
The second step: Download the Oracle driver package Ojdbc14.jar, copy to the TomCat6.0 directory in the Lib directory. (Example: D:program files/apache-tomcat-6.0.14/lib) Ojdbc14.jar Oracle Official website I can't open it here, csdn. There is a download of the jar package.
The third step: where the database connection is required in the program, you need to write the following code:
Import Javax.sql.datasource;import javax.naming.*;
public class Getconnectiontest{public static void Main (String args) {
try{
Context ctx=new InitialContext ();
DataSource ds= (DataSource) context.lookup ("Java:/comp/env/jdbc/oracleds"); Connection conn=ds.getconnection ();
}catch (Exception e) {
E.printstacktrace ();
}
}
1. (Local data source) set the DataSource to our Web project, under the detailed description below:
Step one: Create a context.xml under the Meta-inf folder in our Web project
XML code
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<Context>
<resource name= "Jdbc/mysql"
Auth= "Container"
Type= "Javax.sql.DataSource"
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost/bbs"
Username= "Root"
password= "Root"
Maxactive= "50"
Maxidle= "20"
maxwait= "10000"/>
</Context>
Step Two: Create a Web. XML under the Web-inf folder under our website (if it exists, you can modify it directly)
(These days test a bit, do not do this step can also, O (∩_∩) o haha ~ convenient)
XML code
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Step three: We can use the code to get the connection object.
Java code
Package xushun.util;
Import java.sql.*;
Import javax.sql.*;
Import javax.naming.*;
public class DBHelper {
public static Connection getconnection () throws Sqlexception,namingexception
{
Initializing the Lookup namespace
Context initcontext = new InitialContext ();
Context Envcontext = (context) initcontext.lookup ("java:/comp/env");
Find DataSource
DataSource ds = (DataSource) envcontext.lookup ("Jdbc/mysql");
return Ds.getconnection ();
}
}
2. (Global data source) set DataSource to our Tomcat, under the detailed introduction below (the Java code of the test is not listed as above):
The Setup method I found here is a bit different. Some people put DataSource under the globalnamingresources of Tomcat's Server.xml file and then map it in Context.xml. Some of them are written directly in the context.xml.
First, add DataSource in Server.xml.
First step: Found in the Server.xml file in Tomcat's conf
XML code
<GlobalNamingResources>
<!--Editable User database that can also is used by
Userdatabaserealm to authenticate users
-
<resource name= "Userdatabase" auth= "Container"
Type= "Org.apache.catalina.UserDatabase"
description= "User database that can be updated and saved"
factory= "Org.apache.catalina.users.MemoryUserDatabaseFactory"
Pathname= "Conf/tomcat-users.xml"/>
</GlobalNamingResources>
Revision changed to
XML code
<GlobalNamingResources>
<!--Editable User database that can also is used by
Userdatabaserealm to authenticate users
-
<resource name= "Userdatabase" auth= "Container"
Type= "Org.apache.catalina.UserDatabase"
description= "User database that can be updated and saved"
factory= "Org.apache.catalina.users.MemoryUserDatabaseFactory"
Pathname= "Conf/tomcat-users.xml"/>
<resource name= "Jdbc/bbs"
Auth= "Container" type= "Javax.sql.DataSource"
Driverclassname= "Com.mysql.jdbc.Driver"
Maxidle= "20"
maxwait= "5000"
Username= "Root"
password= "Admin"
Url= "Jdbc:mysql://localhost:3306/bbs"
Maxactive= "100"
Removeabandoned= "true"
removeabandonedtimeout= "60"
Logabandoned= "true"/>
</GlobalNamingResources>
Step Two: Add the context.xml in the Tomcat Conf folder
XML code
<resourcelink name= "Jdbc/bbs" global= "Jdbc/bbs" type= "Javax.sql.DataSource"/>
The third step is to add the Web. XML in Web-inf
XML code
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
There is the method mentioned in the tomcat documentation that directly modifies the context.xml file.
Add in Context.xml under Tomcat's Conf folder
XML code
<resource name= "Jdbc/bbs"
Auth= "Container" type= "Javax.sql.DataSource"
Driverclassname= "Com.mysql.jdbc.Driver"
Maxidle= "20"
maxwait= "5000"
Username= "Root"
password= "Admin"
Url= "Jdbc:mysql://localhost:3306/bbs"
Maxactive= "100"
Removeabandoned= "true"
removeabandonedtimeout= "60"
Logabandoned= "true"/>
Then there is the Web. XML add in the Web-inf
XML code
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Example:
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
"Http://www.w3.org/TR/REC-html40/strict.dtd" >
<%@ page import= "java.sql.*"%>
<%@ page import= "javax.sql.*"%>
<%@ page import= "javax.naming.*"%>
<%@ page session= "false"%>
<%@ page contenttype= "text/html;charset=gb2312"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title></title>
<%
DataSource ds = null;
Connection Conn=null;
Statement Stmt=null;
try{
InitialContext ctx=new InitialContext ();
Ds= (DataSource) ctx.lookup ("Java:comp/env/mydatasource");
conn = Ds.getconnection ();
stmt = Conn.createstatement ();
String strSQL = "SELECT * from user";
ResultSet rs = stmt.executequery (strSQL);
while (Rs.next ()) {
Out.print ("User name is. "+rs.getstring (1));
}
if (rs!=null) {rs.close ();}
if (stmt!=null) {stmt.close ();}
if (conn!=null) {conn.close ();}
}
catch (Exception e) {
E.printstacktrace ();
}
%>
<body>
</body>
--------------------------------------------------------------------------------------------------4) maxactive The maximum number of database connections for the connection pool. Set to 0 to indicate no limit.
5) Maximum idle time for Maxidle database connections. When this idle time is exceeded, the database connection is marked as unavailable and then released. Set to 0 to indicate no limit.
6) maxwait Maximum setup connection wait time. If this time is exceeded, the exception will be received. Set to-1 to indicate no limit.
7) removeabandoned collection of abandoned (usually forgotten) databases are connected to the connection pool.
8) Removeabandonedtimeout database connection for too long does not have to be considered abandoned and retracted in the connection pool.
9) logabandoned logs the abandoned database connection.
) Driverclassname the JDBC driver.
One) URL database connection string
Here are the necessary explanations for the parameters:
1) Description A description of the referenced resource.
2) res-ref-name resource name. See above <resourceparams name= "Jdbc/connectdb" >
3) Res-type resource type. See above <resource name= "Jdbc/connectdb" auth= "Container" type= "Javax.sql.DataSource"/>
Tomcat6.0 Database Connection Pool configuration