电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 1918 人浏览分享

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

[复制链接]
1918 0

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


  1. /**
  2. * 连接池类
  3. */
  4. package com.junones.test;

  5. import java.sql.Connection;
  6. import java.sql.SQLException;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.Map.Entry;

  10. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

  11. public class MySQLPool {
  12.   private static volatile MySQLPool pool;
  13.   private MysqlDataSource ds;
  14.   private Map<Connection, Boolean> map;

  15.   private String url = "jdbc:mysql://localhost:3306/test";
  16.   private String username = "root";
  17.   private String password = "root1234";
  18.   private int initPoolSize = 10;
  19.   private int maxPoolSize = 200;
  20.   private int waitTime = 100;
  21.    
  22.   private MySQLPool() {
  23.     init();
  24.   }
  25.    
  26.   public static MySQLPool getInstance() {
  27.     if (pool == null) {
  28.       synchronized (MySQLPool.class) {
  29.         if(pool == null) {
  30.           pool = new MySQLPool();
  31.         }
  32.       }
  33.     }
  34.     return pool;
  35.   }
  36.    
  37.   private void init() {
  38.     try {
  39.       ds = new MysqlDataSource();
  40.       ds.setUrl(url);
  41.       ds.setUser(username);
  42.       ds.setPassword(password);
  43.       ds.setCacheCallableStmts(true);
  44.       ds.setConnectTimeout(1000);
  45.       ds.setLoginTimeout(2000);
  46.       ds.setUseUnicode(true);
  47.       ds.setEncoding("UTF-8");
  48.       ds.setZeroDateTimeBehavior("convertToNull");
  49.       ds.setMaxReconnects(5);
  50.       ds.setAutoReconnect(true);
  51.       map = new HashMap<Connection, Boolean>();
  52.       for (int i = 0; i < initPoolSize; i++) {
  53.         map.put(getNewConnection(), true);
  54.       }
  55.     } catch (Exception e) {
  56.       e.printStackTrace();
  57.     }
  58.   }
  59.    
  60.   public Connection getNewConnection() {
  61.     try {
  62.       return ds.getConnection();
  63.     } catch (SQLException e) {
  64.       e.printStackTrace();
  65.     }
  66.     return null;
  67.   }
  68.    
  69.   public synchronized Connection getConnection() {
  70.     Connection conn = null;
  71.     try {
  72.       for (Entry<Connection, Boolean> entry : map.entrySet()) {
  73.         if (entry.getValue()) {
  74.           conn = entry.getKey();
  75.           map.put(conn, false);
  76.           break;
  77.         }
  78.       }
  79.       if (conn == null) {
  80.         if (map.size() < maxPoolSize) {
  81.           conn = getNewConnection();
  82.           map.put(conn, false);
  83.         } else {
  84.           wait(waitTime);
  85.           conn = getConnection();
  86.         }
  87.       }
  88.     } catch (Exception e) {
  89.       e.printStackTrace();
  90.     }
  91.     return conn;
  92.   }
  93.    
  94.   public void releaseConnection(Connection conn) {
  95.     if (conn == null) {
  96.       return;
  97.     }
  98.     try {
  99.       if(map.containsKey(conn)) {
  100.         if (conn.isClosed()) {
  101.           map.remove(conn);
  102.         } else {
  103.           if(!conn.getAutoCommit()) {
  104.             conn.setAutoCommit(true);
  105.           }
  106.           map.put(conn, true);
  107.         }
  108.       } else {
  109.         conn.close();
  110.       }
  111.     } catch (SQLException e) {
  112.       e.printStackTrace();
  113.     }
  114.   }
  115. }

  116. /**
  117. * 测试类
  118. */
  119. package com.junones.test;

  120. import java.sql.Connection;
  121. import java.sql.ResultSet;
  122. import java.sql.SQLException;
  123. import java.sql.Statement;

  124. public class TestMySQLPool {
  125.   private static volatile int a;

  126.   private synchronized static void incr() {
  127.     a++;
  128.   }

  129.   public static void main(String[] args) throws InterruptedException {
  130.     int times = 10000;
  131.     long start = System.currentTimeMillis();
  132.     for (int i = 0; i < times; i++) {
  133.       new Thread(new Runnable() {

  134.         @Override
  135.         public void run() {

  136.           MySQLPool pool = MySQLPool.getInstance();
  137.           Connection conn = pool.getConnection();
  138.           Statement stmt = null;
  139.           ResultSet rs = null;
  140.           try {
  141.             stmt = conn.createStatement();
  142.             rs = stmt.executeQuery("select id, name from t_test");
  143.             while (rs.next()) {
  144.               System.out.println(rs.getInt(1) + ", "
  145.                   + rs.getString(2));
  146.             }
  147.           } catch (SQLException e) {
  148.             e.printStackTrace();
  149.           } finally {
  150.             incr();
  151.             if (rs != null) {
  152.               try {
  153.                 rs.close();
  154.               } catch (SQLException e) {
  155.                 e.printStackTrace();
  156.               }
  157.             }
  158.             if (stmt != null) {
  159.               try {
  160.                 stmt.close();
  161.               } catch (SQLException e) {
  162.               }
  163.             }
  164.             pool.releaseConnection(conn);
  165.           }
  166.         }
  167.       }).start();
  168.     }
  169.     while (true) {
  170.       if (a == times) {
  171.         System.out.println("finished, time:"
  172.             + (System.currentTimeMillis() - start));
  173.         break;
  174.       }
  175.       Thread.sleep(100);
  176.     }
  177.   }
  178. }
复制代码


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

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






您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.