Data Source C3P0 configuration,

Source: Internet
Author: User

Data Source C3P0 configuration,

1. Import the jar package (dbutil-> QueryRunner)

2. C3P0Util tool class

Package com. learning. utils; import java. beans. propertyVetoException; 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; public class C3P0Util {private static DataSource dataSource = new ComboPooledDataSource ();/* the configuration file private static combooleddatasource is not used = New ComboPooledDataSource (); static {try {dataSource. setDriverClass ("com. mysql. jdbc. driver "); // loads the jdbc driver dataSource. setJdbcUrl ("jdbc: mysql: // localhost: 3306/book"); dataSource. setUser ("root"); dataSource. setPassword ("root");} catch (PropertyVetoException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} * // obtain the Connection public static Connection getConnection () {tr from the data source Y {return dataSource. getConnection () ;}catch (SQLException e) {throw new RuntimeException ("database Connection failed") ;}// release the public static void release (connection Connection, Statement statement, resultSet resultSet) {if (connection! = Null) {try {// puts unused connection objects back into the connection pool (actually in decorative mode) connection. close ();} catch (SQLException e) {e. printStackTrace ();} connection = null;} if (statement! = Null) {try {statement. close () ;}catch (SQLException e) {e. printStackTrace () ;} statement = null ;}if (resultSet! = Null) {try {resultSet. close () ;}catch (SQLException e) {e. printStackTrace () ;}resultset = null ;}}}

3. configuration file: c3p0-config.xml (name cannot be changed)

<c3p0-config>    <default-config>                <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql://localhost:3306/book</property>        <property name="user">root</property>        <property name="password">root</property>                    <property name="automaticTestTable">con_test</property>        <property name="checkoutTimeout">30000</property>        <property name="idleConnectionTestPeriod">30</property>        <property name="initialPoolSize">10</property>        <property name="maxIdleTime">30</property>        <property name="maxPoolSize">100</property>        <property name="minPoolSize">10</property>        <property name="maxStatements">200</property>        <user-overrides user="test-user">            <property name="maxPoolSize">10</property>            <property name="minPoolSize">1</property>            <property name="maxStatements">0</property>        </user-overrides>    </default-config>     </c3p0-config>

 

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.