[C3p0] Article 1: a simple introduction to c3p0,

Source: Internet
Author: User

[C3p0] Article 1: a simple introduction to c3p0,
Preface

According to the c3p0-0.9.5.1 version of the official documents, coupled with their own understanding, compiled into the translation. Limited capabilities. If the translation content is incorrect or your understanding is biased, please correct it!


What is c3p0?

C3p0 is a very useful jar class library. According to the jdbc3 specification and the jdbc2 specification, it extends part of the definition implementation, making traditional JDBC more suitable for enterprise-level development. In version 0.9.5, c3p0 fully supports the jdbc4 specification.

C3p0 provides several useful services:

1. Obtain the database connection method, which is obtained by the traditional JDBC Driver Based on DriverManager and converted to the new javax. SQL. DataSource mode.
2. The connection pool and PreparedStatement are hidden behind the DataSource. DataSource can be "encapsulated" with a traditional driver or any DataSource without a connection pool.

The library tries to get the correct details:

1. The c3p0 data source is both Referenceable (recommendable) and Serializable (Serializable). Therefore, it is suitable for binding a JNDI-based naming service.

2. When the Connection and Statement retrieved from the Connection pool are put back into the Connection pool, the Statement and ResultSet will be carefully cleared, prevent resource depletion caused by laziness using resource management policies that only clean up connections.

3. The Library is defined by JDBC2 and 3 (this conflicts with the author's preferences ). DataSource is written in the JavaBean specification and provides all the required attributes and many optional attributes (including some non-standard attributes) without a miserable constructor. All internal interfaces defined in JDBC specifications are implemented (ConnectionPoolDataSource, PooledConnection, ConnectionEvent-generating Connections, etc ), you can use both c3p0 and third-party components (but not all c3p0 features are well compatible with external ConnectionPoolDataSource ).

C3p0 hopes to provide DataSource implementation that is more suitable for large-scale "JavaEE enterprise-level applications. If a bug is found or fixed, contact the official website!


Getting started

The initial design purpose of c3p0 is to use simple, just put the jar file lib/c3p0-0.9.5.1.jar in the effective classpath of the application, and then create DataSource:

import com.mchange.v2.c3p0.*;...ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" ); cpds.setUser("dbuser"); cpds.setPassword("dbpassword"); 
[Optional] if you want to use the PreparedStatement pool, you must set the maxStatements and maxStatementsPerConnection parameters (the default value is 0 ):
cpds.setMaxStatements( 180 );
Whatever you want to do with DataSource, it will be supported by a connection pool initialized with default parameters. You can bind the data source to a JNDI service or directly use it based on your preferences.
After completing the task, you can clear the created Data source:
cpds.close();

The basic usage is like this! The rest are details.


Prerequisites

The c3p0-0.9.5.1 requires Java Runtime Environment 1.6.x or later.


Jar Import

Place the file lib/c3p0-0.9.5.1.jar and lib/mchange-commons-java-0.2.10.jar under the program CLASSPATH (or somewhere other classloaders can find ). That's it!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.