MySQL Java connection pool. opensta is used to test the concurrent access of 50 people for 10 thousand times. It is easy to use.

Source: Internet
Author: User
Package dBc; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. util. arraylist; public class databaseconnection {Private Static int maxconnectnum = 20; private Java. SQL. connection conn [] = new connection [maxconnectnum]; Private Static arraylist connectpool = new arraylist (); Private Static int flag = 0; Public databaseconnection () {If (flag = 0) {Init () ;}} private connection getconnecti Onfromdatabase () {connection trueconn = NULL; try {// class. forname ("org. gjt. mm. mySQL. driver "). newinstance (); Class. forname ("com. mySQL. JDBC. driver "). newinstance (); string url = "JDBC: mysql: // localhost: 3306/SSK? User = root & Password = dzjc & useunicode = true & characterencoding = GBK "; trueconn = drivermanager. getconnection (URL);} catch (exception ex) {system. out. println ("data connection error:" + ex. tostring ();} return trueconn;} // all connections are established here; private void Init () {for (INT I = 0; I <maxconnectnum; I ++) {conn [I] = getconnectionfromdatabase (); connectpool. add (I, Conn [I]);} flag = 1;} // obtain an available connection from the connection pool public connection getconnection () {connection conn = NULL; if (connectpool. size () = 0) {try {Java. lang. thread. sleep (1000); getconnection ();} catch (interruptedexception ex) {system. out. println ("all connections are used up. Sleep has an error. ") ;}} else {conn = (connection) connectpool. remove (0) ;}return conn ;}// provide it to external programs for calling. unused connections are put back in the connection pool... public Boolean release (connection conn) {return connectpool. add (conn);} public static void main (string [] ARGs ){}}

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.