tomcat connection pool configuration

This article is reproduced

1. The Tomcat version I am currently using is: 6.0.20, and the oracle is the stable 9i version

2. For the sake of convenience, I will use %Tomcat_Home% to represent the directory where Tomcat is installed. My installation directory is "E:\ "Program Files\WindowsXP\tomcat6"

configuration steps are as follows:

1. The configuration of Tomcat 6 is different from the previous one. It is not recommended to configure it in server.xml, but in %Tomcat_Home%\webapps\yourApp\META-INF \context. xml configuration is a better way. Instead of the context.xml file under the previous version %Tomcat_Home%\conf. In this way, the connection pool can be configured separately under different web applications, and Tomcat will automatically reload. Of course, you can also change the context.xml file under %Tomcat_Home%\conf to uniformly configure the connection pools under all web applications.

2. Modify the code as follows:

view plaincopy to clipboardprint?
<Context reloadable="true"> 
    <WatchedResource>WEB-INF/web.xml</WatchedResource> 
    <Resource name="jdbc/oracleds" auth="Container" type= "javax.sql.DataSource"   
    maxActive="100"   
    maxIdle=" 

    username="scott"   
    password="tiger" 
    driverClassName="oracle.jdbc.driver.OracleDriver" 
    url="jdbc:oracle:thin:@localhost:1521:ora9"/> 
</Context> 
<Context reloadable="true">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/oracleds" auth="Container" type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="scott"
password="tiger"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:ora9"/> type is the resource type; auth is the authentication method; name is the name of the current data source JNDI, which can be set at will;
</Context>







driverClassName is the Oracle driver reference;

maxActiv is the maximum number of active connections in the connection pool, set to 0 for unlimited;

maxIdle is the maximum number of idle connections in the connection pool, and the maximum idle time for database connections. Beyond the idle time, the

            database connection will be marked as unavailable and then released. Set to 0 means unlimited;

maxWait is the maximum waiting time for the connection, in milliseconds, if it exceeds this time, an exception will be received. Set to -1

              for unlimited. ;

username is a user name of the oracle database;

password is the password of username;

url is the connection address to connect to oracle;

Note: I tried to change the code "driverClassName="oracle.jdbc.driver.OracleDriver" to "driverClassName="oracle The .jdbc.OracleDriver"" program is still running normally. At first, I thought there was something wrong with the teacher's code.

3. The calling form in the program is:

view plaincopy to clipboardprint?
Context context = new InitialContext();  
DataSource ds = (DataSource)context. lookup("java:/comp/env/jdbc/oracleds");  
Connection conn = ds.getConnection(); 
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
Connection conn = ds.getConnection();

Note: "java:/comp/env /jdbc/oracleds" red marked text is the Resource name set in step 1, the

connection establishment method can be replaced by the traditional method in the above form:

view plaincopy to clipboardprint?
String driver = "oracle.jdbc.driver.OracleDriver";  
String url = "jdbc:oracle:thin:@localhost:1521:ora9";  
String username = "scott";  
String password = "tiger";  
Class.forName(driver);  
Connection conn = DriverManager.getConnection(url, username, password) ; 
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin: @localhost:1521:ora9";
String username = "scott";
String password = "tiger"; Class.forName
(driver);
Connection conn = DriverManager.getConnection(url, username, password);

%Tomcat_Home%\lib directory,

otherwise the following exception will be thrown:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'

Follow the above steps to successfully configure Tomcat6. 0 Connection pool, and some netizens have posted that the following code form needs to be added under the web-app node

in the web.xml file:
<resource-ref>
<res-ref-name>jdbc/myoracle</res-ref-name >
<res-type>javax.sql.DataSource</res-type>
</resource-ref>

Because I did not add this item, the program is still correct, so I think this step is unnecessary



------- -------------------------------------------------- ------------------


Today, I need to redeploy the system on another machine and reset the connection pool of db. When I copied tomcat to that machine, and modified META-INF\context.xml, then restarted tomcat, but found that the system is still connected to the old db. Checked the META-INF\context.xml file again and made sure that the db connection has been set up correctly in this file, which also means that this file is not really working. Checked the conf\context.xml under tomcat and did not set the connection pool of db. Which file is that working? After tossing around, I found that tomcat generated a file with the same content as the original META-INF\context.xml under conf\Catalina\localhost. I suspect that this file must be working? Delete the conf\Catalina\localhost directory, restart tomcat, the problem disappears.

Reprint an order of tomcat loading classes:





bin: store the script files for starting and closing tomcat;

/conf: store various configuration files of tomcat, such as: server.xml

