Tomcat45 Connection Pool Settings and Simple Application Examples

xiaoxiao2021-04-03  209

I. Configuration of Tomcat4's connection pool 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. Setting the Database to the Oracle's JDBC Driver Set to: Oracle8i's driver is 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: -> In this, add the following: is completed, becoming this: -> Save the D: / Tomcat 4.1 / conf / server.xml file. After the configuration is complete, restart Tomcat can be accessed this way: 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. 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. In the drop-down box in the upper right corner, select "Create New Data Source", 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.racledriver user name: MyName Password: mypassword max. Active Connections: Available default max. Idle Connections: Available Default max. Wait for Connection: Available default values ​​Validation Query: Do not fill in. 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 - changed to conf / server.xml This time the D: / tomcat 4.1 / conf / server.xml file configuration 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 -> is automatically modified to this: good At this position, the application connection pool for Oracle has been configured.

Carefully control the Server.xml file, it will find that the connection pool information just configured is placed in the tab, which means that this information can be globally referenced, which is approximately as follows: < Resource auth = "Container" description = "User database that can be updated and saved" name = "UserDatabase" scope = "Shareable" type = "org.apache.catalina.UserDatabase" /> maxwait 5000 maxactive 200 Password mypassword URL jdbc: Oracle: Thin: @ 192.168.6.40: 1521: dbserver driverclassname Oracle.jdbc.driver.Orcledriver maxidle 20 username myname

"UserDatabase"> factory org.apache.catalina.users.MemoryUserDatabaseFactory Pathname confes /Tomcat-Users.xml 5. Sets the 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 removes a lot of comment content, so the web application just configured is automatically modified as this: setting after completion, the web application substantially as described in the following way: saves 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. [:(] (] At that time, I only checked a little bit. Because it is simple to configure a connection pool. [:(] 6. Under the case, here, the settings of the connection pool are completed. Then you need to restart Tomcat. This can be used to work using the connection pool. However, I saw a lot of article, I need to set up web.xml to apply. I didn't set up Web.xml, the connection pool can also be fully used. Now let's talk about web.xml Configuration method. In D: / MyPool, create a new folder, named Web-INF, pay attention to a capital letter. Then in D: / MyPool / Web-INF / Create New File Web.xml, its content is: Oracle DataSource Example JDBC / MyorCl javax.sql.datasource Container Save D: /mypool/web-inf/web.xml file .7. A simple application example. Write A simple JSP page, use the connection pool to access the database.

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 initX = new initialContext (); if (); 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 Match 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)); // Close ResultSet Stmt.close (); // Turn 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, the connection to the connection pool is exhausted: java.sql.sqlexception: DBCP COULD NOT OBTAIN An IDLE DB Connection, Pool EXHAUSTED 2 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. After Tomcat5 is started, visit http://127.0.0.1:8080/admin page, after setting the Tomcat connection pool data source, open the D: / Tomcat 5.0 / conf / server.xml file, you will see the configuration Connection pool related information is approximately as follows: maxwait 5000 maxactive 4 Password mypassword URL jdbc: Oracle: Thin: @ 127.0.0.1: 1521: dbserver driverclassname oracle.jdbc.driver.Orcledriver < Parameter> maxidle 2 username myusername < ResourceParams Name = "UserDatabase">

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

New Post(0)