Configure the database connection pool and Tomcat6.0 connection pool

Source: Internet
Author: User

Tomcat6.0 connection pool configuration 1. My current useTomcatVersion: 6.0.20. oracle is a stable 9i version.
2. For convenience, % Tomcat_Home % is used to indicateTomcatInstallation directory. The installation directory is "E: \ Program Files \ WindowsXP \Tomcat6"
The configuration steps are as follows:
1. the configuration of Tomcat 6 is different from the previous one. It is not recommended on the server. configure in xml, but in % atat_home % \ webapps \ yourApp \ META-INF \ context. configuration in xml is a better way. Instead of the context. xml file in the previous version % atat_home % \ conf. In this way, the connection pool can be configured separately for different web applications, andTomcatWill be automatically reloaded. Of course, you can also change the context. xml file under % atat_home % \ conf to configure the connection pools of all web applications in a unified manner.
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 = "30"
MaxWait = "10000"
Username = "scott"
Password = "tiger"
DriverClassName = "oracle. jdbc. driver. OracleDriver"
Url = "jdbc: oracle: thin: @ localhost: 1521: ora9"/>
</Context>

The name is the name of the current data source JNDI, which can be set at will;
Auth is the authentication method;
Type resource type;
DriverClassName is an Oracle driver reference;
MaxActiv is the maximum number of activated connections in the connection pool. If it is set to 0, there is no limit;
MaxIdle indicates the maximum number of idle connections in the connection pool and the maximum idle time of the database connection. Idle time exceeded,
The database connection is marked as unavailable and then released. If it is set to 0, there is no limit;
MaxWait is the maximum wait time for a connection, in milliseconds. If the wait time is exceeded, an exception is thrown. Set to-1
Indicates no limit .;
Username is a username of the oracle database;
Password is the password of username;
The url is the connection address to connect to oracle;
Note: I tried to run the code "driverClassName =" oracle. jdbc. driver. change OracleDriver "to" driverClassName = "oracle. jdbc. oracleDriver "" The program is still running normally. At first, I thought there was a problem with the instructor's code.
3. The call 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" indicates the Resource name set in step 1 in red.
In this case, you can replace the traditional method with the method described above:
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 );
4. You also need to import the jdbc driver Library to the % atat_home % \ lib directory.
Otherwise, the following exception is thrown:
Org. apache. tomcat. dbcp. dbcp. SQLNestedException: Cannot load JDBC driver class 'oracle. jdbc. driver. OracleDriver'
Configuration can be successful according to the above stepsTomcat6.0 connection pool, some netizens post the need
Add the following code to 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 not necessary
---------------------------------------------------------------------------
Today, you need to redeploy the system on another machine and reset the database connection pool. WhenTomcatCopy to that machine, modify META-INF \ context. xml, and restartTomcatBut the system is still connected to the old db. Checking the META-INF \ context. xml file again and making sure that the file has correctly set the db connection does not actually work. ViewTomcatNo db connection pool is set for conf \ context. xml. Which file is working? After a hard job, I found thatTomcatUnder conf \ Catalina \ localhost, a file with the same content as the original META-INF \ context. xml is generated. Are you sure this file is working? Delete the conf \ Catalina \ localhost directory and restartTomcatThe problem disappears.
Repost an articleTomcatOrder of classes to load:
Bin: store startup and ShutdownTomcatScript file;
/Conf: StorageTomcatConfiguration files, such as server. xml
/Server/lib: StorageTomcatThe various jar files required by the server (jar files can only beTomcatServer access)
/Server/webapps: StorageTomcatTwo built-in web applications: the admin application and the manager application.
/Common/lib: StorageTomcatThe jar folder (web andTomcatThe server can access this jar)
/Shared/lib: stores jar files accessible to the web. (Can be accessed by all web, but cannot be accessedTomcatAccess)
/Logs: StorageTomcatLog File
/Webapps: When a web application is published, the web application files are stored in this directory by default.
/Work:TomcatStore the Servlet generated by jsp in this directory
In addition, you can create lib sub-directories under the web-Inf directory for WEB applications. Various jar files can be stored in these sub-directories. These jar files can only be accessed by the current web application. The lib and classes directories under the web-inf directory,TomcatThe Class Loader first loads the classes under the classes directory, and then the classes under the lib directory. Because the classes takes precedence over the same name.
When jsp is running, the order of the Search class is: project folder (WEB-INF \ lib) ===" container folder (Tomcat\ Common \ lib) = jdk folder (jdk \ jre \ lib \ ext)
TomcatClass loading priority list
1. The first is the jar file under $ JAVA_HOME/jre/lib/ext.
2. jar and class files in the environment variable CLASSPATH.
3. $ CATALINA_HOME/common/classes class file.
4. $ CATALINA_HOME/commons/endorsed jar file.
5. $ CATALINA_HOME/commons/i18n.
6. $ CATALINA_HOME/common/lib.
(Jar files such as the JDBC Driver can be placed here, so that the JDBC Driver cannot be found after the data source is configured in server. xml .)
7. $ CATALINA_HOME/server/classes class file.
8. $ CATALINA_HOME/server/lib.
9. $ CATALINA_BASE/shared/classes class file.
10. $ CATALINA_BASE/shared/lib.
11. Their specific webapp/WEB-INF/classes under the class file.
12. Their specific webapp/WEB-INF/lib jar files.
The search sequence of the class 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, class files placed in different webapps are loaded into different instances by classloader.
For example, assume that the following two classes have different contents. Put them in the class directories 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 com. lizongbo. NAME results obtained in different webapps are different and do not affect each other.
However, note that the following class exceptions start with the package name:
Javax .*
Org. xml. sax .*
Org. w3c. dom .*
Org. apache. xerces .*
Org. apache. xalan .*
Ps, note. In each jar \ META-INF \ MAINFEST. MF File Class-Path key-value pairs, will also provide 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, first load the commons-beanutils.jar, commons-collections.jar first under the directory where the jar is located... And other jar files.
Placing jar and class in different places may have unexpected consequences. The source code is in the sky, especially the jar files of different versions. Therefore, pay special attention to the actual application deployment when deploying web applications.
For example, a common error message of javamail is used:
Javax. mail. NoSuchProviderException: No provider for smtp
The real reasons are as follows:
Different Versions of mail. jar are placed in different directories for jar loading, such as a mail. jar file of javamail1.3.1.
In D: \ jakarta-tomcat-5.5.8 \ common \ lib, and the other is javamail1.3.2 mail. jar in
D: \ jakarta-tomcat-5.5.8 \ webapps \ lizongbo \ WEB-INF/lib,
When javamail is used in the lizongbo webapp to send emails, the No provider for smtp error will occur.
--

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.