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("连接失败");
}
}
}
Aggregations