Tomcat uses JDNI configuration information and usage information. For JDBC Connection pooling

Source: Internet
Author: User
Tags connection pooling

Jndi:

JNDI (Java naming and directory Interface), Java naming and directory interfaces. The role of Jndi is to configure resources on the server and then to obtain the configured resources in a unified manner

To configure resources in Tomcat:
Configured in the context. Generally we place jdni configured resources in the Conf-->catalina-->localhost folder under Tomcat
The configuration file name is: project name. xml

Example:
<Context>
<!--
Name: Specify the names of the resources
Factory: Who is responsible for creating the resource. Factory is basically fixed.
Type: Types of resources
Everything else is a resource parameter.
-
<resource name= "Jdbc/datasource"
factory= "Org.apache.naming.factory.BeanFactory"
Type= "Com.mchange.v2.c3p0.ComboPooledDataSource"
Jdbcurl= "Jdbc:mysql://127.0.0.1:3306/demo"
Driverclass= "Com.mysql.jdbc.Driver"
User= "Guodaxia"
Password= "961012gz"
Acquireincrement= "3"
Initialpoolsize= "10"
/>
</Context>

2. Access to resources
The purpose of configuring resources is, of course, to get resources. As soon as you start Tomcat, you can get resources through Jndi in any class in your project
To get the resources.
Example:
Context initctx=new InitialContext ();
Context envctx= (context) Initctx.lookup ("java:comp/env"); Java:comp/env is also basically fixed.
DataSource datasource= (DataSource) envctx.lookup ("Jdbc/datasource");
Connection con=datasource.getconnection ();

Get resources:
Context:javax.naming.Context;
InitialContext:javax.naming.InitialContext;
Lookup (String) Gets the method of the resource, where "java:comp/env" is the resource's entry, which is a fixed name, and "Jdbc.datasource" corresponds to Resource
The name value that is configured in this callback is the resource object.

Example:

<Context> <!--Name : Specify the names of the resources factory: The resource is responsible for creating type: The type of the resource other things are the parameters of the resource--<resource name= "Jdbc/datasource"Factory= "Org.apache.naming.factory.BeanFactory"type= "Com.mchange.v2.c3p0.ComboPooledDataSource"Jdbcurl= "Jdbc:mysql://127.0.0.1:3306/demo"Driverclass= "Com.mysql.jdbc.Driver"User= "Guodaxia"Password= "961012gz"acquireincrement= "3"initialpoolsize= "Ten"/></context> PackageCn.itcast.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjava.sql.Connection;Importjava.sql.SQLException;ImportJavax.naming.Context;ImportJavax.naming.InitialContext;Importjavax.naming.NamingException;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJavax.sql.DataSource; Public classAservletextendsHttpServlet {/**     *      */    Private Static Final LongSerialversionuid = 1L;  Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {Try {            /** 1. Create a Jndi context object*/Context Context=NewInitialContext ();//            //2. Inquiry into the entrance//context= (context) Context.lookup ("java:comp/env");//            //3. Query to create the specified bean//DataSource datasource= (DataSource) context.lookup ("Jdbc/datasource"); //the second and third steps can be combined into one stepDataSource datasource= (DataSource) context.lookup ("Java:comp/env/jdbc/datasource"); //4. Get Connection ObjectConnection conn=datasource.getconnection ();        SYSTEM.OUT.PRINTLN (conn); } Catch(namingexception e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); }    }     Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html"); PrintWriter out=Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("); Out.println ("<BODY>"); Out.print ("This is"); Out.print ( This. GetClass ()); Out.println (", using the POST method"); Out.println ("</BODY>"); Out.println ("</HTML>");        Out.flush ();    Out.close (); }}

The jar used:

  

Tomcat uses JDNI configuration information and usage information. For JDBC Connection pooling

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.