соединение java.sql.SQLNonTransientConnectionException: не удалось создать соединение с сервером базы данных - PullRequest
0 голосов
/ 10 сентября 2018

Я пытаюсь добавить несколько записей в базу данных MySQL, используя jdbc в java. Эта программа работает абсолютно нормально для 100 записей. Но после 100 записей я получаю следующую ошибку.

Exception occured while obtaining 

connectionjava.sql.SQLNonTransientConnectionException: Could not create connection to database server.
java.lang.NullPointerException
    at com.rest.EmployeeRest.insertDesigMySQLData(EmployeeRest.java:446)
    at com.rest.EmployeeRest.syncDesignation(EmployeeRest.java:188)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    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)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

После того, как гугл об этой ошибке, кто-то скажет использовать команду telnet на localhost 3306 (порт MySQL). и это дает мне ошибку «Слишком много соединений». Может ли кто-нибудь помочь мне решить эту проблему.

API-код для получения данных и добавления в MySQL:

ArrayList<Employee> arrDesig = getAllDesig();
setTotRecords(arrDesig.size());
System.out.println("Oracle TotRecords = "+getTotRecords());
if(arrDesig.size() > 0){
    for(int iCnt=0; iCnt<arrDesig.size(); iCnt++){
        if(insertDesigMySQLData(arrDesig.get(iCnt)) == 1){
            mysqlDat++;
        }
        System.out.println("MySQL inserted data = "+mysqlDat);
    }
    if(getTotRecords() == mysqlDat){
        retval = 1;
    }else{
        retval = 0;
    }
}else{
    retval = 0;
}

MySQL код для добавления:

int retval = 0;
    OracleDataSource objMySQLDataSource = new OracleDataSource("mysql");
    try{
        Connection connection = objMySQLDataSource.getDbConnection();
        CallableStatement pstmt=connection.prepareCall("{call addDesig(?,?,?)}");
        pstmt.setString(1,objEmployee.getDesig_id());
        System.out.println("getDesig_id() = "+objEmployee.getDesig_id());
        pstmt.setString(2,objEmployee.getDesig_nm());
        System.out.println("getDesig_nm() = "+objEmployee.getDesig_nm());
        pstmt.registerOutParameter(3, Types.NUMERIC);
        pstmt.executeUpdate();
        retval = pstmt.getInt(3);
        pstmt.close();
    }catch (Exception e) {
        retval = 0;
        e.printStackTrace();
    }finally{
        objMySQLDataSource.connectionClose();
    }
    return retval;

Oracle Connection

OracleDataSource(){
    setUrl("jdbc:oracle:thin:@dburl");
    setDriver("oracle.jdbc.OracleDriver");
    setUserName("admin");
    setPassword("**********");
}

MySQL Connection

OracleDataSource(String mysql){
    setUrl("jdbc:mysql://localhost:3306/academicdb?verifyServerCertificate=false&useSSL=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
    setDriver("com.mysql.cj.jdbc.Driver");//com.mysql.jdbc.Driver
    setUserName("root");
    setPassword("");
}

public Connection getDbConnection() {
        Connection con = null;
        try{
            Class.forName(getDriver());
            con = DriverManager.getConnection(getUrl(), getUserName(), getPassword());
        } catch (Exception se) {
            System.out.println("Exception occured while obtaining connection" + se.toString());
        }
        return con;
    }
...