[Author]: Kwu
To create a C3P0 data source, the actual development of the direct use of JDBC database connection is very small, generally using the form of data sources, C3P0 is open source data source, the actual project used a lot:
1. Increase MAVEN Support
<dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version> 0.9.1.2</version></dependency>
2. Configurationc3p0, default configuration file C3p0-config.xml:
<?xml version= "1.0" encoding= "UTF-8"?><c3p0-config> <named-config name= "c3p0" > < Property name= "User" >root</property> <property name= "Jdbcurl" >jdbc:mysql://localhost:3306/ charts</property> <property name= "Driverclass" >com.mysql.jdbc.Driver</property> < Property name= "Password" >root</property> <property name= "Acquireincrement" >5</property> <property name= "initialpoolsize" >5</property> <property name= "Minpoolsize" >5</ property> <property name= "maxpoolsize" >10</property> <property name= "Maxstatements" >25</property> <property name= "maxstatementsperconnection" >5</property> </ Named-config></c3p0-config>
3. Referencing a data source
Package Com.hexun.utils;import Java.sql.connection;import Java.sql.resultset;import java.sql.sqlexception;import Java.sql.statement;import javax.sql.datasource;import com.mchange.v2.c3p0.combopooleddatasource;/** * JDBC Tool class * * This includes methods such as getting a database connection, shutting down database resources, and so on. */public class Jdbctools {//Process database transaction//COMMIT TRANSACTION public static void commit (Connection Connection) {if (Connection! = null) {try { Connection.commit ();} catch (SQLException e) {e.printstacktrace ();}}} ROLLBACK TRANSACTION public static void Rollback (Connection Connection) {if (Connection! = null) {try {connection.rollback ();} catch ( SQLException e) {e.printstacktrace ();}}} Start transaction public static void Begintx (Connection Connection) {if (Connection! = null) {try {Connection.setautocommit (FALSE)} catch (SQLException e) {e.printstacktrace ();}}} private static DataSource DataSource = null;//database connection pool should only be initialized once. Static{datasource = new Combopooleddatasource ("c3p0");} public static Connection getconnection () throws Exception {return datasource.getconnection (); public static void ReleAsedb (ResultSet ResultSet, Statement statement,connection Connection) {if (ResultSet! = null) {try {resultset.close ();} C Atch (SQLException e) {e.printstacktrace ();}} if (statement! = NULL) {try {statement.close ();} catch (SQLException e) {e.printstacktrace ()}} if (connection! = NULL) {The connection object of the try {//Database connection pool is close when//is not actually closed, but the database connection is returned to the database connection pool. Connection.close ();} cat CH (SQLException e) {e.printstacktrace ();}}}}
Call a static method directly when referencing.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Creating a C3P0 data source