org.apache.commons.dbcp.datasources

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » Connection Pool DBCP » org.apache.commons.dbcp.datasources 
org.apache.commons.dbcp.datasources

This package contains two DataSources: PerUserPoolDataSource and SharedPoolDataSource which provide a database connection pool. Below are a couple of usage examples. One shows deployment into a JNDI system. The other is a simple example initializing the pool using standard java code.

JNDI

Most J2EE containers will provide some way of deploying resources into JNDI. The method will vary among containers, but once the resource is available via JNDI, the application can access the resource in a container independent manner. The following example shows deployment into tomcat (catalina).

In server.xml, the following would be added to the <Context> for your webapp:


 <Resource name="jdbc/bookstore" auth="Container"
            type="org.apache.commons.dbcp.datasources.PerUserPoolPoolDataSource"/>
  <ResourceParams name="jdbc/bookstore">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory</value>
    </parameter>
    <parameter>
      <name>dataSourceName</name><value>java:comp/env/jdbc/bookstoreCPDS</value>
    </parameter>
    <parameter>
      <name>defaultMaxActive</name><value>30</value>
    </parameter>

  </ResourceParams>

In web.xml. Note that elements must be given in the order of the dtd described in the servlet specification:

<resource-ref>
  <description>
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the server.xml file.
  </description>
  <res-ref-name>
    jdbc/bookstore
  </res-ref-name>
  <res-type>
    org.apache.commons.dbcp.datasources.PerUserPoolDataSource
  </res-type>
  <res-auth>
    Container
  </res-auth>
</resource-ref>

Catalina deploys all objects configured similarly to above within the java:comp/env namespace. So the JNDI path given for the dataSourceName parameter is valid for a ConnectionPoolDataSource that is deployed as given in the cpdsadapter example

The DataSource is now available to the application as shown below:


    Context ctx = new InitialContext();                
    DataSource ds = (DataSource)
        ctx.lookup("java:comp/env/jdbc/bookstore");
    Connection con = null;
    try
    {
        con = ds.getConnection();
        ... 
        use the connection
        ...
    }
    finally
    {
        if (con != null)
            con.close();
    }

The reference to the DataSource could be maintained, for multiple getConnection() requests. Or the DataSource can be looked up in different parts of the application code. PerUserPoolDataSourceFactory and SharedPoolDataSourceFactory will maintain the state of the pool between different lookups. This behavior may be different in other implementations.

Without JNDI

Connection pooling is useful in applications regardless of whether they run in a J2EE environment and a DataSource can be used within a simpler environment. The example below shows SharedPoolDataSource using DriverAdapterCPDS as the backend source, though any CPDS is applicable.


public class Pool
{
    private static DataSource ds;

    static
    {
        DriverAdapterCPDS cpds = new DriverAdapterCPDS();
        cpds.setDriver("org.gjt.mm.mysql.Driver");
        cpds.setUrl("jdbc:mysql://localhost:3306/bookstore");
        cpds.setUser("foo");
        cpds.setPassword(null);

        SharedPoolDataSource tds = new SharedPoolDataSource();
        tds.setConnectionPoolDataSource(cpds);
        tds.setMaxActive(10);
        tds.setMaxWait(50);

        ds = tds;
    }

    public static getConnection()
    {
        return ds.getConnection();
    }  
}

This class can then be used wherever a connection is needed:

    Connection con = null;
    try
    {
        con = Pool.getConnection();
        ... 
        use the connection
        ...
    }
    finally
    {
        if (con != null)
            con.close();
    }
Java Source File NameTypeComment
CPDSConnectionFactory.javaClass A PoolableObjectFactory that creates PoolableConnection s.
author:
   John D.
InstanceKeyDataSource.javaClass

The base class for SharedPoolDataSource and PerUserPoolDataSource.

InstanceKeyObjectFactory.javaClass
KeyedCPDSConnectionFactory.javaClass A {*link PoolableObjectFactory} that creates {*link PoolableConnection}s.
author:
   John D.
LRUMap.javaClass

This class has been copied from Commons Collections, version 2.1 in order to eliminate the dependency of dbcp on collections.

PerUserPoolDataSource.javaClass

A pooling DataSource appropriate for deployment within J2EE environment.

PerUserPoolDataSourceFactory.javaClass
PooledConnectionAndInfo.javaClass
PoolKey.javaClass
SequencedHashMap.javaClass

This class has been copied from Commons Collections, version 2.1 in order to eliminate the dependency of dbcp on collections.

SharedPoolDataSource.javaClass A pooling DataSource appropriate for deployment within J2EE environment.
SharedPoolDataSourceFactory.javaClass
TestFactory.javaClass
TestPerUserPoolDataSource.javaClass
TestSharedPoolDataSource.javaClass
UserPassKey.javaClass Holds a username, password pair.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.