Search in sources :

Example 11 with DatabaseConfig

use of com.zzg.mybatis.generator.model.DatabaseConfig in project mybatis-generator-gui by zouzg.

the class TabPaneController method testConnection.

@FXML
void testConnection() {
    DatabaseConfig config = extractConfigForUI();
    if (config == null) {
        return;
    }
    if (StringUtils.isAnyEmpty(config.getName(), config.getHost(), config.getPort(), config.getUsername(), config.getEncoding(), config.getDbType(), config.getSchema())) {
        AlertUtil.showWarnAlert("密码以外其他字段必填");
        return;
    }
    Session sshSession = DbUtil.getSSHSession(config);
    if (isOverssh && sshSession != null) {
        PictureProcessStateController pictureProcessState = new PictureProcessStateController();
        pictureProcessState.setDialogStage(getDialogStage());
        pictureProcessState.startPlay();
        // 如果不用异步,则视图会等方法返回才会显示
        Task task = new Task<Void>() {

            @Override
            protected Void call() throws Exception {
                DbUtil.engagePortForwarding(sshSession, config);
                DbUtil.getConnection(config);
                return null;
            }
        };
        task.setOnFailed(event -> {
            Throwable e = task.getException();
            logger.error("task Failed", e);
            if (e instanceof RuntimeException) {
                if (e.getMessage().equals("Address already in use: JVM_Bind")) {
                    tabControlBController.setLPortLabelText(config.getLport() + "已经被占用,请换其他端口");
                }
                // 端口转发一定不成功,导致数据库连接不上
                pictureProcessState.playFailState("连接失败:" + e.getMessage(), true);
                return;
            }
            if (e.getCause() instanceof EOFException) {
                pictureProcessState.playFailState("连接失败, 请检查数据库的主机名,并且检查端口和目标端口是否一致", true);
                // 端口转发已经成功,但是数据库连接不上,故需要释放连接
                DbUtil.shutdownPortForwarding(sshSession);
                return;
            }
            pictureProcessState.playFailState("连接失败:" + e.getMessage(), true);
            // 可能是端口转发已经成功,但是数据库连接不上,故需要释放连接
            DbUtil.shutdownPortForwarding(sshSession);
        });
        task.setOnSucceeded(event -> {
            try {
                pictureProcessState.playSuccessState("连接成功", true);
                DbUtil.shutdownPortForwarding(sshSession);
                tabControlBController.recoverNotice();
            } catch (Exception e) {
                logger.error("", e);
            }
        });
        new Thread(task).start();
    } else {
        try {
            DbUtil.getConnection(config);
            AlertUtil.showInfoAlert("连接成功");
        } catch (RuntimeException e) {
            logger.error("", e);
            AlertUtil.showWarnAlert("连接失败, " + e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            AlertUtil.showWarnAlert("连接失败");
        }
    }
}
Also used : Task(javafx.concurrent.Task) EOFException(java.io.EOFException) EOFException(java.io.EOFException) DatabaseConfig(com.zzg.mybatis.generator.model.DatabaseConfig) Session(com.jcraft.jsch.Session) FXML(javafx.fxml.FXML)

Aggregations

DatabaseConfig (com.zzg.mybatis.generator.model.DatabaseConfig)11 SQLRecoverableException (java.sql.SQLRecoverableException)3 FXML (javafx.fxml.FXML)3 ImageView (javafx.scene.image.ImageView)3 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 EOFException (java.io.EOFException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Task (javafx.concurrent.Task)1 MenuItem (javafx.scene.control.MenuItem)1 TextFieldTreeCell (javafx.scene.control.cell.TextFieldTreeCell)1 ColumnOverride (org.mybatis.generator.config.ColumnOverride)1