示例#1
0
 /**
  * Init the Derby Datasource in JNDI.
  *
  * @param context the naming context
  * @param name the dabase name in the context
  * @param datasourceName the datasource Name
  * @param user
  * @param password
  * @return <code>true</code> if initialized, <code>false</code> otherwise
  */
 public static boolean initDerbyDatasource(
     final InitialContext context,
     final String name,
     final String datasourceName,
     final String user,
     final String password) {
   boolean init = true;
   try {
     context.createSubcontext("java:");
     context.createSubcontext("java:/comp");
     context.createSubcontext("java:/comp/env");
     context.createSubcontext("java:/comp/env/jdbc");
     // Construct DataSource
     final EmbeddedConnectionPoolDataSource40 datasource =
         new EmbeddedConnectionPoolDataSource40();
     datasource.setDatabaseName(datasourceName);
     datasource.setUser("user");
     datasource.setPassword("password");
     LOGGER.info("Datasource created");
     validateDerbyConnection(datasource);
     context.bind("java:/comp/env/jdbc/" + name, datasource);
   } catch (NamingException e) {
     LOGGER.error("Naming Exception", e);
     init = false;
   }
   return init;
 }
  /**
   * Get a connection pool data source for use by Java DB storage engines
   *
   * @param dbPath The path where the db will be located
   * @param memory whether to actually use a memory database
   * @return the {@link ConnectionPoolDataSource}
   */
  public static ConnectionPoolDataSource getDataSource(String dbPath, boolean memory) {

    EmbeddedConnectionPoolDataSource40 ds = new EmbeddedConnectionPoolDataSource40();
    if (memory) {
      ds.setDatabaseName("memory:SyncDB");
    } else {
      String path = "SyncDB";
      if (dbPath != null) {
        File f = new File(dbPath, "SyncDB");
        path = f.getAbsolutePath();
      }

      ds.setDatabaseName(path);
    }
    ds.setCreateDatabase("create");
    ds.setUser("floodlight");
    ds.setPassword("floodlight");
    return ds;
  }