Mysql connection pool for app server (high concurrency support) _mysql

Source: Internet
Author: User
Tags mysql connection pool stmt volatile

This article introduced a simple MySQL connection pool, for the app server is more suitable for everyone to share for your reference, the specific content as follows

/** * Connection Pool class * * Package com.junones.test;
Import java.sql.Connection;
Import java.sql.SQLException;
Import Java.util.HashMap;
Import Java.util.Map;
 
Import Java.util.Map.Entry;
 
Import Com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  public class Mysqlpool {private static volatile mysqlpool pool;
  Private Mysqldatasource DS;
 
  Private map<connection, boolean> Map;
  Private String URL = "Jdbc:mysql://localhost:3306/test";
  Private String username = "root";
  Private String Password = "root1234";
  private int initpoolsize = 10;
  private int maxpoolsize = 200;
   
  private int waittime = 100;
  Private Mysqlpool () {init (); public static Mysqlpool getinstance () {if (pool = = null) {synchronized (Mysqlpool.class) {if (
        Pool = = null) {pool = new mysqlpool ();
  }} return pool;
      private void Init () {try {ds = new Mysqldatasource ();
      Ds.seturl (URL);
      Ds.setuser (username);Ds.setpassword (password);
      Ds.setcachecallablestmts (TRUE);
      Ds.setconnecttimeout (1000);
      Ds.setlogintimeout (2000);
      Ds.setuseunicode (TRUE);
      Ds.setencoding ("UTF-8");
      Ds.setzerodatetimebehavior ("Converttonull");
      Ds.setmaxreconnects (5);
      Ds.setautoreconnect (TRUE);
      Map = new hashmap<connection, boolean> ();
      for (int i = 0; i < initpoolsize i++) {map.put (GetNewConnection (), true);
    } catch (Exception e) {e.printstacktrace ();
    } public Connection GetNewConnection () {try {return ds.getconnection ();
    catch (SQLException e) {e.printstacktrace ();
  return null;
    Public synchronized Connection getconnection () {Connection conn = null;  try {for (entry<connection, boolean> entry:map.entrySet ()) {if (Entry.getvalue ()) {conn
          = Entry.getkey ();
          Map.put (conn, false);
        Break } IF (conn = = null) {if (Map.size () < maxpoolsize) {conn = GetNewConnection ();
        Map.put (conn, false);
          else {wait (waittime);
        conn = getconnection ();
    A catch (Exception e) {e.printstacktrace ());
  Return conn;
    public void Releaseconnection (Connection conn) {if (conn = = null) {return;
        try {if (Map.containskey (conn)) {if (conn.isclosed ()) {map.remove (conn);
          } else {if (!conn.getautocommit ()) {Conn.setautocommit (true);
        } map.put (conn, true);
      } else {conn.close ();
    } catch (SQLException e) {e.printstacktrace ();
 
}}/** * Test class * * Package com.junones.test;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
 
Import java.sql.Statement;
 
  public class Testmysqlpool {private static volatile int A; Private synchronized static void incr () {a++;
    public static void Main (string[] args) throws interruptedexception {int times = 10000;
    Long start = System.currenttimemillis ();
 
          for (int i = 0; I < times i++) {New Thread (new Runnable () {@Override public void run () {
          Mysqlpool pool = mysqlpool.getinstance ();
          Connection conn = Pool.getconnection ();
          Statement stmt = null;
          ResultSet rs = null;
            try {stmt = Conn.createstatement ();
            rs = Stmt.executequery ("Select ID, name from t_test");
            while (Rs.next ()) {System.out.println (Rs.getint (1) + "," + rs.getstring (2));
          } catch (SQLException e) {e.printstacktrace ();
            finally {incr ();
              if (Rs!= null) {try {rs.close (); catch (SQLException e) {E.printStackTrace ();
              } if (stmt!= null) {try {stmt.close ();
          The catch (SQLException e) {}} pool.releaseconnection (conn);
    }}). Start (); } while (true) {if (A = = times) {System.out.println ("Finished, Time:" + (System.currenttim
        Emillis ()-start);
      Break
    } thread.sleep (100);
 }
  }
}

Test results: 10,000 concurrent, 5 seconds complete.

The above is for you to share the MySQL connection pool class, I hope you like, thank you for your attention.

Related Article

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.