DBCP2 Connection Pool Get database connection connection

Source: Internet
Author: User

First, take a look at the way you create it manually

     Public StaticConnection getconnection () {Connection conn=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); Conn=Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mytable?useunicode=true&characterencoding=utf8","Root",                    "123456"); } Catch(Exception e) {e.printstacktrace (); }        returnConn; }

This straightforward way to build must be noted: the release of the link after the access is complete.

Conn.close ();

If this is the time to loop through a table 100 times, you can see that the database has set up 100 links, and if you release it, 100 links will be released, either fast or slow.

If the database access is very frequent, it is a pity ah, just set up the good one link is torn down, just finished and then create a new link ...

So, the connection pool came out. More familiar with C3P0, DBCP2 and so on.

Second, through the DBCP2 connection pool to get connection

A good initialization configuration instance.

1     //ThreadSafe2     Private StaticFinal threadlocal<connection>Connholder;3     Private Staticfinal Basicdatasource DataSource;4     5     Static {6Connholder =NewThreadlocal<connection>();7DataSource =NewBasicdatasource ();8Datasource.setdriverclassname ("Com.mysql.jdbc.Driver");9Datasource.seturl ("jdbc:mysql://127.0.0.1:3306/mytable");TenDatasource.setusername ("Root"); OneDatasource.setpassword ("123456"); A         ///sets the maximum total number of idle and borrowed connections, which can be activated at the same time.  -Datasource.setmaxtotal ( -); -         //Set Initial Size theDatasource.setinitialsize (5); -         //Minimum idle connection -Datasource.setminidle (8); -         //Maximum idle connection +Datasource.setmaxidle ( -); -         //timeout wait time milliseconds +Datasource.setmaxwaitmillis (2*10000); A         //only discover that the current connection is invalid, and then create a connection for the current query to use atDatasource.settestonborrow (true); -         //removeabandonedtimeout: Over time limit, recycle unused (obsolete) connection (default is 300 seconds, adjust to +) -Datasource.setremoveabandonedtimeout ( the); -         //removeabandoned: After more than removeabandonedtimeout time, whether to enter -         //recycle of rows with no connection (deprecated) (default = False, adjusted to true) -         //data_source.setremoveabandonedonmaintenance (removeabandonedonmaintenance); inDatasource.setremoveabandonedonborrow (true); -         //Testwhileidle toDatasource.settestonreturn (true); +         //Testonreturn -Datasource.settestonreturn (true); the         //setremoveabandonedonmaintenance *Datasource.setremoveabandonedonmaintenance (true); $         //Record LogPanax NotoginsengDatasource.setlogabandoned (true); -Datasource.setdefaultautocommit (true); the  +     } A  the      Public StaticConnection getconnection () { +Connection conn = Connholder.Get(); -         if(conn = =NULL) { $             Try { $conn =datasource.getconnection (); -System. out. println ("Get Connection Success"); -}Catch(SQLException e) { theSystem. out. println ("Get connection failure:"+e); -}finally {WuyiConnholder.Set(conn); the             } -         } Wu         returnConn; -     } About  $      Public Static voidCloseConnection () { -Connection conn = Connholder.Get(); -         if(Conn! =NULL) { -             Try { A conn.close (); +System. out. println ("Close Connection Success"); the}Catch(SQLException e) { -System. out. println ("Close connection failure:"+e); $                 Throw NewRuntimeException (e); the}finally { the Connholder.remove (); the             } the         } -}

The actual test, it seems Dbutil helped me release the link, it seems that the upper CloseConnection method is useless.

DBCP2 Connection Pool Get database connection connection

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.