BSAF停止打开数据库连接

时间:2013-10-29 08:23:17

标签: java database swing connection saf

使用Better Swing Application Framework(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);指令被卡住并且不会引发异常。

编辑:我刚刚发现,如果我在发布之前碰巧打开了一个连接,我可以在以后正确打开它们。

编辑2 :添加了更具说明性的示例代码。

编辑3 :澄清了可能原因的信息

1 个答案:

答案 0 :(得分:1)

看起来您的连接阻止了事件派发线程。您应该在SwingWorker等其他线程中处理它。

修改:我不确定为什么Task/SwingWorker无效,但您可以查看BASF上的forum topic