Java Web (ii) Tomcat data source

Source: Internet
Author: User

First, the generation of data sources

1. JDBC Operation principle

(1) Load the database driver (the database driver can be configured via CLASSPATH);

Class.forName ();

(2) Obtain the database connection object through the DriverManager class;

Connection connn = Drivermanager.getconnection ();

(3) Using connection to instantiate PreparedStatement object, write SQL command operation database;

PreparedStatement PS = conn.preparestatement (SQL);

Ps.executequery ();

(4) The database is a resource operation, the database is closed and resources are freed after the operation is completed.

Con.close ();

2, because each user to perform the database operation to go through the same (1) (2) (4) steps, but each user for the database operation is different, so in the database operation if you can remove the repeated 3 steps to keep only step 3, the performance will certainly be improved.

Ii. Introduction to Data sources

1, the core principle of the data source: In one object pool to save multiple database connections (also become the database connection pool--connection), the user to operate the database to take out one, run out and put back the connection waiting for other users to continue to use;

2, consider the factors:

1> minimum number of connections: If a program is in use without a user connection, the minimum number of database connections that should be maintained by the database;

2> Maximum number of connections: the number of database connections a database can open in a single program;

3> Maximum wait time: When a database connection pool has no more database connections available to the user, the maximum wait time for other users can continue to be used if there is time to wait, otherwise the user cannot get the database connection.

Thus we can implement the Java application, first in a class set to save multiple database connection objects, and then through the control class set to achieve the connection pool function implementation, but this will have to consider multithreading problems, the above three kinds of problems to be considered, the implementation is more difficult; This has been supported since the tomcat4.1.x version, so database connection pooling can be implemented directly through Tomcat in web development, as well as through database connection pool components such as the Apache organization's C3P0 component.

3, the connection pool of the database in the Web container is accessed through the data source (Javax.sql.Datasource), That is, you can get a connection object through the Javax.sql.Datasource class, and the desired DataSource object needs to be found using JNDI (Jndi--java naming and directory interface, one of Java EE13 Technologies, The main function is to find the corresponding value by a key of a name)

Third, the realization of the data source

Note that the Mysql-connector-java-5.1.6-bin.jar package is stored in the Lib directory specified by Docbase or in Lib under the Tomcat installation file

1. Configure database connection pool, Server.xml file

<context path= "/kk" docbase= "D:\Local\webKK" reloadable= "true" >
<resource
Name= "JDBC/XRK", Configuring a Connection pool resource named JDBC/XRK
Auth= "Container"-Container responsible for resource connection
Type= "Javax.sql.DataSource" This data source name corresponds to a type of DataSource
maxactive= Maximum number of connections
maxidle= minimum number of connections
Maxwait= "10000"-the maximum time the user waits
Username= "root" database user name
password= "KK" Database password
Driverclassname= "Org.gjt.mm.mysql.Driver" database driver
Url= "jdbc:mysql://localhost:3306/test" database name
/>
</Context>

The above configuration is a global data source configuration, and the global data source means that any web app can access it after a data source has been configured.

The AUTH option in the <Resource> node represents a way to connect to a database:

The container--container will log on on behalf of the application to the Resource Manager (common);

The application--application must programmatically log on to the resource manager.

2. Configure the Web. xml file

Note that the name of the data source to use is indicated in the file;

<display-name>welcome to Tomcat</display-name>
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/xrk</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

3. Find the data source

The operation of the data source is to be looked up using a jndi approach, as follows:

1> Initialize Name Lookup context: Context ctx=new initialcontext ();

2> find DataSource objects by name: DataSource ds= (DataSource) ctx.lookup (dsname);

3> get a database connection via datasource: Connection conn=ds.getconnection ();

<%@ page contenttype= "text/html" pageencoding= "Utf-8"%>
<%@ page import= "javax.naming.*"%>
<%@ page import= "javax.sql.*"%>
<%@ page import= "java.sql.*"%>
<body>
<%
String dsname= "JAVA:COMP/ENV/JDBC/XRK"; Jndi Name
Context ctx=new InitialContext ();
DataSource ds= (DataSource) ctx.lookup (dsname);
Connection conn=ds.getconnection ();
%>

<%=conn%>

<%
Conn.close ();
%>

</body>

The java:comp/env/prefix is preceded by the initialization of the Jndi name in the above file, primarily to resolve the conflict in Jndi lookups (Java EE-defined environment naming contexts--environment naming Context (ENC))

Run the program and get the connection output as follows, otherwise the output is null.

Iv. Benefits of using database connection pooling

1, through the database connection pool can improve the operation performance of the database, can avoid class loading, database connection, database shutdown and other operations;

2, the data source operation to use Jndi lookup, and look for the need to develop a prefix attribute.

Java Web (ii) Tomcat data source

Related Article

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.