Jdbcutils Tool Class

Source: Internet
Author: User

Jdbcutils.java

Importjava.sql.Connection;Importjava.sql.SQLException;ImportJavax.sql.DataSource;ImportCom.mchange.v2.c3p0.ComboPooledDataSource;/*** Dependent: * + c3p0-config.xml * + C3p0-0.9.2-pre1.jar * + Mchange-commons-0.2.jar * Version 1.3 * Update Date: 20 18/07/03 *@authorCeobai **/ Public classJdbcutils {//Combopooleddatasource (String configname) parameter configname refers to <named-config name= "MySQL" in the configuration file C3p0-config.xml >...</named-config>//if the ConfigName parameter is not entered, then the default <default-config>...</defalut-config> is used, then the null parameter can be passed.    Private StaticCombopooleddatasource DataSource =NewCombopooleddatasource ("MySQL"); Private StaticConnection con =NULL; Private Staticthreadlocal<connection> TL =NewThreadlocal<connection>(); /*** Get Connection Object *@return     * @throwsSQLException*/     Public StaticConnection getconnection ()throwssqlexception{con=Tl.get (); if(Con! =NULL){returncon;} returndatasource.getconnection (); }    /*** Get Connection pool Object *@return     */     Public StaticDataSource Getdatasource () {returnDataSource; }    /*** Open Transaction *@throwsSQLException*/     Public Static voidBeginTransaction ()throwssqlexception{con=Tl.get (); if(Con! =NULL){Throw NewRuntimeException ("The transaction is already open!" Cannot be opened repeatedly! ");} Con=getconnection (); Con.setautocommit (false);    Tl.set (con); }    /*** Submit a transaction *@throwsSQLException*/     Public Static voidCommitTransaction ()throwssqlexception{con=Tl.get (); if(Con = =NULL){Throw NewRuntimeException ("The transaction hasn't been opened yet!" Cannot submit! ");}        Con.commit (); Con.close ();//returning a Connection object to a connection poolTl.remove ();//Remove the Connection object con. So tl.get () = = NULL    }    /*** ROLLBACK TRANSACTION *@throwsSQLException*/     Public Static voidRollbackTransaction ()throwssqlexception{con=Tl.get (); if(Con = =NULL){Throw NewRuntimeException ("The transaction hasn't been opened yet!" Can't Roll back! ");}        Con.rollback ();        Con.close ();    Tl.remove (); }    /*** Close non-transactional private connections *@paramConnection *@throwsSQLException*/     Public Static voidReleaseconnection (Connection Connection)throwssqlexception{con= Tl.get ();//get a transactional private connection        if(Con = =NULL) {connection.close ();} if(Con! =connection)    {Connection.close ();} } }

C30p-config.xml

<?xml version= "1.0" encoding= "UTF-8"?><c3p0-config> <!--C3P0 are configured in two ways, one is the default Default-config, One is differentiated by name: Named-config needs to add a property value name--<default-config> <property name= "Jdbcurl" >jdbc:oracle:thin:username/[email protected]:1521:emp</property> <property name= "Driverclass" >oracle.jdbc.driver.OracleDriver</property> <property name= "user" & gt;root</property> <property name= "password" ></property> <property name= "Acquir Eincrement ">3</property> <property name=" initialpoolsize ">10</property> <property na Me= "Minpoolsize" >2</property> <property name= "maxpoolsize" >10</property> </default-config> <named-config name= "MySQL" > <property name= "Jdbcurl" >jdbc:mysql://localhost:3306/db_user</property><property name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "user" >root</ property> <property name= "password" ></property> <property name= "Acquireincrement" & gt;3</property> <property name= "initialpoolsize" >10</property> <property name= "Minpools Ize ">2</property> <property name=" maxpoolsize ">10</property> </named-config></c3p0 -config>

Jdbcutils Tool Class (RPM)

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.