Esempio n. 1
1
  public static Connection getConnection() {

    Iterator<ConnectionWrapper> poolIter = connectionPool.iterator();
    while (poolIter.hasNext()) {
      ConnectionWrapper conn = poolIter.next();
      try {
        if (conn == null || conn.isReallyClosed()) {
          poolIter.remove();
        } else if (!conn.isClosed()) {
          return conn;
        }
      } catch (SQLException e) {
        log.error("Bad connection", e);
        poolIter.remove();
      }
    }

    // No valid connections
    ConnectionWrapper conn = createConnection();
    if (conn != null) {
      connectionPool.add(conn);
      log.info("Connection pool size: " + connectionPool.size());
    }
    return conn;
  }
Esempio n. 2
0
  public static int close() {
    int count = 0;

    Iterator iterator = m_notUsedConnection.iterator();
    while (iterator.hasNext()) {
      try {
        ((ConnectionWrapper) iterator.next()).close();
        count++;
      } catch (Exception e) {
      }
    }
    m_notUsedConnection.clear();

    iterator = m_usedUsedConnection.iterator();
    while (iterator.hasNext()) {
      try {
        ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next();
        wrapper.close();
        if (DEBUG) {
          wrapper.debugInfo.printStackTrace();
        }
        count++;
      } catch (Exception e) {
      }
    }
    m_usedUsedConnection.clear();

    return count;
  }
Esempio n. 3
0
  public static void shutdown() {
    for (ConnectionWrapper conn : connectionPool) {
      if (conn != null) {
        try {
          conn.reallyClose();
        } catch (SQLException e) {

        }
      }
    }
    connectionPool.clear();
  }
Esempio n. 4
0
  public static synchronized Connection getConnection() {
    clearClosedConnection();
    while (m_notUsedConnection.size() > 0) {
      try {
        ConnectionWrapper wrapper = (ConnectionWrapper) m_notUsedConnection.removeFirst();
        if (wrapper.connection.isClosed()) {
          continue;
        }
        m_usedUsedConnection.add(wrapper);
        if (DEBUG) {
          wrapper.debugInfo = new Throwable("Connection initial statement");
        }
        return wrapper.connection;
      } catch (Exception e) {
      }
    }
    int newCount = getIncreasingConnectionCount();
    LinkedList list = new LinkedList();
    ConnectionWrapper wrapper = null;
    for (int i = 0; i < newCount; i++) {
      wrapper = getNewConnection();
      if (wrapper != null) {
        list.add(wrapper);
      }
    }
    if (list.size() == 0) {
      return null;
    }
    wrapper = (ConnectionWrapper) list.removeFirst();
    m_usedUsedConnection.add(wrapper);

    m_notUsedConnection.addAll(list);
    list.clear();

    return wrapper.connection;
  }