Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

打杂uu 发布于 2016/05/05 09:20
阅读 2K+
收藏 0
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure


The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.GeneratedConstructorAccessor9.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:357)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2324)
... 17 more
Caused by: java.net.SocketException: Too many open files
at java.net.Socket.createImpl(Socket.java:460)
at java.net.Socket.getImpl(Socket.java:520)
at java.net.Socket.setTcpNoDelay(Socket.java:980)
加载中
0
南湖船老大
南湖船老大

Caused by: java.net.SocketException: Too many open files

你需要使用连接池。如果你已经用了,那么你可能没有用对连接池,导致连接并没有池化,连接过多导致的。

0
打杂uu
打杂uu

引用来自“南湖船老大”的评论

Caused by: java.net.SocketException: Too many open files

你需要使用连接池。如果你已经用了,那么你可能没有用对连接池,导致连接并没有池化,连接过多导致的。

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
 



import com.fz.util.Global;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
 
public class MySQLPool {
    private static volatile MySQLPool pool;
    private MysqlDataSource ds;
    private Map<Connection, Boolean> map;
    
    private static   String url = Global.getConfig("jdbc.url");
    private static	 String username = Global.getConfig("jdbc.username");
    private static  String password = Global.getConfig("jdbc.password");
  
    private int initPoolSize = 100;
    private int maxPoolSize = 100000;
    private int waitTime = 10000;
     
    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(200000);
            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  ,Statement stmt ,ResultSet rs) {
        if (conn == null) {
            return;
        }
        try {
        	if(rs!=null){
    			try {
    				rs.close();
    			} catch (Exception e2) {
    				// TODO: handle exception
    				e2.printStackTrace();
    			}
    			rs=null;
    		}
    		
    		if(stmt!=null){
    			try {
    				stmt.close();
    			} catch (Exception e2) {
    				// TODO: handle exception
    				e2.printStackTrace();
    			}
    			stmt=null;
    		}
        	
            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();
        }
    }
}
 
/**
 * 测试类
 */



0
10书生
10书生

既然异常显示: java.net.SocketException: Too many open files

那就首先看下异常发生现场究竟有多少数据源连接在工作。

此外,推荐使用功能完备的数据源连接池实现,比如:https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

0
whaon
whaon

linux系统吧

linux对进程的打开文件数是有限制的

打杂uu
打杂uu
ulimit -n 65535
打杂uu
打杂uu
是呀,1怎么解决这个文件问题
0
南湖船老大
南湖船老大

引用来自“南湖船老大”的评论

Caused by: java.net.SocketException: Too many open files

你需要使用连接池。如果你已经用了,那么你可能没有用对连接池,导致连接并没有池化,连接过多导致的。

引用来自“Rest721”的评论


	


果然被我言中,你是自己写的连接池,然而你写错了,用错了
返回顶部
顶部