Пример #1
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");
  }
Пример #2
0
 /**
  * Borrow a new connection.
  *
  * @return
  * @throws SQLException
  */
 public Connection getConnection() throws SQLException {
   connCount++;
   if (connCount == Long.MAX_VALUE) {
     connCount = Long.MIN_VALUE;
   }
   Connection conn = null;
   if (conConfig.isPooled()) {
     conn = ds.getConnection();
   } else {
     // Open connection directly
     conn =
         DriverManager.getConnection(
             conConfig.getUrl(), conConfig.getUsername(), conConfig.getPassword());
   }
   if (conConfig.isTraced()) {
     TracedConnection tc = new TracedConnection(connCount, this, conn);
     conn = tc;
     connectionList.add(tc);
   }
   return conn;
 }