Create a C3P0 data source,

Source: Internet
Author: User

Create a C3P0 data source,

[Author]: kwu

When creating a C3P0 data source, there are very few connections to the database using JDBC in actual development. Generally, the data source is used. C3P0 is an open source data source, and the actual project is used a lot:


1. Added support for maven.

<dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency>

2. Configure C3P0, the 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. Reference 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 ** contains methods such as getting database connections and disabling database resources. */public class JDBCTools {// process database transactions // submit the 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 the transaction public static void beginTx (Connection connection) {if (connection! = Null) {try {connection. setAutoCommit (false);} catch (SQLException e) {e. printStackTrace () ;}} private static DataSource dataSource = null; // The database connection pool should be initialized only 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 () ;}catch (SQLException e) {e. printStackTrace () ;}} if (statement! = Null) {try {statement. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (connection! = Null) {try {// when the Connection object of the database Connection pool is closed, // The database Connection is returned to the database Connection pool instead of being closed. connection. close ();} catch (SQLException e) {e. printStackTrace ();}}}}

You can directly call Static Methods When referencing them.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.