Tomcat Data Source Summary

Source: Internet
Author: User

first, the origin of the data source

JDBC operations consist primarily 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 executed every time a JDBC operation is performed, and repeated execution is time-consuming, so the data source is introduced to solve the problem of repetitive operations.


Ii. Introduction of data sources

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

Factors to consider:

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

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

(3) Maximum wait time: The longest wait time when cannot connect;

Tomcat support for database connection pooling;


Third, JNDI (Java naming Directory Interface)

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

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

Four, configuration steps A Global Data Source configuration

A global data source means that any Web application 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 sources need to be configured in the context-->
<resource name= "Jdbc/xiazdong" <!--data Source name-->
Auth= "Container"
Type= "Javax.sql.DataSource"
maxactive= <!--maximum number of connections-->
maxidle= <!--minimum connection number-->
Maxwait= "10000" <!--maximum wait time-->
Username= "Root"
Password= "123456"
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:3306/xiazdong"/>
</Context>

(3) Add in Web.xml:

<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 into the tomcat/lib;

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

	<resource name= "Jdbc/test" auth= "Container" type= "Javax.sql.DataSource"
		Com.mysql.jdbc.Driver "
		url=" Jdbc:mysql://localhost:3306/javaee "
		username=" root "password=" 12345 " Maxactive= "5"
		maxidle= "2" maxwait= "10000"/>

As a child element of <Context>;


Five, the main operation steps 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 ();

Can very clearly see, from the above code completely can't see the operation of which database, configuration is in the configuration file;

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.