国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

用于App服務端的MySQL連接池(支持高并發)

2024-07-24 13:08:40
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了用于App服務端的MySQL連接池,并支持高并發,感興趣的小伙伴們可以參考一下
 

本文向大家介紹了簡單的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連接池類,希望大家喜歡,謝謝大家的關注。



注:相關教程知識閱讀請移步到MYSQL教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通许县| 化德县| 新平| 蓝田县| 永昌县| 谷城县| 沾益县| 靖西县| 荆州市| 平原县| 余姚市| 汉中市| 南华县| 张家港市| 开原市| 云龙县| 德昌县| 墨江| 庆阳市| 芮城县| 浮山县| 陇西县| 孟连| 仪陇县| 谷城县| 秦皇岛市| 五台县| 阳新县| 凉城县| 建平县| 礼泉县| 河西区| 平原县| 安阳县| 剑阁县| 兰考县| 荔波县| 瑞昌市| 望谟县| 拉孜县| 永康市|