Jdbc单例连接的CommunicationsException

时间:2018-06-16 14:38:22

标签: java jdbc

我期待在我的整个应用程序中使用singleton jdbc连接。 使用我的下面的编码,当我开始我的tomcat从数据库获取数据后,在某个时间后,我得到低于异常。 我没有得到什么问题,任何人都可以提出可能的原因。

例外:

com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 65,128,558 milliseconds ago.  The last packet sent succ$
        at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:615)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:57)
        at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2011)
        at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1826)
        at com.mysql.cj.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1923)
        at com.pradeep.sms.db.DBDataAccess.getDropdown(DBDataAccess.java:25)
        at com.pradeep.sms.controller.WebServicesController.getDropdown(WebServicesController.java:35)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
        at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

单身人士课程

public class DBConnection {
    private static String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static String DB_USER = "pradeep";
    private static String DB_PASSWORD = "12345678";
    private static String DB_NAME = "edusoft";
    private static String HOSTNAME = "host";
    private static String DB_PORT = "3306";
    private static String DB_CONNECTION = "jdbc:mysql://" + HOSTNAME + ":" + DB_PORT + "/" + DB_NAME + "?user="
            + DB_USER + "&password=" + DB_PASSWORD;

    private static final DBConnection ONLY_ONE = new DBConnection();
    private Connection con;
    private DBConnection() {
            try {
                Class.forName(DB_DRIVER);
                con = DriverManager.getConnection(DB_CONNECTION);// SQLException
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
    }

    public static DBConnection getInstance() {
        return ONLY_ONE;
    }

    public Connection getcon() {
        return con;
    }
}

我的申请类

private Connection connection = DBConnection.getInstance().getcon();
    public String getDropdown(String dropdownName ) {

        String proCall="{call getDropdown(?)}";
        JSONObject jSONObject = new JSONObject();
        ArrayList<String> result = new ArrayList<String>();
        try {
            preparedStatement = connection.prepareStatement(proCall);
            preparedStatement.setString(1, dropdownName);

            resultSet= preparedStatement.executeQuery();
            while (resultSet.next()) {
                String classNames = resultSet.getString(1);
                result.add(classNames);
            }
            JSONArray jsArray = new JSONArray(result);
            jSONObject.put("rs", jsArray);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(resultSet!=null)
                try {
                    resultSet.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            if(preparedStatement !=null){
                try {
                    preparedStatement.close();  
                } catch (Exception e2) {

                }
            }
        }
        return jSONObject.toString();
    }

1 个答案:

答案 0 :(得分:-1)

服务器未启动或主机错误。检查服务器状态和您在代码中提供的主机名