Tomcat Data Source Summary

Source: Internet
Author: User

First, the origin of the data source

The JDBC operation consists mainly of the following steps:

(1) Class.forName ();

(2) Connection con = drivermanager.getconnection ();

(3) PreparedStatement stat = con.preparestatement (SQL);

(4) Stat.executequery ();

(5) Con.close ();

Obviously, (1) (2) (5) Steps are performed each time the JDBC operation is executed, and repeated execution is very time consuming, so in order to solve the problem of repetitive operation, the data source is introduced.

Ii. introduction of data sources

There is a database connection pool, multiple database connections exist in the pool, and the user takes one out of the database operation and puts it back into the connection pool.

Factors to consider:

(1) Minimum number of connections: the minimum number of connections in the connection pool;

(2) Maximum connection: the maximum number of connections in the connection pool;

(3) Maximum wait time: The longest waiting time cannot be connected;

Tomcat support for database connection pooling;

Third, JNDI (Java naming Directory Interface)

1. Find the corresponding DataSource by the name of DataSource;

2. Obtain a connection in the connection pool through a datasource;

Iv. Configuration Step A Global Data Source Configuration

A global data source means that any web app can be accessed after a data source has been configured.

(1) Copy the driver of the database into the tomcat\lib;

(2) Add in Server.xml:

<context path= "/test" docbase= "E:\JAVA WEB" reloadable= "true" > <!--data source needs to be configured in Context--
<resource name= "Jdbc/xiazdong" <!--data Source name--
Auth= "Container"
Type= "Javax.sql.DataSource"
maxactive= <!--maximum number of connections--
Maxidle= "<!--min. connections--
Maxwait= "10000" <!--maximum wait time--
Username= "Root"
Password= "123456"
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:3306/xiazdong"/>
</Context>

(3) in Web. XML, add:

<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/xiazdong</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

B Local Data Source configuration

(1) Copy the drive to the Tomcat/lib;

(2) Add in Conf/catalina/localhost/test.xml:

[HTML]View Plaincopy
  1. <Resource name="jdbc/test" auth="Container "
  2. type="Javax.sql.DataSource"
  3. driverclassname="Com.mysql.jdbc.Driver"
  4. url="Jdbc:mysql://localhost:3306/javaee"
  5. username="root" password="12345" maxactive="5"
  6. maxidle="2" maxwait="10000"/>

As a sub-element of <Context>;

Five, the main operating procedures import Javax.naming.*;import Javax.sql.*;import java.sql*;

Context CTX = new InitialContext ();

DataSource ds = Ctx.lookup ("Java:comp/env/jdbc/xiazdong");

Connection con = ds.getconnection ();

It can be clearly seen from the above code completely see the operation of which database, configuration is in the configuration file;

Tomcat Data Source Summary

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.