Exemplo n.º 1
0
  public ConnectionHandler(ConnectionConfig conConfig) {
    this.conConfig = conConfig;

    if (conConfig.isPooled()) {
      initializeConnectionPool();
    } else {
      try {
        Class.forName(conConfig.getDriver());
      } catch (ClassNotFoundException e) {
        throw new IllegalStateException(
            "The configured driver '" + conConfig.getDriver() + "' can not be found.");
      }
    }
  }
Exemplo n.º 2
0
  private void initializeConnectionPool() {
    try {
      log.info("Initializing connection " + conConfig.getId());

      // DBCP properties used to create the BasicDataSource
      Properties dbcpProperties = new Properties();

      // DriverClass & url
      dbcpProperties.put("driverClassName", conConfig.getDriver());
      dbcpProperties.put("url", conConfig.getUrl());

      // Username / password
      dbcpProperties.put("username", conConfig.getUsername());
      dbcpProperties.put("password", conConfig.getPassword());

      // Pool size
      dbcpProperties.put("maxActive", Integer.toString(conConfig.getPoolSize()));

      // Let the factory create the pool
      ds = (BasicDataSource) BasicDataSourceFactory.createDataSource(dbcpProperties);

      ds.setMaxWait(conConfig.getMaxWait()); // wait max before throwing an exception.
      ds.setTestOnBorrow(true);
      ds.setValidationQuery(conConfig.getValidationQuery());

      if (!conConfig.isLazyInit()) {
        // The BasicDataSource has lazy initialization
        // borrowing a connection will start the DataSource
        // and make sure it is configured correctly.
        Connection conn = ds.getConnection();
        conn.close();
      }

    } catch (Throwable e) {
      String message = "Unable to create DHCP pool... ";
      log.fatal(message, e);
      if (ds != null) {
        try {
          ds.close();
        } catch (Exception e2) {
          // ignore
        }
        ds = null;
      }
    }
    log.info("Configure ConnectionHandler '" + conConfig.getId() + "' complete");
  }