Connection pooling technology for Java

Source: Internet
Author: User
Tags connection pooling

How to use Jndi (Java naming and directory Interface) is an application interface that provides developers with a common, unified interface to find and access various naming and directory services, similar to JDBC, which is built on the abstraction layer. Jndi provides a unified way to find and access services on the network. By specifying a resource name that corresponds to one record in the database or naming service, and also returns the information necessary to establish the database connection.

In practical development, we sometimes use the database connection pool that the server provides to us, for example, we want the Tomcat server to help us create a database connection pool when it starts, so we don't need to create the database connection pool manually in the application. You can create a database connection pool directly using the Tomcat server. To help us create a database connection pool ' when the Tomcat server restarts, you need to configure the Tomcat server simply.

The configuration process is as follows:

(1) Place the database driver (Jar package) in the Lib folder in the Tomcat installation directory.

(2) Manually create a context.xml file in the webcontent\meta-inf\ directory of the project, edit the contents, 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= "" maxidle= "" maxwait= " 10000 "username=" sa "password=" 525825GAOzhe "
Driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"
Url= "Jdbc:sqlserver://localhost:1433;databasename=test"/>
</Context>

(3) in the above webcontent\meta-inf\ directory manually create a Web. xml file, edit the contents, 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 the 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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Database Connectivity Testing </title>
<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>

So far, the connection pool for the database has been created.

Connection pooling technology for Java

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.