欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用于App服务端的MySQL连接池(支持高并发)

程序员文章站 2024-02-24 21:50:52
本文向大家介绍了简单的mysql连接池,用于app服务端比较合适,分享给大家供大家参考,具体内容如下 /** * 连接池类 */ package com...

本文向大家介绍了简单的mysql连接池,用于app服务端比较合适,分享给大家供大家参考,具体内容如下

/**
 * 连接池类
 */
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();
        }
      }
    } 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();
    }
  }
}
 
/**
 * 测试类
 */
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();
              } catch (sqlexception e) {
              }
            }
            pool.releaseconnection(conn);
          }
        }
      }).start();
    }
    while (true) {
      if (a == times) {
        system.out.println("finished, time:"
            + (system.currenttimemillis() - start));
        break;
      }
      thread.sleep(100);
    }
  }
}

测试结果:1万个并发,5秒完成。

以上就是为大家分享的mysql连接池类,希望大家喜欢,谢谢大家的关注。