java连接多个mysql,JAVA:MySql:太多连接

I think that my application is cursed, debug goes where it wants and I don't know why.

Line by line debugging seems to analyze also the commented rows.

I think that the problems are on my Connection method, I see a significant performance slowdown, and at the third (or fourth nvm) connection I get this error:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections

I'm sure that I close the connection each time I've access to the DB (with finally statement out of the try catch in the implementation).

Here's my connection class:

package Connection;

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.rmi.RemoteException; import java.sql.*; import java.util.Properties;

public class ConnectionDB {

public static ResultSet rs = null;

public static Statement stat = null;

public static Connection cn = null;

public static void connect() throws RemoteException {

String dbHost="",dbUser="",dbPassword="";

Properties prop=new Properties();

try

{

//carico il file:

prop.load(new FileInputStream("***/config.properties"));

//Leggo le proprietà del file:

dbHost=prop.getProperty("host");

dbUser=prop.getProperty("user");

dbPassword=prop.getProperty("password");

}catch(FileNotFoundException fe){

System.out.println("config file not found");

}catch(IOException ex){

System.out.println("Error reading config file");

}

try

{

String driver = "com.mysql.jdbc.Driver";

Class.forName(driver);

dbHost = "jdbc:mysql://"+dbHost;

cn = DriverManager.getConnection(dbHost,dbUser,dbPassword);

stat = cn.createStatement();

}catch(SQLException e){

System.out.println("Can't connect to the DB");

}

catch(ClassNotFoundException cln){

System.out.println("Error using JDBC driver");

}

}

public static void disconnect() throws RemoteException {

try{

if(rs != null) rs.close();

if(stat != null) stat.close();

if(cn != null) cn.close();

}

catch(SQLException sqlEx){

System.out.println("Error: disconnect");

}

}

}

解决方案

Normally there is limited number of connections available for user. Additionally, establishing connections is costly operation. Therefore, if you experience performance problems you should start using connection pool, discussion about connection pools

Regarding your code, make sure you always release connections in finally block. Also we don't know what kind of queries you run. Print your queries and try running them manually in mysql and see if the problems is your SQLs, not Java. Also you never close FileInputStream meaning you can run out of file handlers.

In case you run the same statement in a loop consider using PreparedStatements and batch updates and transactions. If you use transactions, make sure you don't have much data in uncommitted state.

If you want to analyze performance of your queries you can use JAMon (Java Application Monitor)

Suggestion to increase number of connections is similar to advising a person with diarrhea to eat more food.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值