Jndi Configuration c3p0 Connection pool

Source: Internet
Author: User

What is Cndi?

Is the Java naming and directory interface, which is a standard Java naming system interface provided by Sun Corporation.

Not good to understand? Simply put, he is a resource, a resource placed in Tomcat, today we put the database connection pool into Tomcat, of course, you can also put something else, such as been.

ok! start

First we create a new Web project:

New project called: Jndi_demo
Then, under the Tomcat directory: F:\apache-tomcat-5.5.25\conf\Catalina\localhost, create a resource.

Name is: project name. xml

I'm calling it: Jndi_demo.xml.

<Context>  <resource name= "Datasource/c3p0datasource"             type= " Com.mchange.v2.c3p0.ComboPooledDataSource "            factory=" org.apache.naming.factory.BeanFactory "            Driverclass = "Com.mysql.jdbc.Driver" jdbcurl= "Jdbc:mysql://localhost:3306/test" user= "root" password= "root" acquireincrement= " 3 "initialpoolsize=" 5 "maxpoolsize=" minpoolsize= "2"/></context>
In this simple say this configuration file:

Name= "at random, to be used in the upcoming project."
Type= you create the type of resource, here we are to create a C3P0 connection pool, the corresponding class name is: Com.mchange.v2.c3p0.ComboPooledDataSource
Factory can not be changed.
Here is the connection pool configuration information, note the beginning of the lowercase letter, you change the account password to your own, and then create a database called Test.
All right. Next we create a servlet that gets the connection pool resource to get the database connection.
 We're building a new ser. Vlet:jndi_servlet 
Package Com;import Java.io.ioexception;import Java.io.printwriter;import java.sql.connection;import Javax.naming.context;import Javax.naming.initialcontext;import Javax.naming.namingexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.sql.DataSource; public class Jndi_servlet extends HttpServlet {/** * We get a practice session connection pool resource for the c3p0 configured in Tomcat. */private static final Long serialversionuid = 1l;public void doget (HttpServletRequest request, HttpServletResponse Respo NSE) throws Servletexception, IOException {//Get Tomcat configuration database connection pool try {Context cxt = new InitialContext ();//lookup must be in Java: comp/env/start + The value of the name of the configuration file you just wrote DataSource ds = (DataSource) cxt.lookup ("Java:comp/env/datasource/c3p0datasource"); Connection con = ds.getconnection (); System.out.println ("Con:" +con);} catch (Exception e) {//TODO auto-generated catch Blockthrow new RuntimeException ();}} }


Then we import the jar package (C3p0jar+mysql drive jar): Click to download the jar package

Well, no, I put the web. XML on it, but this one is generated by myself, I didn't change it.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">  <display-name></display-name>  < Servlet>    <description>this is the description of my EE component</description>    < Display-name>this is the display name of my EE component</display-name>    <servlet-name>jndi_ Servlet</servlet-name>    <servlet-class>com. jndi_servlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name> jndi_servlet</servlet-name>    <url-pattern>/servlet/JNDI_Servlet</url-pattern>  </ servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>


Then we start Tomcat and then visit: Http://localhost:8080/JNDI_demo/servlet/JNDI_Servlet

Enter to see the blank, it means success. We look at the information that the console prints:

Con:[email protected]

This shows that we have successfully obtained the C3P0 database connection. This print is to change the object! , if not successful, you can give me a message. Share together and learn together.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Jndi configuration c3p0 connection pooling

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.