Use of c3p0

Source: Internet
Author: User

1. CreateCom. mchange. v2.c3p0. combopooleddatasource

This is a JavaBean. Before use, set its jdbcurl, user, password, and driverclass. For other parameters, see configuration properties.

Combopooleddatasource CPPS = new combopooleddatasource (); CPPS. setdriverclass ("org. postgreSQL. driver "); <br/> CPDs. setjdbcurl ("JDBC: PostgreSQL: // localhost/testdb"); CPPS. setuser ("swaldman"); <br/> CPPS. setpassword ("test-Password"); <br/> // The following settings are optional. c3p0 can be used by default, you can set other conditions. <br/>. setminpoolsize (5); <br/> CPPS. setacquireincrement (5); <br/> CPPS. setmaxpoolsize (20 );

You can also use the name Configuration

Combopooleddatasource CPPS = new combopooleddatasource ("intergalactoapp ");

2. Use FactoryCom. mchange. v2.c3p0. CES

Com. mchange. v2.c3p0. CESYou can create a datasource without a connection pool based on the traditional JDBC driver and convert it to the datasource of the connection pool.

Datasource ds_unpooled = datasources. unpooleddatasource ("JDBC: PostgreSQL: // localhost/testdb", "swaldman", "test-Password"); <br/> datasource ds_pooled = CES. pooleddatasource (ds_unpooled); <br/> // datasource can be used now, but the set driver class should be displayed <br/> class. forname ("org. postgreSQL. driver "); <br/>

If you want to set the parameters, you can put the parameters in a map.

Datasource ds_unpooled = datasources. unpooleddatasource ("JDBC: PostgreSQL: // localhost/testdb", "swaldman", "test-Password"); <br/> map overrides = new hashmap (); <br/> overrides. put ("maxstatements", "200"); overrides. put ("maxpoolsize", new INTEGER (50); <br/> // create a pooleddatasource that includes the default value and set value <br/> ds_pooled = datasources. pooleddatasource (ds_unpooled, overrides); <br/>

If you use the named configuration, you can:

Ds_pooled = datasources. pooleddatasource (ds_unpooled, "intergalactoappconfig", overrides );

 

There are two ways to destroy datasource:

Datasource. Destroy () method

Datasource ds_pooled = NULL; <br/> try {<br/> datasource ds_unpooled = CES. unpooleddatasource ("JDBC: PostgreSQL: // localhost/testdb", "swaldman", "test-Password"); <br/> ds_pooled = CES. pooleddatasource (ds_unpooled); <br/> // datasource is used normally below... <br/>}< br/> finally {<br/> datasources. destroy (ds_pooled); <br/>}< br/>

The other isPooleddatasourceInterfaceClose ()Method

Static void cleanup (datasource DS) throws sqlexception {<br/> // determines whether it is a pooleddatasource of c3p0 <br/> If (DS instanceof pooleddatasource) {<br/> pooleddatasource PDS = (pooleddatasource) ds; <br/> PPS. close (); <br/>}else <br/> system. err. println ("not a c3p0 pooleddatasource! "); <Br/>}< br/>

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.