C3P0 Connection Pool & Jdbcutils

Source: Internet
Author: User
Tags connection pooling

First, the introduction of JAR package

Second, Java code

1 @Test2      Public voidTestxml ()throwsException {3         //creating the C3P0 Connection Pool core tool class4         //automatic loading src under c3p0 profile "C3p0-config.xml"5Combopooleddatasource DataSource =NewCombopooleddatasource ();//Use the default configuration6PreparedStatement pstmt =NULL;7         8         //Get Connections9Connection con =datasource.getconnection ();Ten          for(intI=1; i<11;i++){ OneString sql = "INSERT into employee (EMPNAME,DEPT_ID) VALUES (?,?)"; A             //Perform the update -PSTMT =con.preparestatement (SQL); -Pstmt.setstring (1, "Rose" +i); thePstmt.setint (2, 1); - pstmt.executeupdate (); -         } - pstmt.close (); +         //Close - con.close (); +          A}

Iii. configuration Files (c3p0-config.xml)

1 <C3p0-config>2     <Default-config>3         < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/jdbc_demo4         </ Property>5         < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>6         < Propertyname= "User">Root</ Property>7         < Propertyname= "Password">Root</ Property>8         < Propertyname= "Initialpoolsize">3</ Property>9         < Propertyname= "Maxpoolsize">6</ Property>Ten         < Propertyname= "MaxIdleTime">1000</ Property> One     </Default-config> A  -  -     <Named-configname= "Oracle_config"> the         < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/jdbc_demo</ Property> -         < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property> -         < Propertyname= "User">Root</ Property> -         < Propertyname= "Password">Root</ Property> +         < Propertyname= "Initialpoolsize">3</ Property> -         < Propertyname= "Maxpoolsize">6</ Property> +         < Propertyname= "MaxIdleTime">1000</ Property> A     </Named-config> at  -  - </C3p0-config>

Iv. jdbcutils (using connection pooling)

1  Public classJdbcutil {2 3     //Initializing the connection pool4     Private StaticDataSource DataSource;5     6     //Static code block initializes the connection pool resource and executes only once7     Static{8DataSource =NewCombopooleddatasource ();9     }Ten  One     /** A * Return Connection object -      */ -      Public StaticConnection getconnection () { the         Try { -             returndatasource.getconnection (); -}Catch(SQLException e) { -             Throw NewRuntimeException (e); +         } -     } +  A     /** at * Close -      */ -      Public Static voidCloseAll (Connection con, Statement stmt, ResultSet rs) { -         Try { -             if(rs! =NULL) { -Rs.close ();//Fast Exception Capture Alt + SHIFT + Z inrs =NULL;//recommend recycling resources during garbage collection -             } to             if(stmt! =NULL) { + stmt.close (); -stmt =NULL; the             } *             if(Con! =NULL&&!con.isclosed ()) { $ con.close ();Panax NotoginsengCon =NULL; -             } the}Catch(SQLException e) { +             Throw NewRuntimeException (e); A         } the     } +}

C3P0 Connection Pool & Jdbcutils

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.