/server/lib: store various jars required by the tomcat server File (jar file can only be accessed by tomcat server)

/server/webapps: Stores two web applications that come with tomcat: admin application and manager application.

/common/lib: Stores the jar folder accessible by the tomcat server and all web applications (both web and tomcat servers can access this jar)

/shared/lib: Stores the jar files accessible by the web. (can be accessed by all webs, but not by tomcat)

/logs: store the log files of tomcat

/webapps: when publishing web applications, the web application files are placed in this directory by default

/work: tomcat puts the servlet generated by jsp in this directory. In

addition , in the web application, under the WEB-Inf directory, a lib subdirectory can also be established. Various jar files can be stored in this subdirectory. These jar files only Can be accessed by the current web application. Among them, in the lib and classes directories in the web-inf directory, the Tomcat class loader first loads the classes in the classes directory, and then loads the classes in the lib directory. Because classes have the same name, classes take precedence.



When jsp is running, the order of finding classes is: project folder (WEB-INF\lib) ===>container folder (tomcat\common\lib)==>jdk folder (jdk\jre\lib\ext)



Tomcat's class loading priority list

1. The first is the jar file under $JAVA_HOME/jre/lib/ext/.

2. The jar and class files in the environment variable CLASSPATH.

3. The class file under $CATALINA_HOME/common/classes.

4. The jar file under $CATALINA_HOME/commons/endorsed.

5. The jar file under $CATALINA_HOME/commons/i18n.

6. The jar file under $CATALINA_HOME/common/lib.

(Jar files such as JDBC drivers can be placed here, so as to avoid the situation where the JDBC Driver cannot be found after configuring the data source in server.xml.)

7. The class file under $CATALINA_HOME/server/classes.

8. The jar file under $CATALINA_HOME/server/lib/.

9. The class file under $CATALINA_BASE/shared/classes.

10. The jar file under $CATALINA_BASE/shared/lib.

11. The class files under their specific webapp /WEB-INF/classes.

12. The jar files under the respective specific webapp /WEB-INF/lib.



The search order for classes is as follows

-------------

/WEB-INF/classes of your web application

/WEB-INF/lib/*.jar of your web application

$CATALINA_HOME/common/classes

$CATALINA_HOME /common/endorsed/*.jar

$CATALINA_HOME/common/i18n/*.jar

$CATALINA_HOME/common/lib/*.jar

$CATALINA_BASE/shared/classes

$CATALINA_BASE/shared/lib/*.jar

------ --------

Therefore, the class files placed in different webapps will be loaded into different instances by the classloader.

For example, suppose the following two classes with different content. They are placed in the class directory of different webapps.

package com.lizongbo;

public class TestClass {

  private String NAME="lizongbo";

}



package com.lizongbo;

public class TestClass {

  private String NAME="li_zongbo";

}



The result of com.lizongbo.NAME obtained in different webapps is different , and do not affect each other.

Note, however, that classes starting with the following package names are exceptions:

javax.*

org.xml.sax.*

org.w3c.dom.*

org.apache.xerces.*

org.apache.xalan.*



ps, note. in each jar The Class-Path key-value pair in the \META-INF\MAINFEST.MF file will also provide the jar loading priority.

For example, the MAINFEST.MF content of a jar is as follows:

Manifest-Version: 1.0

Created-By: lizongbo

Class-Path: commons-beanutils.jar

Class-Path: commons-collections.jar

Class-Path: commons-dbcp.jar

Class-Path: commons-digester.jar

Class-Path: commons-logging.jar

Class-Path: commons-pool.jar

Class-Path: commons-services.jar

Class-Path: commons-validator.jar

Class-Path: jakarta -oro.jar

Main-Class: com.lizongbo.MyTestClass





Then when loading this jar, it will first load commons-beanutils.jar and commons-collections.jar in the directory where the jar is located. . . and other jar files.



Placing jars and classes in different places may have unintended consequences, especially with different versions of jar files, so be careful when deploying web applications in actual applications.



For example, a common error message when using javamail:

javax.mail. NoSuchProviderException: No provider for smtp

The real reason is probably as follows:

different versions of mail.jar are placed in different jar loaded directories, such as a mail.jar of javamail1.3.1

in D:\jakarta-tomcat-5.5 .8\common\lib, and the other is the mail.jar of javamail1.3.2 in

D:\jakarta-tomcat-5.5.8\webapps\lizongbo\WEB-INF/lib,

then when the webapp of Lizongbo uses javamail to send emails, the error of No provider for smtp will appear.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326844676&siteId=291194637