Tomcat4.1.31 Database Connection Pool configuration

Source: Internet
Author: User
Tags auth object sql server driver sql mysql new features stmt version
Data | database | database connection
(Excerpt from Java Chinese station)

This article is the author in the configuration of the blog of Brother Liao used, just started using Tomcat5.0.27, and then use Tomcat5.5.4, but the configuration database always have errors, and later found that the context flag was canceled. Luben temperament of the human, can not change, so the use of Tomcat4.1.31. The main reference Fanyhan and other related articles, introduced Tomcat4.1.31 this version of the database connection pool configuration, and program to the connection pool Jndi lookup, and provide the corresponding test code. Finally, the paper points out the common problems and solutions in the process of configuration and application. One, Tomcat profile Tomcat is one of the Jakarta of Apache and is a recommended JSP, servlet container for sun company. As an excellent application server, Tomcat provides a database connection pool, SSL, proxy, and many other common component functions, including the connection pool is more than 4.0 version of the new features, the application is very extensive. Second, the article Configuration environment Tomcat4.1.31 + jdk1.4.2 + SQL Server C + Win2000 as III, DBCP configuration for Tomcat4.1.31 here, place the connection pool in the blog subfolder of the Tomcat's Engineering directory (new ), the Jndi name is set to Jdbc/blog, the database server IP is localhost,sid as the blog, and the configuration steps are as follows. Step One: Configure Server.xml to find <!--Tomcat Root context--> <! in Server.xml? <context path= "" docbase= "ROOT" debug= "0"/>--> change it to: <context path= "/blog" docbase= "blog" debug= "0" Reloadable= "true" > <resource name= "jdbc/blog" auth= "Container" type= "Javax.sql.DataSource"/> < Resourceparams name= "Jdbc/blog" > <parameter> <name>factory</name> <value> Org.apache.commons.dbcp.basicdatasourcefactory</value> </parameter> <parameter> <name>driverClassName</name> <value> com.microsoft.jdbc.sqlserver.sqlserverdriver</value> </parameter> <parameter> <name>url </name> <value>jdbc:microsoft:sqlserver://localhost:1433;databaseName=blog</value> </ parameter> <parameter> <name>username</name> <value>sa</value> </parameter> <parameter> <name>password</name> <value>sa</value> </parameter> <parameter > <name>maxActive</name> <value>20</value> </parameter> <parameter> <name >maxIdle</name> <value>20</value> </parameter> <parameter> <name>maxwait </name> <value>-1</value> </parameter> </ResourceParams> </Context> parameter Description: Resource: <resource name= "Jdbc/blog" auth= "Container" type= "Javax.sql.DataSource"/> The resource item (that is, the DataSource object of the connection pool) has 3 properties name, auth, type,nThe AME item is a JNDI name definition that can be found by the program through Jndi, where the name Jdbc/sblog;auth the connection Pool Admin property, where the value container is declared for container management; Here is the value Javax.sql.DataSource, declared as the database connection pool. The next <ResourceParams> field content contains four parameters user, password, driverclassname, drivername, followed by the database username, password, JDBC driver, and database address. Factory parameters: <parameter> <name>factory</name> <value> Org.apache.commons.dbcp.basicdatasourcefactory</value> </parameter> is the base object factory, Here the value of the org.apache.commons.dbcp.BasicDataSourceFactory, that is, the dbcp from the factory, can also use other. Driverclassname parameters: <parameter> <name>driverClassName</name> <value> com.microsoft.jdbc.sqlserver.sqlserverdriver</value> </parameter> the JDBC driver name for the database, which is: SQL Server 2000:c Om.microsoft.jdbc.sqlserver.SQLServerDriver First download the install sqlserver-jdbc-driver, and then put the three jar files under its lib under Tomcat/common/lib. MySql:org.gjt.mm.mysql.Driver MySQL's JDBC driver package Mm.mysql-2.0.14.jar. The JDBC driver package for Oracle8.1.7:oracle.jdbc.driver.oracledriver Oracle8.1.7 is named Classes.jar, which is typically located under the Ora81\jdbc\lib directory under the Oracle installation directory. The initial extension is zip, and you need to manually change the name of Classes.zip toClasses.jar, and put it under Tomcat/common/lib. Oracle.jdbc.driver.OracleDriver This class is provided by Classes.jar. URL parameters: <parameter> <name>url</name> <value>jdbc:microsoft:sqlserver://localhost:1433; Databasename=blog</value> </parameter> is the address of the database. (Different database has the same address) Username parameters: <parameter> <name>username</name> <value>sa</value> </ Parameter> is the user name that connects to the database. Password parameters: <parameter> <name>password</name> <value>**</value> </parameter> The password that connects the database. Maxactive, Maxidle and maxwait parameters: <parameter> <name>maxActive</name> <value>20</value> </parameter> <parameter> <name>maxIdle</name> <value>20</value> </parameter > <parameter> <name>maxWait</name> <value>-1</value> </parameter> Maxactive is the maximum number of active connections, where a value of 20 indicates that there are up to 20 database connections. The Maxidle is the largest number of idle connections, with a value of 20, which means that even if there is no database connection, 20 idle connections can be maintained without being purged and ready for standby. Maxwait is the maximum number of wait seconds, here is a value of 1, which means infinite wait until the timeout, but also the value of 9000, which means timeout after 9 seconds. Step Two: Configure Web.xml to open Webapps/root/web-inf Web.xml, add the following: <resource-ref> <description>sqlserver Datasource Example</description> <res-ref-name>jdbc/blog</res-ref-name> <res-type> Javax.sql.datasource</res-type> <res-auth>Container</res-auth> </resource-ref> Part III: Configure Tomcat (add Class) to copy the three jar files under the Microsoft SQL Server Driver for jdbc\lib directory under the Common\lib of the Tomcat installation directory. Configuration Complete!! The test code database is as follows: Create a new database called blog, creating a table inside: "CREATE TABLE test" (ID varchar (), name varchar (30)); Write a JSP file (testdb.jsp), place it under the Webapps/root directory, and open Tomcat to run the page for testing. Page testdb.jsp reads as follows: <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%> <%@ page import= "java.sql.*"%> <%@ Page import= "javax.naming.*"%> <% try{Context initctx = new InitialContext (); Context CTX = (context) initctx.lookup ("java:comp/env"); Gets the connection pool object obj = (object) ctx.lookup ("Jdbc/blog"); Type conversion Javax.sql.DataSource ds = (javax.sql.DataSource) obj; Connection conn = Ds.getconnection (); Statement stmt = Conn.createstatement (); String strSQL = "INSERT INTO Test (id,name) VALUES (´00001´,´fany´)"; Stmt.executeupdate (strSQL); strSQL = "Select Id,name from Test"; ResultSet rs = stmt.executequery (strSQL); if (Rs.next ()) {out.println (rs.getstring (1)); Out.println (Rs.getstring (2));}} catch (Exception ex) {ex.printstacktrace (); throw new SQLException ("Cannot get Connection pool.");%> run Tomcat, open the browser,   Enter http://localhost:8080/blog/test.jsp and normally see "00001 fany" output on the page.


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.