簡體   English   中英

BSAF停止打開數據庫連接

[英]BSAF stalls opening a DB connection

如果我曾經嘗試在程序啟動后打開與數據庫的連接,則會使用Better Swing應用程序框架(BSAF)運行Java應用程序。 如果我恰巧在啟動之前運行連接,則它可以正常工作。 在我的情況下,沒有解決方法,因為我要求用戶主動打開和關閉連接。

以下代碼是發生的事的典范

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.EventObject;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.Task;

public class BsafConnection extends SingleFrameApplication {

    private static final String DB_URL = ""; // A proper URL
    private static final String DRIVER = ""; // A proper driver
    private static Connection CONNECTION;

    public BsafConnection() {
        super();

        addExitListener(new ExitListener() {
            @Override
            public boolean canExit(EventObject event) {
                return true;
            }
            @Override
            public void willExit(EventObject event) {
                if (CONNECTION != null) {
                    try {
                        CONNECTION.close();
                        System.out.println("Connection closed");
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("Connection was not open");
                }
            }});
    }

    @Override
    protected void startup() {
    }

    @Override
    public void ready() {
        JPanel panel = new JPanel();
        panel.add(new JButton(getContext().getActionMap().get("connect")));
        panel.add(new JButton("Press here to check that the EDT is not blocked"));
        show(panel);
    }

    @Action
    public Task<?, ?> connect() {
        return new Task<Object, Object> (this) {
            @Override
            protected Object doInBackground() throws Exception {
                javax.swing.Action action = getContext().getActionMap().get("connect");
                action.putValue(javax.swing.Action.NAME, "Connecting...");
                openConnection(); // Placing the connection here makes the application stall

                action.putValue(javax.swing.Action.NAME, "Connected!");
                return null;
            }};
    }

    public static void openConnection() {
        try {
            Class.forName(DRIVER);

            CONNECTION = DriverManager.getConnection(DB_URL); // This instruction stalls when called after launch()
            System.out.println("Connection open");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String... args) {
//        openConnection(); // Here, the connection would work but is not the desired place 
        launch(BsafConnection.class, args);
    }

}

我正在Windows環境下工作,如果那可能是某種問題。 恐怕這可能與BSAF使用EDT運行應用程序的方式或與ClassLoader 只是要澄清一下:阻止EDT並不是問題,它可以正常工作,問題是DriverManager.getConnection(DB_URL);指令DriverManager.getConnection(DB_URL); 被卡住,不會拋出異常。

編輯 :我剛剛發現,如果我碰巧在啟動之前打開連接,則可以稍后正確打開它們。

編輯2 :添加了更具說明性的示例代碼。

編輯3 :澄清有關可能原因的信息

看來您的連接正在阻止事件分發線程。 您應該在另一個線程(如SwingWorker處理它。

編輯:我不確定為什么Task/SwingWorker無法正常工作,但是您可能會在BASF 論壇中找到有關該主題的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM