Java connection pool technology and Java connection pool technology

Source: Internet
Author: User

Java connection pool technology and Java connection pool technology

How to Use JNDI (Java Naming and Directory Interface) is an application Interface that provides developers with a universal and unified Interface for searching and accessing various Naming and Directory services, JDBC is built on the abstraction layer. JNDI provides a unified method for searching and accessing services on the network. By specifying a resource name, the name corresponds to one record in the database or naming service, and returns the information required for database connection establishment.

In actual development, we sometimes use the database connection pool provided by the server. For example, we hope that the Tomcat server can help us create a database connection pool at startup, in this case, you do not need to manually create a database connection pool in the application. You can directly use the database connection pool created by the Tomcat server. To create a database connection pool when the Tomcat server is restarted, You need to configure the Tomcat server.

The configuration process is as follows:

(1) Place the database Driver (jar package) in the lib folder under the Tomcat installation directory.

(2) manually create a context. xml file under the WebContent \ META-INF \ directory of the project, edit the content, the final content is as follows:

 

<? Xml version = "1.0" encoding = "UTF-8"?>
<Context>
<Resource name = "jdbc/test" auth = "Container" type = "javax. SQL. dataSource "maxActive =" 100 "maxIdle =" 30 "maxWait =" 10000 "username =" sa "password =" 525825 GAOzhe"
DriverClassName = "com. microsoft. sqlserver. jdbc. SQLServerDriver"
Url = "jdbc: sqlserver: // localhost: 1433; DatabaseName = test"/>
</Context>

(3) then manually create a web. xml file under the WebContent \ META-INF \ directory above, edit the content, the final content is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xsi: schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id = "WebApp_ID" version = "3.1">
<Resource-ref>
<Description> DB Connection </description>
<Res-ref-name> jdbc/test </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
</Web-app>

(3) create a jsp page to test database access. The Code is as follows:

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<% @ Page import = "java. SQL. *, javax. SQL. *, javax. naming. *, com. microsoft. *" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> database connection test </title>
</Head>
<Body>
<%
Connection con = null;
Try {
Context ctx = new InitialContext ();
DataSource ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/test ");
Con = ds. getConnection ();


System. out. println ("connection pool connectioned !! ");
Con. close ();
} Catch (NamingException e ){
System. out. println (e. getMessage ());
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Con. close ();
}


%>
</Body>
</Html>

So far, the database connection pool has been created.

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.