Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.sql.BatchUpdateException:I/O错误:对等方重置连接:套接字写入错误_Java_Sql Server_Sockets - Fatal编程技术网

java.sql.BatchUpdateException:I/O错误:对等方重置连接:套接字写入错误

java.sql.BatchUpdateException:I/O错误:对等方重置连接:套接字写入错误,java,sql-server,sockets,Java,Sql Server,Sockets,我使用jtds 1.3.1连接到MSSQL Server 2008 SP3。我在3个独立线程中创建3个PreparedStatements,并向它们添加查询。当我执行PreparedStatements时,会出现以下异常: java.sql.BatchUpdateException: I/O Error: Connection reset by peer: socket write error at net.sourceforge.jtds.jdbc.JtdsStatement.exe

我使用jtds 1.3.1连接到MSSQL Server 2008 SP3。我在3个独立线程中创建3个PreparedStatements,并向它们添加查询。当我执行PreparedStatements时,会出现以下异常:

 java.sql.BatchUpdateException: I/O Error: Connection reset by peer: socket write error
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:1091)
    at de.mvn.gotdb_mvn.Main.main(Main.java:181)
请帮我解决这个问题。这是我的代码:

  public static void main(String[] args) throws SQLException
{

    final Importer importer = new Importer();
    importer.openConnection();
    final Connection connection = importer.getCon();
    importer.createNewTables();

    StatementPreparer test_falldata = new StatementPreparer(connection.prepareStatement(q1)) {

        @Override
        public void doRun()
        {
            try
            {
                PreparedStatement falldata_stmt = getStmt();
                // |         
                String[] line = this.getLine();
                falldata_stmt.setInt(1, Integer.parseInt(line[0].trim()));
                //FallNr
                falldata_stmt.setString(2, line[1]);
                //DRG
                falldata_stmt.setString(3, line[18]);
                //MDC
                falldata_stmt.setString(4, line[19]);
                //PCCL
                falldata_stmt.setString(5, line[20]);
                falldata_stmt.addBatch();

            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }

    };

    StatementPreparer test_diagnosen = new StatementPreparer(connection.prepareStatement(q2)) {

        @Override
        public void doRun()
        {
            try
            {
                PreparedStatement diagnosen_stmt = getStmt();

                String[] line = this.getLine();
                String[] diagnose_only = line[13].split("~");

                for (int i = 0; i < diagnose_only.length; i++)
                {
                    int index_of_roof = diagnose_only[i].indexOf("^");
                    //FallNr
                    diagnosen_stmt.setString(1, line[1]);
                    //Code 
                    diagnosen_stmt.setString(2, diagnose_only[i].substring(0, index_of_roof));
                    //Used int
                    diagnosen_stmt.setInt(3, Integer.parseInt(diagnose_only[i].substring(index_of_roof + 1, index_of_roof + 2)));
                    //HDX/NDX int
                    if (i == 1)
                    {
                        diagnosen_stmt.setInt(4, 1);
                    } else
                    {
                        diagnosen_stmt.setInt(4, 0);
                    }
                    diagnosen_stmt.addBatch();
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }

        }

    };

    StatementPreparer test_proceduren = new StatementPreparer(connection.prepareStatement(q3)) {

        @Override
        public void doRun()
        {
            try
            {
                PreparedStatement proceduren_stmt = getStmt();

                String[] line = this.getLine();
                String[] procedure_only = line[14].split("~");

                for (String single_proce : procedure_only)
                {
                    if (!single_proce.isEmpty())
                    {
                        int index_of_amp = single_proce.indexOf("&");
                        //fallnr
                        proceduren_stmt.setString(1, line[1]);
                        //code
                        proceduren_stmt.setString(2, single_proce.substring(0, index_of_amp));

                        //lokalisation
                        if (single_proce.lastIndexOf("&") == index_of_amp + 1)
                        {
                            proceduren_stmt.setString(4, "");
                            //Datum
                            proceduren_stmt.setDate(3, getProcedureDate(single_proce, proce_no_lok));
                        } else
                        {
                            String lokalisation = single_proce.substring(index_of_amp + 1, single_proce.lastIndexOf("&"));
                            proceduren_stmt.setString(4, lokalisation);
                            //Datum
                            proceduren_stmt.setDate(3, getProcedureDate(single_proce, proce_with_lok));
                        }
                        //used
                        proceduren_stmt.setInt(5, Integer.parseInt(single_proce.substring(single_proce.indexOf("^") + 1, single_proce.indexOf("^") + 2)));
                        proceduren_stmt.addBatch();

                    }
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }

        }

    };

    PreparerListener listener = new PreparerListener();
    test_falldata.addListener(listener);
    test_diagnosen.addListener(listener);
    test_proceduren.addListener(listener);
    try
    {

        for (String nextline = importer.getNextLine(); nextline != null; nextline = importer.getNextLine())
        {
            test_falldata.setLine(nextline);
            test_diagnosen.setLine(nextline);
            test_proceduren.setLine(nextline);
            test_falldata.resumeThread();
            test_diagnosen.resumeThread();
            test_proceduren.resumeThread();
        }

        if(!connection.isClosed())
        {   
            test_falldata.getStmt().executeBatch();
            if(connection.isClosed())
            {
            System.out.print("CLOSED");
            System.exit(-10);
            }
            test_diagnosen.getStmt().executeBatch();// Exception 
            test_proceduren.getStmt().executeBatch();
        }else
        {
            System.out.println("Connection has Closed");
        }
    } catch (Exception e)
    {
        e.printStackTrace();
    }

}

我不完全理解您的代码,但我的第一个猜测是您只使用了一个数据库连接,随后被多个线程使用。这似乎是因为您只调用openConnection一次。线程不使用连接,只使用PreparedStatement
   public void openConnection(String p_propertys)
   {
     try{
      Class.forName("net.sourceforge.jtds.jdbc.Driver");
     String url="jdbc:jtds:sqlserver://LB236/checkpointDB;loginTimeout=30;socketTimeout=1800";
     String user ="jadmin";
     String pw="jadmin123";
     con = DriverManager.getConnection(url,user,pw);
     }catch(Exception e){e.printStacktrace();}
   }