Connection Pool Basics : Connection Pool « Apache Common « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Apache Common » Connection PoolScreenshots 
Connection Pool Basics

import java.sql.Connection;
import java.util.Properties;
import java.sql.PreparedStatement;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.dbcp.DriverConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;

public class ConnectionPoolBasics {

  public static void main(String args[]) throws Exception {

    GenericObjectPool gPool = new GenericObjectPool();

    /*Class.forName("com.mysql.jdbc.Driver");

    DriverManagerConnectionFactory cf =
      new DriverManagerConnectionFactory(
        "jdbc:mysql://localhost/commons", "root", "");*/

    Properties props = new Properties();
    props.setProperty("Username""root");
    props.setProperty("Password""");
    ConnectionFactory cf =
      new DriverConnectionFactory(new com.mysql.jdbc.Driver(),
                                  "jdbc:mysql://localhost/commons",
                    props);


    KeyedObjectPoolFactory kopf =new GenericKeyedObjectPoolFactory(null, 8);

    PoolableConnectionFactory pcf =  new PoolableConnectionFactory(cf,
                                    gPool,
                                    kopf,
                                    null,
                                    false,
                                    true);


    for(int i = 0; i < 5; i++) {
      gPool.addObject();
    }

    // PoolingDataSource pds = new PoolingDataSource(gPool);
    PoolingDriver pd = new PoolingDriver();
    pd.registerPool("example", gPool);

    for(int i = 0; i < 5; i++) {
      gPool.addObject();
    }

    Connection conn = java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:example");

    System.err.println("Connection: " + conn )//": Delegate: " + ((org.apache.commons.dbcp.PoolingConnection)conn).getDelegate());

    // do some work with the connection
    PreparedStatement ps = conn.prepareStatement("Select * from customer where id = ?");

    System.err.println("Active: " + gPool.getNumActive() ", Idle: " + gPool.getNumIdle());

    conn.close();

    System.err.println("Active: " + gPool.getNumActive() ", Idle: " + gPool.getNumIdle());

  }
}

           
       
ConnectionPoolBasics.zip( 1,214 k)
Related examples in the same category
1.Basic DataSource Example
2.Database connection pool demo
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.