用于App服務(wù)端的MySQL連接池(支持高并發(fā))
本文向大家介紹了簡單的MySQL連接池,用于App服務(wù)端比較合適,分享給大家供大家參考,具體內(nèi)容如下
/**
* 連接池類
*/
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);
}
}
}
測試結(jié)果:1萬個并發(fā),5秒完成。
以上就是為大家分享的MySQL連接池類,希望大家喜歡,謝謝大家的關(guān)注。
相關(guān)文章
MySQL的多版本并發(fā)控制MVCC的實(shí)現(xiàn)
MVCC就是多版本并發(fā)控制,本文主要介紹了MySQL的多版本并發(fā)控制MVCC的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
MySQL四種日志binlog/redolog/relaylog/undolog詳解
undo?log主要存儲的也是邏輯日志,比如我們要insert一條數(shù)據(jù)了,那undo?log會記錄的一條對應(yīng)的delete日志,我們要update一條記錄時,它會記錄一條對應(yīng)相反的update記錄,這篇文章主要介紹了MySQL四種日志binlog/redolog/relaylog/undolog,需要的朋友可以參考下2024-08-08
MySQL學(xué)習(xí)之?dāng)?shù)據(jù)更新操作詳解
這篇文章我們將學(xué)習(xí)一下用于數(shù)據(jù)更改的 “UPDATE” 語句, “UPDATE” 語句也是屬于 DML 這一類數(shù)據(jù)庫操作語言,感興趣的可以了解一下2022-08-08
MySQL幾點(diǎn)重要的性能指標(biāo)計(jì)算和優(yōu)化方法總結(jié)
下面小編就為大家?guī)硪黄狹ySQL幾點(diǎn)重要的性能指標(biāo)計(jì)算和優(yōu)化方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
分享幾道關(guān)于MySQL索引的重點(diǎn)面試題
這篇文章主要給大家介紹了幾道關(guān)于MySQL索引的重點(diǎn)面試題,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
mysql使用物理備份安裝xtrabackup的詳細(xì)過程
這篇文章主要介紹了mysql使用物理備份安裝xtrabackup的詳細(xì)過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
Slave memory leak and trigger oom-killer
這篇文章主要介紹了Slave memory leak and trigger oom-killer,需要的朋友可以參考下2016-07-07

