public DatabaseManager(ManagerDirector director) { this.director = director; final String path = new File(director.getContext().getLocalStorage().getDirectory(), "frd.odb") .getAbsolutePath(); logger.info("Database path " + path); factory = Persistence.createEntityManagerFactory(path); // hook to shutdown database on JVM close Runtime.getRuntime() .addShutdownHook( new Thread( new Runnable() { @Override public void run() { if (factory != null) { try { factory.close(); } catch (Exception e) { LogUtils.processException(logger, e); // ignore } } } })); }
public void runOnTask(final Runnable runnable, final Runnable succeeded) { final TaskService service = director.getTaskServiceManager().getTaskService(TaskServiceManager.DATABASE_SERVICE); service.execute( new CoreTask(director.getContext().getApplication()) { @Override protected void succeeded(Object result) { if (succeeded != null) { succeeded.run(); } } @Override protected Object doInBackground() throws Exception { runnable.run(); return null; } @Override protected void failed(Throwable cause) { LogUtils.processException(logger, cause); if (cause instanceof PersistenceException && cause.getMessage().contains("error 141")) { Swinger.showErrorDialog( director.getContext().getResourceMap(), "DatabaseManager.databaseIsAlreadyUsed", cause); } else super.failed(cause); } }); }