public static DataSource createDataSource(String homeDir, String dbName) throws IOException { // Setup the Derby datasource. System.setProperty("derby.system.home", homeDir); System.setProperty("derby.storage.fileSyncTransactionLog", "true"); System.setProperty("derby.storage.pageCacheSize", "100"); final EmbeddedDataSource ds = new EmbeddedDataSource(); ds.setDatabaseName(dbName); ds.setCreateDatabase("create"); return ds; }
public static void shutdownDefaultDataSource(DataSource dataSource) { final EmbeddedDataSource ds = (EmbeddedDataSource) dataSource; ds.setCreateDatabase("shutdown"); ds.setShutdownDatabase("shutdown"); try { ds.getConnection(); } catch (SQLException expectedAndIgnored) { } }
@Override DataSource createDataSource() { EmbeddedDataSource dataSource = new EmbeddedDataSource(); dataSource.setUser(""); dataSource.setPassword(""); dataSource.setDatabaseName("dbtest"); dataSource.setCreateDatabase("create"); dataSource.setConnectionAttributes("upgrade=true"); return dataSource; } };
Connection getConnection() throws SQLException, DAOConnection { EmbeddedDataSource ds = new EmbeddedDataSource(); ds.setDatabaseName(dbUri); ds.setPassword(password); ds.setUser(username); Connection conn = null; try { conn = ds.getConnection(); } catch (SQLException e) { // check if cannot connect due to "Another instance of // Derby may have already booted the database". // The error code of this exception is 45000. if (e.getNextException().getErrorCode() == 45000) { throw new DAOConnection(e.getNextException().getMessage()); } throw new SQLException(e); } conn.setSchema(schema); return conn; }
return new TestDatabase(TEST_DATABASE); final EmbeddedDataSource ds = new EmbeddedDataSource(); ds.setDatabaseName("memory:" + name); ds.setDataSourceName("Apache SIS test database"); ds.setCreateDatabase("create"); return new TestDatabase(ds) { @Override public void close() throws SQLException {
public void ensureDatabaseExists() { Connection conn = null; try { conn = dataSource.getConnection(); } catch (SQLException e) { log.info("creating database... " + baseDir); dataSource.setCreateDatabase("create"); } finally { close(conn); } }
/** * Default constructor for a Derby Embedded Local DataSource. */ public EmbeddedLocalMCF() { super(new EmbeddedDataSource(), new NoExceptionsAreFatalSorter(), false); ds = (EmbeddedDataSource) super.dataSource; }
@Override public void close() throws SQLException { final EmbeddedDataSource ds = (EmbeddedDataSource) source; ds.setCreateDatabase("no"); ds.setConnectionAttributes("drop=true"); try { ds.getConnection().close(); } catch (SQLException e) { // This is the expected exception. if (!Initializer.isSuccessfulShutdown(e)) { throw 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()); } } } }
/** * Set the database name. * * @param name the database name */ public void setDatabaseName(String name) { ds.setDatabaseName(name); }
/** * Set whether the database should be created if it does not already exist. * * @param create TRUE if the database should be created */ public void setCreateDatabase(Boolean create) { ds.setCreateDatabase(create.booleanValue() ? "create" : null); }
/** * Set this property if one wishes to shutdown the database identified by databaseName. * * @param shutdown - if set to the string "shutdown", this data source will shutdown the * database if it is running. */ public void setShutdownDatabase(String shutDown) { ds.setShutdownDatabase(shutDown); }
public void ensureTablesExist() throws SQLException { for (TableSpec ts : getTableSpecs()) { try { execute("SELECT " + ts.getPrimaryKey() + " FROM " + ts.getTableName()); } catch (Exception e) { log.info("creating table... " + ts.getTableName()); execute(ts.getDdl()); } } // TODO: eventually use DatabaseMetaData to create proper table schema. Connection conn = null; try { conn = dataSource.getConnection(); DatabaseMetaData metadata = conn.getMetaData(); ResultSet results = metadata.getTables(null, null, "*", null); while (results.next()) { log.info("examining tables:" + results.getObject(0).toString()); } } catch (SQLException e) { log.error("Database should already exist."); throw e; } finally { close(conn); } }
/** * Set the user name used establish the connection. * This value is used if no connection information is supplied by the application * when attempting to create a connection. * * @param user the user name used to establish the connection; may be null */ public void setUserName(String user) { ds.setUser(user); }
/** * Set the user password credential establish the connection. * This value is used if no connection information is supplied by the application * when attempting to create a connection. * * @param password the password credential used to establish the connection; may be null */ public void setPassword(String password) { ds.setPassword(password); }
dataSource.setCreateDatabase("create"); connection = dataSource.getConnection(); connection.setAutoCommit(false); LOGGER.info("Could not determine datamodel's version, recreating.."); } finally { dataSource.setCreateDatabase("");
@Override public DataSource createDataSource(Properties props) throws SQLException { EmbeddedDataSource ds = new EmbeddedDataSource(); setProperties(ds, props); return ds; }
public void disconnect() { dataSource.setShutdownDatabase("shutdown"); }
@Override protected DataSource createInstance() throws Exception { File directory = new File(dataDirectory); System.setProperty("derby.system.home", directory.getCanonicalPath()); System.setProperty("derby.storage.fileSyncTransactionLog", "true"); System.setProperty("derby.storage.pageCacheSize", "100"); final EmbeddedDataSource ds = new EmbeddedDataSource(); ds.setDatabaseName("build/derbydb"); ds.setCreateDatabase("create"); return ds; }
public DatabaseUtil(Credential cred, String baseDir, String bootPassword) { this.baseDir = baseDir; dataSource = new EmbeddedDataSource(); dataSource.setUser(cred.getUsername()); dataSource.setPassword(cred.getPassword()); dataSource.setDatabaseName(baseDir); if (!bootPassword.equals(NOT_ENCRYPTED)) { String connAtts = "dataEncryption=true;bootPassword=" + bootPassword; dataSource.setConnectionAttributes(connAtts); } jdbcTemplate = new JdbcTemplate(dataSource); }