This is as simple as I can make it.
public class Main extends Application
{
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/payrollDB - Copy0.odb");
public static EntityManager departmentDB = emf.createEntityManager();
public static EntityManager employeeDB = emf.createEntityManager();
public static EntityManager paygradeDB = emf.createEntityManager();
public static EntityManager programDB = emf.createEntityManager();
public static EntityManager supervisorDB = emf.createEntityManager();
public static EntityManager programImportDB = emf.createEntityManager();
public static EntityManager shiftImportDB = emf.createEntityManager();
private static Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
private static Stage dialogStage = new Stage();
private static Stage primaryStage;
private static BorderPane rootLayout;
@Override
public void start(Stage stage)
{
primaryStage = stage;
primaryStage.setResizable(false);
initRootLayout();
showMainMenu();
}
private void initRootLayout()
{
try
{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
}
catch (IOException e)
{e.printStackTrace();}
}
public static void showMainMenu()
{
try
{
if(dialogStage.isShowing())
dialogStage.close();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/MainMenu.fxml"));
GridPane mainMenu = (GridPane) loader.load();
rootLayout.setCenter(mainMenu);
primaryStage.setHeight(rootLayout.getMinHeight() + mainMenu.getMinHeight());
if(mainMenu.getMinWidth() > rootLayout.getMinWidth())
primaryStage.setWidth(mainMenu.getMinWidth());
else
primaryStage.setWidth(rootLayout.getMinWidth());
primaryStage.setX((screenBounds.getWidth() - primaryStage.getWidth()) / 2);
primaryStage.setY((screenBounds.getHeight() - primaryStage.getHeight()) / 2);
}
catch (IOException e)
{e.printStackTrace();}
}
public static void main(String[] args)
{
launch(args);
departmentDB.close();
employeeDB.close();
paygradeDB.close();
programDB.close();
supervisorDB.close();
programImportDB.close();
shiftImportDB.close();
emf.close();
}
}
I cannot/don't know how to remove external dependencies at this point since I use FXML files for all my UI.
The problem I'm describing can happen just by starting and stopping the program in quick succession or after running it for extended time, stopping the program, and modifying code for extended time and all variations in between. There is no way to isolate the issue directly as there is no consistent pattern or trigger. I can provide you the stack trace when it happens, but it's ultimately all related to the fact that this [filename].odb$ file exists for some reason (unknown to me) and doesn't always get removed when the application is terminated.
At no point do I use System.exit() nor do I use hard termination from my Eclipse IDE. Each time the application is terminated it is done so by using the 'X' on the window produced.