<commit_before>/*
 *
 *  *  Copyright 2010-2016 OrientDB LTD (http://orientdb.com)
 *  *
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *       http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS,
 *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  See the License for the specific language governing permissions and
 *  *  limitations under the License.
 *  *
 *  * For more information: http://orientdb.com
 *
 */
package com.orientechnologies.orient.core.db;

import com.orientechnologies.common.concur.resource.OResourcePool;
import com.orientechnologies.common.concur.resource.OResourcePoolListener;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.exception.OAcquireTimeoutException;

import static com.orientechnologies.orient.core.config.OGlobalConfiguration.DB_POOL_ACQUIRE_TIMEOUT;
import static com.orientechnologies.orient.core.config.OGlobalConfiguration.DB_POOL_MAX;

/**
 * Created by tglman on 07/07/16.
 */
public class ODatabasePoolImpl implements ODatabasePoolInternal {
  private final OResourcePool<Void, ODatabaseDocumentInternal> pool;
  private final OrientDBInternal                               factory;
  private final OrientDBConfig                                 config;

  public ODatabasePoolImpl(OrientDBInternal factory, String database, String user, String password, OrientDBConfig config) {
    int max = config.getConfigurations().getValueAsInteger(DB_POOL_MAX);
    // TODO use configured max
    pool = new OResourcePool(max, new OResourcePoolListener<Void, ODatabaseDocumentInternal>() {
      @Override
      public ODatabaseDocumentInternal createNewResource(Void iKey, Object... iAdditionalArgs) {
        return factory.poolOpen(database, user, password, ODatabasePoolImpl.this);
      }

      @Override
      public boolean reuseResource(Void iKey, Object[] iAdditionalArgs, ODatabaseDocumentInternal iValue) {
        if (iValue.getStorage().isClosed()) {
          return false;
        }
        iValue.reuse();
        return true;
      }
    });
    this.factory = factory;
    this.config = config;
  }

  @Override
  public synchronized ODatabaseSession acquire() throws OAcquireTimeoutException {
    return pool.getResource(null, config.getConfigurations().getValueAsLong(DB_POOL_ACQUIRE_TIMEOUT));
  }

  @Override
  public synchronized void close() {
    for (ODatabaseDocumentInternal res : pool.getAllResources()) {
      res.realClose();
    }
    pool.close();
    factory.removePool(this);
  }

  public synchronized void release(ODatabaseDocumentInternal database) {
    pool.returnResource(database);
  }

  public OrientDBConfig getConfig() {
    return config;
  }
}
<commit_msg>fixed issue with pool high concurrency when pool run out of resources<commit_after>/*
 *
 *  *  Copyright 2010-2016 OrientDB LTD (http://orientdb.com)
 *  *
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *       http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS,
 *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  See the License for the specific language governing permissions and
 *  *  limitations under the License.
 *  *
 *  * For more information: http://orientdb.com
 *
 */
package com.orientechnologies.orient.core.db;

import com.orientechnologies.common.concur.resource.OResourcePool;
import com.orientechnologies.common.concur.resource.OResourcePoolListener;
import com.orientechnologies.orient.core.exception.OAcquireTimeoutException;
import com.orientechnologies.orient.core.exception.ODatabaseException;

import static com.orientechnologies.orient.core.config.OGlobalConfiguration.DB_POOL_ACQUIRE_TIMEOUT;
import static com.orientechnologies.orient.core.config.OGlobalConfiguration.DB_POOL_MAX;

/**
 * Created by tglman on 07/07/16.
 */
public class ODatabasePoolImpl implements ODatabasePoolInternal {
  private volatile OResourcePool<Void, ODatabaseDocumentInternal> pool;
  private final    OrientDBInternal                               factory;
  private final    OrientDBConfig                                 config;

  public ODatabasePoolImpl(OrientDBInternal factory, String database, String user, String password, OrientDBConfig config) {
    int max = config.getConfigurations().getValueAsInteger(DB_POOL_MAX);
    // TODO use configured max
    pool = new OResourcePool(max, new OResourcePoolListener<Void, ODatabaseDocumentInternal>() {
      @Override
      public ODatabaseDocumentInternal createNewResource(Void iKey, Object... iAdditionalArgs) {
        return factory.poolOpen(database, user, password, ODatabasePoolImpl.this);
      }

      @Override
      public boolean reuseResource(Void iKey, Object[] iAdditionalArgs, ODatabaseDocumentInternal iValue) {
        if (iValue.getStorage().isClosed()) {
          return false;
        }
        iValue.reuse();
        return true;
      }
    });
    this.factory = factory;
    this.config = config;
  }

  @Override
  public ODatabaseSession acquire() throws OAcquireTimeoutException {
    OResourcePool<Void, ODatabaseDocumentInternal> p;
    synchronized (this) {
      p = pool;
    }
    if (p != null) {
      return p.getResource(null, config.getConfigurations().getValueAsLong(DB_POOL_ACQUIRE_TIMEOUT));
    } else {
      throw new ODatabaseException("The pool is closed");
    }
  }

  @Override
  public synchronized void close() {
    OResourcePool<Void, ODatabaseDocumentInternal> p;
    synchronized (this) {
      p = pool;
      pool = null;
    }
    if (p != null) {
      for (ODatabaseDocumentInternal res : p.getAllResources()) {
        res.realClose();
      }
      p.close();
      factory.removePool(this);
    }
  }

  public void release(ODatabaseDocumentInternal database) {
    OResourcePool<Void, ODatabaseDocumentInternal> p;
    synchronized (this) {
      p = pool;
    }
    if (p != null) {
      pool.returnResource(database);
    } else {
      throw new ODatabaseException("The pool is closed");
    }
  }

  public OrientDBConfig getConfig() {
    return config;
  }
}

{'fraction_non_alphanumeric': 0.0895880149812734, 'fraction_numerical': 0.005393258426966292, 'mean_word_length': 3.3892176199868507, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1697389', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1872, 'n_words': 487}