Ejemplo n.º 1
0
 @Bean
 public DataSource dataSource() {
   boolean hsql = StringUtils.containsIgnoreCase(hibernateDialect, "hsql");
   boolean mysql = StringUtils.containsIgnoreCase(hibernateDialect, "mysql");
   if (hsql) {
     EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
     builder.setType(EmbeddedDatabaseType.HSQL);
     builder.addScript(embeddedDatabaseSchemaSQL);
     builder.addScript(embeddedDatabaseDataSQL);
     return builder.build();
   } else if (mysql) {
     DriverManagerDataSource dataSource = new DriverManagerDataSource();
     dataSource.setDriverClassName("com.mysql.jdbc.Driver");
     dataSource.setUrl("jdbc:mysql://localhost:3306/aj");
     dataSource.setUsername(user);
     dataSource.setPassword(password);
     return dataSource;
   } else {
     throw new IllegalStateException();
   }
 }
Ejemplo n.º 2
0
  @Bean
  public DataSource dataSource() {
    DataSource dataSource = null;
    if (env.getProperty("spring.database.vendor").equals(Database.H2.toString())) {

      EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder();
      edb.setType(EmbeddedDatabaseType.H2);
      edb.addScript("classpath:db/h2/schema.sql");
      edb.addScript("classpath:db/h2/test-data.sql");
      EmbeddedDatabase embeddedDatabase = edb.build();

      dataSource = embeddedDatabase;

    } else if (env.getProperty("spring.database.vendor").equals(Database.POSTGRESQL.toString())) {

      // TODO Review all parameters for this class and update this setup as appropriate
      BasicDataSource basicDataSource = new BasicDataSource();
      basicDataSource.setDriverClassName(env.getProperty("db.jdbc.driverclass"));
      basicDataSource.setUrl(env.getProperty("db.jdbc.url"));
      basicDataSource.setUsername(env.getProperty("db.username"));
      basicDataSource.setPassword(env.getProperty("db.password"));
      // basicDataSource.setDefaultCatalog("ServerCommon");
      basicDataSource.setInitialSize(0);
      // basicDataSource.setMaxActive(this.dbConcurrentCallsMaxCalls + 8);  // This is for DBCB v1.4
      basicDataSource.setMaxTotal(10); // This is for DBCB v2.0 (API change)
      //		basicDataSource.setMaxIdle(this.dbConcurrentCallsMaxCalls / 2 + 8);
      basicDataSource.setMinIdle(0);
      // basicDataSource.setRemoveAbandoned(true);		// Can help to reduce chance of memory leaks //
      // This is for DBCB v1.4
      basicDataSource.setRemoveAbandonedTimeout(300); // this is the default (5 minutes)

      dataSource = basicDataSource;

    } else {
      logger.error("Unsupported database: {}", env.getProperty("spring.database.vendor"));
      throw new UnsupportedOperationException(
          "Unsupported database: " + env.getProperty("spring.database.vendor"));
    }
    return dataSource;
  }