public static void initEmbeddedDataSource(String dbUrl) { eds = new EmbeddedDataSource(); eds.setDatabaseName(dbUrl); eds.setUser(Constants.DERBY_DATABASE.STRING_DB_USER); eds.setPassword(Constants.DERBY_DATABASE.STRING_DB_PASSWORD); eds.setConnectionAttributes("create=true"); logger.log(Level.INFO, eds.getDatabaseName()); }
private WrappedDataSource createLocal() { localDataSource = (DataSource) System.getProperties().get(LOCAL_DB_NAME); if (localDataSource == null) { localDataSource = new EmbeddedDataSource(); ((EmbeddedDataSource) localDataSource).setDatabaseName(LOCAL_DB_NAME); ((EmbeddedDataSource) localDataSource).setCreateDatabase(LOCAL_DB_ACTION); System.getProperties().put(LOCAL_DB_NAME, localDataSource); } logger.warn(EMBEDDED_DATA_SOURCE_IS_USED); WrappedDataSource wrappedDataSource = new WrappedDataSource(localDataSource); return wrappedDataSource; }
/** get a connection */ public static Connection getConnection() throws Exception { if (tranConnection.get() != null) { return tranConnection.get(); } else { return eds.getConnection(); } }
@Override protected void shutdownDb() throws SQLException { // annoyingly, shutting down a derby instance involves catching an exception // and checking error codes to make sure it shut down "normally" try { EmbeddedDataSource dataSource = new EmbeddedDataSource(); dataSource.setDatabaseName(dbDir.getAbsolutePath()); dataSource.setShutdownDatabase("shutdown"); dataSource.getConnection(); } catch (SQLException e) { // make sure we get the correct error codes if (e.getErrorCode() != 45000 || !"08006".equals(e.getSQLState())) { throw e; } } }
/** Starts the server. */ @CommandArgument public void start() { System.out.println("Starting server"); try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); EmbeddedDataSource ds = new EmbeddedDataSource(); ds.setConnectionAttributes("create=true"); ds.setDatabaseName("classes"); logger.info("Initializing jobProgressStates monitor"); ProgressStateFactory factory = new ProgressStateFactory(); jobProgressStates = factory.getProgressStates(); logger.info("Initializing folders..."); Preferences pref = Preferences.userNodeForPackage(JobServer.class); String path = pref.get("rootDirectory", "./server"); File rootDirectory = new File(path); File jobsDirectory = new File(rootDirectory, "jobs"); rootDirectory.mkdir(); jobsDirectory.mkdir(); logger.info("Initializing service"); DbClassManager classManager = new DbClassManager(ds); classManager.prepareDataSource(); TaskScheduler scheduler = new PrioritySerialTaskScheduler(); Executor executor = Executors.newCachedThreadPool(); jobServer = new JobServer(jobsDirectory, factory, scheduler, classManager, executor); AuthenticationServer authServer = new AuthenticationServer(jobServer, JdcpUtil.DEFAULT_PORT); logger.info("Binding service"); Registry registry = getRegistry(); registry.bind("AuthenticationService", authServer); logger.info("Server ready"); System.out.println("Server started"); } catch (Exception e) { System.err.println("Failed to start server"); logger.error("Failed to start server", e); } }
@Override public void destroy() throws Exception { logger.info("Attempting Derby database shut down on: " + dataSource); if (!isShutdown && dataSource != null && dataSource instanceof EmbeddedDataSource) { EmbeddedDataSource ds = (EmbeddedDataSource) dataSource; try { ds.setShutdownDatabase("shutdown"); ds.getConnection(); } catch (SQLException except) { if (except.getSQLState().equals("08006")) { // SQLState derby throws when shutting down the database logger.info("Derby database is now shut down."); isShutdown = true; } else { logger.error("Problem shutting down Derby " + except.getMessage()); } } } }