C3P0 Database Connection Pool

Source: Internet
Author: User
Tags postgresql
c3p0 Database Connection pool manual definition

C3P0 is a mature, high-concurrency JDBC connection pool that is distributed with Hibernate and wants to provide a good data source implementation for enterprise-class Java EE applications. Usage Scenarios

C3P0: Provides classes that obtain database connections that are compatible with the drive-managed JDBC driver and the Java.sql.DataSource view. The connection and PreparedStatement of the data source are transparent, so that the traditional driver and any data source of the pool can get the wrapping processing. Suitable for binding a large number of Jndi-based naming services whose data sources are reference and serializable. Compatible with the methods defined in JDBC2 and JDBC3, all required attributes and most of the optional attributes are opened, providing a parameterless construction method that implements the internal interfaces defined in all JDBC. How to use Required Environment

C3p0-0.9.5-pre8 requires a Java runtime environment of 1.6.x or higher. installation

Put Lib/c3p0-0.9.5-pre8.jar and Lib/mchange-commons-java-0.27.jar two files under your ClassPass (or anywhere your classloader can load). Quick to use

To create a data source:

1
2
3
4
5
6
7
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 need to open the PreparedStatement pool, you must set the maxstatements and/or Maxstatementsperconnection two values (default is 0).

1
Cpds.setmaxstatements (180);

The connection pool at the bottom of the data source is created with default parameters, and you can do anything with the data source. You can bind the data source to the Jndi namespace or use it directly, depending on your preferences.

When you are finished, you can destroy the data source:

1
Cpds.close ();
Advanced Use

The C3P0 provides standard JDBC2 data source objects. Users can control pool-related, naming-related properties, and so on when creating data source objects. All pool management operations are completely transparent to the user after the data source object is created.

There are three ways to create a C3P0 data source: Instantiate directly, configure a combopooleddatasource bean. Use the data source factory class. Create a personal data source with pooled technology: instantiate Poolbackeddatasource directly and set its conectionpooldatasource.

If you do not specify a configuration, C3P0 will create the data source using the default parameters.

Once instantiated successfully, the C3P0 data source can be bound to a naming service that is compatible with the JNDI standard.

C3P0 has a hard-coded configuration built in, but you can overwrite configuration information by creating a c3p0.properties file and placing it in the top-level directory of the Classpath (or classloader) that loads the c3p0 jar file.
For more configuration information, see the next chapter. Instantiate and configure Combopooleddatasource

Instantiating a com.mchange.v2.c3p0.ComboPooledDataSource is the most straightforward way to create a c3p0 pool data source. This is a JavaBean-style class that has a parameterless public constructor method. Make sure you set the Jdbcurl property before you use it. You can also set the user and password properties as needed. If you are using an old JDBC driver that is not preloaded, you should also set the Driverclass property.

1 2 3 4 5 6 7 8 9 
combopooleddatasource CPDs = new Combopooleddatasource (); Cpds.setdriverclass ("Org.postgresql.Driver");
Loads the JDBC driver cpds.setjdbcurl ("Jdbc:postgresql://localhost/testdb");
Cpds.setuser ("Swaldman");
Cpds.setpassword ("Test-password");
The settings below is optional--c3p0 can work with defaults cpds.setminpoolsize (5);
Cpds.setacquireincrement (5);
Cpds.setmaxpoolsize (20); The DataSource CPDs is now a fully configured and usable pooled DataSource ... 

The initial state of any instance of a c3p0 datasource depends on the configuration you provide or the default configuration that you restore to hard-coded.

C3P0 supports named configurations to configure multiple data sources.

You can construct your own com.mchange.v2.c3p0.ComboPooledDataSource named configuration:

1

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.