Profit database connection pool under Tomcat

xiaoxiao2021-03-06  117

Some things are used to learn, and sometimes I have forgotten it for a long time after I have been going to school. When I go to school, my teacher has a saying. It is better to make a pen, it seems to be a pen, even if it is very simple. Things, the time has been blurred, so I still write her down.

For the version below Tomcat5.0, the configuration database connection pool is about a few steps:

First, add the database configuration code in the Tomcat / conf / server.xml file, because Tomcat is managed through the server.xml file, in the server.xml file, The contained pair of markers is the configuration of an application that is published. We can add this tag to make Tomcat to publish different applications. The most effective application is: You can put the app in any directory without copying to Tomcat / WebApps / down. Just add a in the server.xml file, then configure it to publish the application in the different directories of the server. This is just a brief introduction. Let's take a look at the configuration of the connection pool.

After opening the server.xml file, find the last tag, and then add the following paragraph, in the server.xml file is actually a good example, directly copy a section down to modify it can:

username spres password spres driverclassname Oracle.jdbc.driver.OracleDriver URL JDBC: Oracle: Thin: @ 192.168.0.3: 1521: Oracle

For these parameters, everyone should know what it means, but I want to say path = "/ demo" and docBase = "f: / project / igsnrr_proj / authorization_demo / webcontent", PATH: Remember the name of the original root of the application Anti-"" / "slash

DOCBASE: Refers to the directory where the application is released. If the published application directory is in Tomcat / WebApps /, then it is directly written to the name of the application, such as "WebContent"

The format of the Name value in this tag is the "JDBC / Custom Name" This custom name is best to make sense, when the write program is established This name will also be used.

Second, write a JSP test connection

After the configuration is configured above, you can write a JSP page test, I give the Java code that is connected:

public static synchronized Connection getConnection () {DataSource datasource; Connection connection; try {InitialContext initialcontext = new InitialContext (); Context context = (Context) initialcontext.lookup ( "java: comp / env"); datasource = (DataSource) context. Lookup ("JDBC / OSWF"); connection = DataSource.getConnection ();} catch (exception e) {system.out.println ("ConnectionPool - Initialization Database Exception!" E.getMessage ())} Return Connection; }

For the version after Tomcat5.0, the configuration connection pool is also the same, but 5.0 is better later, you can separate the configuration files of different applications, which we are placed in a unified system configuration file, after 5.0 You can write an XML file separately.

The directory structure is as follows:

Tomcat5.0 / conf / Catalina / Localhost /

In this directory, create a new XML file, such as ConnectTest.xml, and then copy the piece of the code above the connection pool to this file, 5.0 is the separation of the application configuration and system file configuration.

Ok, about the configuration of the database connection pool in Tomcat is over, if I find that I have a wrong place, please let me know. Thank you.

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

New Post(0)