Creating a C3P0 data source

Source: Internet
Author: User

[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

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.