Tomcat4 database connection pool configuration

zhaozj2021-02-16  50

Tomcat4 database connection pool configuration add by: Fanyhan This article mainly introduces the configuration of the database connection pool under Tomcat4.1.19, 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. I. Tomcat introduction Tomcat is one of the subprojects of Apache Jakarta, which is a JSP recommended by Sun. As an excellent application service

Merries, Tomcat provides many universal component features such as database connection pool, SSL, proxy, where connecting pools are newly added in 4.0 or higher.

Can, the application is very wide. Second, the text configuration environment Tomcat4.1.19 jbuilder 8.0 win2000 three, the DBCP configuration for Tomcat4.1.19 is convenient, set the connection pool in root, JNDI name is JDBC / SQLServerDB , Database server IP is

192.168.0.47, SID is HRNTEST, the configuration steps are as follows. Step 1: Configure server.xml to find "0" /> -> "" "DocBase =" root "" "

Change it to factory

Org.apache.commons.dbcp.basicDataSourceFactory driverclassname

com.microsoft.jdbc.sqlserver.sqlserverdriver URL jdbc: Microsoft: SQLServer: //192.168.0.47: 1433; DataBA

SENAME = HRNTEST username fany password Fanyhan < / value> maxactive 20 maxidle 20 < / parameter> maxwait -1 parameter description: Resource item:

Resource item (ie the DataSource object of the connection pool), there are 3 attributes name, auth, type, name item is the name definition of JNDI,

The program can find this object through JNDI, here is named JDBC / SQLServerDB; Auth item, connect pool management rights attribute, here

Container, stated that it is container management; Type item is the type of object, this is valued here javax.sql.datasource, this is declared as a database

Pell. In the next domain contains four parameters User, Password, DriverclassName, Drivername,

The user name, password, JDBC driver, and database address of the database are sequentially. Factory parameters: factory org.apache.commons.dbcp.basicDataSourceFactory

That is, the base object factory, this is valued in org.apache.commons.dbcp.basicDataSourceFactory, the factory that comes with DBCP, also

Can be used. Driverclassname parameters: driverclassname com.microsoft.jdbc.sqlserver.sqlserverDriver

That is, the Database's JDBC driver name, the specific value is: SQL Server 2000: com.microsoft.jdbc.sqlserver.sqlserverDriver first to download the installation SQLServer-JDBC-driver, then put the three JAR files under LIB in Tomcat / Common / Lib. Mysql: org.gjt.mm.mysql.driver mysql JDBC driver package mm.mysql-2.0.14.jar. Oracle8.1.7: Oracle.jdbc.driver.OracleDriver Oracle8.1.7 JDBC driver package name is classs.jar, generally located in the Oracle installation directory

Under the ORA81 / JDBC / LIB directory, the initial extension is ZIP, need to manually change the classs.zip to classes.jar, and put it

Under Tomcat / Common / LIB. Oracle.jdbc.driver.Orcledgedriver This class is provided by classes.jar. URL parameters: URL

JDBC: Microsoft: SQLServer: //192.168.0.47: 1433; DatabaseName = HRNTEST

That is, the address of the database. (Different databases are different) SQL Server 2000: JDBC: Microsoft: SQLServer: //192.168.0.47: 1433; DatabaseName = HRNTEST ORACLE8.1.7: JDBC: Oracle: Thin: @ 192.168.0.47: 1521: HRNTESTUSERNAME parameters :

username fany

That is, the username of the database is connected. Password Fanyhan

Password parameter:

That is, the password of the database is connected. MaxActive, Maxidle and Maxwait Parameters: maxactive 20 maxidle 20 < / parameter> maxwait -1

Maxactive is the maximum number of connections, which is 20, indicating that there are up to 20 databases at the same time. MaxIdle is the maximum number of idle connections, here is 20, indicating that it can still be kept 20 idle even when there is no database connection.

Connection, not cleared, is in standby. MaxWait is the maximum number of seconds, here is all -1, indicating unlimited waits until the timeout can be taken, and the value is 9000, indicating 9 seconds. 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

Part III: Configuring Tomcat (add classes) Copy the three JAR files in the Microsoft SQL Server 2000 Driver for JDBC / LIB directory to the Tomcat installation directory

COMMON / LIB. Configure completion

Fourth, the test code database is as follows: Create Table Test (ID varchar (12)) method 1: Write a JSP file (TestDb.jsp), put it in a WebApps / root directory, open Tomcat Run Page test

test.

The contents of the page TestDb.jsp are 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"); // Get connection pool object Object obj = (Object) CTX.lookup ("JDBC / SQLServerDB "); // 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 "; resultSet RS = stmt.executeQuery (strsql); if (rs.next )) {Out.println (rs.getstring (1)); out.println (rgetstring (2));}}}} catch (exception ex) {EX.PrintStackTrace (); throw new SQLEXCEPTION ("Cannot Get Connection Pool ");}%> Method 2: Write a database connection class content as follows:

Static string jndiname = "jdbc / sqlserverdb";

/ * ** acquired from an idle database connection pool * @return Connection * @exception Exception * / public static Connection getConnection () throws Exception {Connection conn = null; try {Context initCtx = new InitialContext (); Context ctx = (Context) INITCTX.LOOKUP ("Java: Comp / ENV"); // Gets the connection pool object Object obj = (Object) CTX.lookup (JNDINAME); // Type conversion javax.sql.dataSource DS = (Javax. Sql.datasource) OBJ; // Get the database connection conn = ds.getConnection ();} catch (java.lang.securityException se) {throw se;} catch (exception e) {E.PrintStackTrace (); throw e;} Return conn;

}

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

New Post(0)