如何解决"线程中的异常"Thread-1"java.lang.StackOverflowError"?在我的申请中

hon*_*ey1 -5 java stack-overflow multithreading

在我的应用程序中,我通过IP地址通信从另一个系统数据库访问数据.

所以为此,如果数据库系统当时处于脱机状态,则在连接期间发生异常,因此在catch块中我再次调用run()方法,当它到达联机正常的应用程序执行流程时.但在这个过程中我得到"线程中的异常"Thread-1"java.lang.StackOverflowError"这个异常如何在我的场景中解决这个异常.

这是我的代码:

MAIN CLASS :-

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new LsduJFrame().setVisible(true);

        }
    });
 new Thread(new DisplayPlazaNameLocation()).start();// i am calling here
}

DisplayPlazaNameLocation:-

public class DisplayPlazaNameLocation implements Runnable{
static String plazaNameLocation;
static Connection con;
int i =0;
public  void getPlazaNameLocation(){
    try {
        System.out.println("in getPlazaNameLocation()==============================================");
        PreparedStatement pst=con.prepareStatement("SELECT DISTINCT Plaza_Loc FROM lsdu_live");
        ResultSet rs = pst.executeQuery();
        while(rs.next()){
            plazaNameLocation = rs.getString("Plaza_Loc");
            //System.out.println(plazaNameLocation);
        }

        String ar[] = plazaNameLocation.split(",");
        jLabel199.setText("<html>"+ar[0]+"<br>"+ar[1]+"</html>");
        rs.close();
        pst.close();
        con.close();

    } catch (SQLException ex) {

        run();
     }
}
 @Override
public void run(){
    try {
        Class.forName(DB_DRIVER_CLASS);
        con= DriverManager.getConnection(DB_URL[0],DB_USERNAME,DB_PASSWORD);
       if(con != null){
        this.getPlazaNameLocation();}
    } catch (ClassNotFoundException ex) {
         run();
    } catch (SQLException ex) {
        run();
    }

}
Run Code Online (Sandbox Code Playgroud)

}

执行一段时间后,我得到了这个例外 Exception in thread "Thread-1" java.lang.StackOverflowError

假设我在获得此异常之前解决了Connection问题,应用程序正常工作,但在获得此异常后,在此时获取此异常后不再使用resolve我需要关闭应用程序并在此时再次打开它正在运行. How to resolve this issue with respect to my code ?

stackTrace() data:-

Exception in thread "Thread-1" java.lang.StackOverflowError
at java.lang.Exception.<init>(Exception.java:66)
at java.sql.SQLException.<init>(SQLException.java:70)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:435)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at lsdu_application.DisplayPlazaNameLocation.run(DisplayPlazaNameLocation.java:47)
at lsdu_application.DisplayPlazaNameLocation.run(DisplayPlazaNameLocation.java:59)
at 
Run Code Online (Sandbox Code Playgroud)

f1s*_*1sh 5

在你的run方法中,你打电话getPlazaNameLocation.如果SQL失败,则run再次调用.这会导致这两种方法之间出现无限的"循环"(递归),每次调用都会添加到调用堆栈中,最终会导致调用StackOverflowError.

  • 为什么run在SQLException的情况下调用?您应该处理catch块中的错误,而不是重新启动您的功能.删除该电话并获取有关您SQLException使用的信息e.printStackTrace().
  • 从不run手动打电话.应该使用调用Thread.start().

编辑:如果你真的想重新尝试数据库操作,以防它失败,试试这个:

public void getPlazaNameLocation() throws SQLException {
  //same code, but remove the try/catch blocks and
  // especially the run()!
}

@Override
public void run(){
  try {
    Class.forName(DB_DRIVER_CLASS);
    con = DriverManager.getConnection(DB_URL[0],DB_USERNAME,DB_PASSWORD);
  } catch (ClassNotFoundException ex) {
    e.printStackTrace();
    //fail here since no driver was found
    return;
  }

  boolean retry = true;
  while(retry){
    try {
      getPlazaNameLocation();
      retry = false;
    } catch (SQLException ex) {
      //if we arrive here, "retry" will still have a value of true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我不建议通过与数据库断开连接的垃圾邮件请求,就像这段代码一样.