mysql连接池提高并发_Mysql连接池 并发

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

转自:http://www.oschina.net/code/snippet_932414_47697

/**

* 连接池类

*/

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 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();

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 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秒完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用MySqlConnection时,可能会遇到无法并发连接的问题,这通常是由于连接池设置不当或数据库配置不当引起的。以下是一些可能导致并发连接问题的常见原因和解决方法: 1. 连接池设置不当 在使用连接池时,如果连接池的最大连接数设置过低,可能会导致并发连接问题。可以通过增加连接池的最大连接数来解决这个问题。例如: ``` MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(); builder.Server = "localhost"; builder.UserID = "root"; builder.Password = "password"; builder.Database = "test"; builder.MaximumPoolSize = 100; MySqlConnection connection = new MySqlConnection(builder.ToString()); ``` 在上面的代码中,我们将连接池的最大连接数设置为100,这可以根据应用程序的需要进行调整。 2. 数据库配置不当 在MySQL中,有一些配置项可以影响并发连接的性能,例如max_connections、innodb_buffer_pool_size等。如果这些配置项设置不当,也可能会导致并发连接问题。可以通过修改这些配置项来解决这个问题。例如: ``` SET GLOBAL max_connections = 1000; SET GLOBAL innodb_buffer_pool_size = 2G; ``` 在上面的代码中,我们将最大连接数设置为1000,将InnoDB缓冲池大小设置为2G。 需要注意的是,修改MySQL的全局配置项需要有足够的权限,否则会提示“Access denied”错误。 总之,解决并发连接问题需要综合考虑多个因素,包括连接池设置、数据库配置等。可以根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值