Java Web----Tomcat Configuration connection Pool

Source: Internet
Author: User

1 Tomcat Configuration Jndi Resource

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

The resources we are configuring here are, of course, connection pooling, so that the connection pool object can be obtained in a unified manner in the project.

Configuring the Jndi resource requires the configuration <Resource> child elements in the <Context> element:

    • Name: Specify the names of the resources, this name can be given casually, in the acquisition of resources required by this name;
    • Factory: The factory used to create the resource, which is basically fixed, without modification;
    • Type: Types of resources, the type we want to give is of course the type of our connection pool;
    • Bar: Represents the property of a resource, and the value of bar is configured if the resource has a property named bar. For the DBCP connection pool, you need to configure not bar, because it does not have bar this property, but should go to configure the URL, username and other properties.

Create a new "project name. xml" file in \apache-tomcat-8.0.15\conf\catalina\localhost\.

<Context>    <resource name= "mydbcp" type= "Org.apache.tomcat.dbcp.dbcp.BasicDataSource" factory= " Org.apache.naming.factory.BeanFactory "username=" root "password=" 123 "Driverclassname=" Com.mysql.jdbc.Driver "    url= "JDBC:MYSQL://127.0.0.1/MYDB1" maxidle= "3" maxwait= "maxactive=" "5" initialsize= "3"/></context >  <Context>    <resource name= "myc3p0" type= "Com.mchange.v2.c3p0.ComboPooledDataSource" factory= "Org.apache.naming.factory.BeanFactory" user= "root" password= "123" classdriver= "Com.mysql.jdbc.Driver    " Jdbcurl= "JDBC:MYSQL://127.0.0.1/MYDB1" maxpoolsize= "minpoolsize =" 5 "initialpoolsize=" acquireIncrement= "2"/ ></Context>  

2 Getting Resources

The purpose of configuring resources is, of course, to get resources. As soon as you launch Tomcat, you can get resources in any class in your project by using Jndi to get resources.

Get resources:

    • Context:javax.naming.Context;
    • InitialContext:javax.naming.InitialContext;
    • Lookup (String): The method of getting a resource, where "java:comp/env" is the source of the resource (which is a fixed name), gets a context, which means that it needs to be further fetched in the context of the acquisition. "Bean/mybeanfactory" corresponds to the name value configured in <Resource>, which gets the resource object.

        Context cxt = new InitialContext ();         DataSource ds = (DataSource) cxt.lookup ("java:/comp/env/mydbcp");        Connection con = ds.getconnection ();        System.out.println (con);        Con.close ();
        Context cxt = new InitialContext ();             Context ENVCXT = (context) cxt.lookup ("java:/comp/env");        DataSource ds = (DataSource) env.lookup ("mydbcp");        Connection con = ds.getconnection ();        System.out.println (con);        Con.close ();





Java Web----Tomcat Configuration connection Pool

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.