Database connection pool--tomcat built-in implementation

Source: Internet
Author: User

1.JNDI (Java naming and directory Interface,java naming and directory interface)

is a set of APIs that access the naming and directory services in a Java application. The naming service links names to objects so that we can access them by name. A directory service is a naming service in which objects are not only named but also attributes. It can be understood as a container in which any content can be stored, a string name is given to the content in the container, the name is named in the directory structure, and the name is referenced elsewhere to get the content.

2. Implement

Idea: Store the database connection pool as a server-side resource in a Jndi container, and remove the resource from the container when used, that is, the database connection pool.

A) The object exists in the Jndi container

Create a context.xml file in the webroot\meta-inf\ directory of the Web project, store the object as a resource in a container, or be accessed by another Web project.

      

<?XML version= "1.0" encoding= "UTF-8"?><Context>    <!--#1 to Tomcat configuration objects, adding content to the Jndi container -  <Resourcename= "Jdbc/pool"Auth= "Container"type= "Javax.sql.DataSource"maxactive= "+"Maxidle= "+"maxwait= "10000"username= "root"Password= "1234"Driverclassname= "Com.mysql.jdbc.Driver"URL= "Jdbc:mysql://localhost:3306/day17"/></Context>

b) The current Web project will require objects to be referenced from the Tomcat Jndi container.

Add the following code to the Web. xml file under the current project \webroot\web-inf path

<!--#2 referencing an already existing object from the Jndi container -    <Resource-ref>      <Res-ref-name>Jdbc/pool</Res-ref-name>      <Res-type>Javax.sql.DataSource</Res-type>      <Res-auth>Container</Res-auth>  </Resource-ref>

c) JSP or Java code use

<%@page Import="java.sql.Connection"%><%@page Import="Javax.sql.DataSource"%><%@page Import="Javax.naming.InitialContext"%><%@page Import="Javax.naming.Context"%><%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib URI="Http://java.sun.com/jsp/jstl/sql"prefix="SQL" %><%@ taglib URI="Http://java.sun.com/jsp/jstl/core"prefix="C" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title>    <%--Querying Data--%>    <Sql:queryvar= "RS"DataSource= "Jdbc/pool">SELECT * from T_user</Sql:query>    </Head><Body>    <C:foreachItems= "${rs.rows}"var= "Row">${row.id} ${row.username} ${row.password}</C:foreach>      <%        //1 initializing Jndi container, package: javax.naming Context initcontext= NewInitialContext (); //2 Initial Java environment context Envcontext=(Context) initcontext.lookup ("java:/comp/env"); //3 Getting custom content DataSource ds=(DataSource) envcontext.lookup ("Jdbc/pool"); Connection Conn=ds.getconnection ();    Out.print (conn); %></Body></HTML>

Database connection pool--tomcat built-in implementation

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.