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/>