【问题标题】:Java Waiting for Mysql connectionJava等待Mysql连接
【发布时间】:2016-10-23 13:24:03
【问题描述】:

我的应用程序使用了一个Mysql连接,它是使用这段代码获得的:

public static void Connect(){
    try
    {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String url = "jdbc:mysql://" + host + ":" + port + "/"+ db;
        ct = DriverManager.getConnection(url, user, pass);
        st = ct.createStatement();
        System.out.println("Csatlakozva....\n");
    }   
    catch (InstantiationException e) 
    {
        Main.textArea.setText("Error : Instantiation!");
        //System.err.println("Error : Instantiation");
        e.printStackTrace();
    }
    catch (IllegalAccessException e) 
    {
        Main.textArea.setText("Error : Illegális Behatolás!");
        //System.err.println("Error : Illegális Behatolás!");
        e.printStackTrace();
    } 
    catch (ClassNotFoundException e)
    {
        Main.textArea.setText("Error : Class Nem Található!");
        //System.err.println("Error : Class Nem Található!");
        e.printStackTrace();
    } 
    catch (SQLException e) 
    {
        Main.textArea.setText("Error : Adatbázis Nem Található!");
        //System.err.println("Error : Adatbázis Nem Található!");
        e.printStackTrace();
    }
}

如果 MySQL 数据库服务器未运行,因此我的应用无法打开连接,如何让我的应用等待连接建立?

【问题讨论】:

  • 您可以在代码中添加注释以显示等待的位置吗?这是本地还是远程 MySQL 实例?你用的是什么操作系统?
  • 那么,您的确切问题是什么?你想发生什么?
  • @Michael Markidis 我想要做的是......有我的连接代码......我想要如果mysql没有运行所以我的应用程序无法打开连接,我的应用程序必须等待连接做某事。

标签: java mysql swing connection wait


【解决方案1】:

为了让您的Connect 方法在实际获得连接之前不会返回,您需要用循环包围现有的try 块。像这样的:

public static void Connect() throws InterruptedException { // It isn't nice to block forever, so we will allow for interrupt
    for (;;) {
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://" + host + ":" + port + "/"+ db;
            ct = DriverManager.getConnection(url, user, pass);
            st = ct.createStatement();
            System.out.println("Csatlakozva....\n");
            return; // Break out of loop because we got a connection - no exception was thrown
         }   ...

         // One of the exceptions happened. We will continue to loop so
         // we try to connect again until we are successful. We don't want
         // to retry too fast, because painful experience has taught us that
         // bad things can happen (full logs, 100% CPU, etc.).
         Thread.sleep(1);
    }
}

探索和理解这些低级问题是一个很好的练习。但是,我将在此处说明(根据我的经验)大多数应用程序使用连接池来管理数据库连接。一般来说,连接池会提供阻塞的功能(通常是在有限的时间内),直到可以获得连接。换句话说,连接池不仅允许重用之前创建的连接,还可以在必要时处理连接重试。

【讨论】:

    【解决方案2】:

    我猜你有线程问题。

    你可以把 JDBC 连接代码放到新线程中

    private class JDBCConnection extends Thread{
    
    public void run(){
    ....
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 2020-08-24
      • 2018-07-01
      • 1970-01-01
      • 2020-11-21
      • 2015-11-16
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多