Beispiel #1
0
  public Connection connect(String url, Properties info) throws SQLException {
    if (acceptsURL(url)) {
      String fileName = getFileNameFromUrl(url);
      try {
        Configuration configuration = configLoader.load(fileName);

        List<String> drivers = configuration.getDrivers();
        for (String classname : drivers) {
          Class<? extends Driver> driverClass = (Class<? extends Driver>) Class.forName(classname);
          Driver driver = driverClass.newInstance();
          java.sql.DriverManager.registerDriver(driver);
          logger.debug("Registering driver " + classname);
        }

        List<ConnectionInfo> connections = configuration.getConnections();

        ConnectionsHolder connectionsHolder = new ConnectionsHolder();
        for (ConnectionInfo connectionInfo : connections) {
          Properties properties = new Properties(info);
          properties.setProperty("user", connectionInfo.getUser());
          properties.setProperty("password", connectionInfo.getPassword());

          Connection connection = DriverManager.getConnection(connectionInfo.getUrl(), properties);
          connectionsHolder.add(connectionInfo.getName(), connection);
        }
        return new ShardsConnection(connectionsHolder, configuration);
      } catch (Exception e) {
        throw new SQLException("Cannot load configuration file", e);
      }
    } else {
      return null;
    }
  }