Tomcat Database Connection Pool configuration

Source: Internet
Author: User
Tags auth

Tomcat Database Connection Pool configuration

1. Configuration of the Server.xml

(1) Locate the Conf\server.xml file in the directory where Tomcat is located

(2) Add the following code to the last

<context path= "/website" docbase= "website" debug= "0" reloadable= "true" >

<resource <!--/website for development catalog, customizable---

Name= "Jdbc/connectdb" <!-connection name, Connectdb customizable

Auth= "Container"

Type= "Javax.sql.DataSource"

Maxactive= "20"

Maxidle= "5"

Maxwait= "10000"

Username= "sa" <!--SqlServer2000 login name, which should be the same as your database settings--

Password= "123456" <!-sqlserver2000 password, custom-to-

factory= "Org.apache.commons.dbcp.BasicDataSourceFactory"

Driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"

Url= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=db_web"/>

<!-here to pay special attention, databasename to establish a good database name, the first to ensure that the database exists, and secondly, there can be no space between the equals sign and the database name, unless your database name itself contains spaces, such as written databasenae= website , then your database name is "_website" instead of "website", underline the location of the space, if the database name is wrong, debugging will be reported: Org.apache.commons.dbcp.SQLNestedException: Cannot create poolableconnectionfactory ([Microsoft][sqlserver Driver for Jdbc][sqlserver] cannot open the requested database in login ' Db_web '. Login failed. )--

</Context>

2. Configuration of Web. xml

(1) Locate the Web-inf\web.xml file under the development directory

(2) Add the following code between the <webapp></webapp>

<resource-ref>

<description>sql Server Text app</description>

<res-ref-name>jdbc/connectDB</res-ref-name> <!-This name must be the same as the connection name you defined earlier! -

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

3. Internal reference of the program:

Inside the program, set up the database connection pool by the following code:

Context CTX = new InitialContext ();

DataSource ds = (DataSource) ctx.lookup ("Java:comp/env/jdbc/connectdb");

Connection con = ds.getconnection ();

Then we can use the database.

Tomcat Database Connection Pool configuration

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.