Tomcat8 configures the database connection pool and tomcat8 database connection

Source: Internet
Author: User

Tomcat8 configures the database connection pool and tomcat8 database connection

1,All tomcat projects share one connection pool configuration.

1.1 modify the conf-> context. xml file and configure it under the Context node.

<Resource
Name = "jdbc/myDataSource"
Type = "javax. SQL. DataSource"
DriverClassName = "com. microsoft. sqlserver. jdbc. SQLServerDriver"
MaxIdle = "2"
MaxWait = "5000"
Username = "sa"
Password = "xx"
Url = "jdbc: sqlserver: // 127.0.0.1 \ SQLEXPRESS: 1433; DatabaseName = ShopSystem"
MaxActive = "4"/>

Note:

<! -- Name: DataSource name -->
<! -- Type: the java type corresponding to the data source. It is generally designed as javax. SQL. DataSource -->
<! -- Username: Database login name -->
<! -- Password: Database login password -->
<! -- DriverClassName: Specifies the database JDBC driver -->
<! -- Url: Specifies the URL of the database -->
<! -- MaxIdle: Maximum number of database connections in idle state in the connection pool. If this parameter is set to 0, no restriction is imposed. -->
<! -- MaxWait: the maximum time (in milliseconds) for idle database connections in the connection pool. The value 0 indicates unlimited waiting time. -->
<! -- MaxActive: Maximum number of database connections in the active state of the connection pool. Removing 0 indicates no restriction. -->

1.2 modify tomcat's web. xml file

Configure under the web-app node:

<Resource-ref>
<Res-ref-name> jdbc/myDataSource </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>

2. Each Web project is configured independently with its own connection pool

2.1 create the context. xml file in the META-INF directory of the Web project with the following content:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Context path = "/">
<Resource
Name = "jdbc/myDS"
Type = "javax. SQL. DataSource"
DriverClassName = "com. microsoft. sqlserver. jdbc. SQLServerDriver"
MaxIdle = "2"
MaxWait = "5000"
Username = "sa"
Password = "xx"
Url = "jdbc: sqlserver: // 127.0.0.1 \ SQLEXPRESS: 1433; DatabaseName = ShopSystem"
MaxActive = "4"/>
</Context>

2.2 modify the Web. xml file under the WEB-INF directory of the web Project

Configure under the web-app node:

<Resource-ref>
<Res-ref-name> jdbc/myDS </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>

3. No matter which connection pool is used, copy the database driver jar file to the tomcat lib directory.

4. Test Procedures

<%
Context initContext = new InitialContext ();
Context envContext = (Context) initContext. lookup ("java: comp/env ");
DataSource ds = (DataSource) envContext. lookup ("jdbc/myDataSource ");
Connection connection = ds. getConnection ();
Statement statement = connection. createStatement ();
ResultSet resultSet=statement.exe cuteQuery ("select * from admin ");
While (resultSet. next ()){
Out. print (resultSet. getString ("a_name "));
Out. print ("<br/> ");
}
ResultSet. close ();
Statement. close ();
Connection. close ();
%>

 

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.