<commit_before>/*
 * #!
 * Ontopia Engine
 * #-
 * Copyright (C) 2001 - 2013 The Ontopia Project
 * #-
 * 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.
 * !#
 */

package net.ontopia.persistence.proxy;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import net.ontopia.utils.OntopiaRuntimeException;
import net.ontopia.utils.PropertyUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.dbcp.AbandonedConfig;
import org.apache.commons.dbcp.AbandonedObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
/** 
 * INTERNAL: Apache Commons DBCP connection factory implementation.
 */

public class DBCPConnectionFactory extends AbstractConnectionFactory {

  public static final String EXHAUSED_BLOCK = "block";
  public static final String EXHAUSED_GROW = "grow";
  public static final String EXHAUSED_FAIL = "fail";

  // configuration and defaul values
  public static final String ROOT = "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.";

  public static final String MIN_IDLE = ROOT + "MinimumSize";
  public static final int DEFAULT_MIN_IDLE = GenericObjectPool.DEFAULT_MIN_IDLE; // 0

  public static final String MAX_ACTIVE = ROOT + "MaximumSize";
  public static final int DEFAULT_MAX_ACTIVE = GenericObjectPool.DEFAULT_MAX_ACTIVE; // 8

  public static final String MAX_IDLE = ROOT + "MaximumIdle";
  public static final int DEFAULT_MAX_IDLE = GenericObjectPool.DEFAULT_MAX_IDLE; // 8

  public static final String USER_TIMEOUT = ROOT + "UserTimeout";
  public static final int DEFAULT_USER_TIMEOUT = -1; // never

  public static final String IDLE_TIMEOUT = ROOT + "IdleTimeout";
  public static final int DEFAULT_IDLE_TIMEOUT = -1; // never

  public static final String SOFT_MAXIMUM = ROOT + "SoftMaximum";
  public static final boolean DEFAULT_SOFT_MAXIMUM = true;

  public static final String EXHAUSTED_ACTION = ROOT + "WhenExhaustedAction";
  public static final String POOL_STATEMENTS = ROOT + "PoolStatements";
  public static final String VALIDATION_QUERY = ROOT + "ValidationQuery";

  // Define a logging category.
  private static final Logger log = LoggerFactory.getLogger(DBCPConnectionFactory.class.getName());

  protected AbandonedObjectPool pool;
  protected DataSource datasource;  
  protected TraceablePoolableConnectionFactory pcfactory;
  protected boolean readOnly;
  protected int defaultTransactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
  
  public DBCPConnectionFactory(Map<String, String> properties, boolean readOnly) {
    super(properties);
    this.readOnly = readOnly;
    // set up connection pool
    initPool();
  }
  
  protected void initPool() {
    // Read/Write by default
    log.debug("Creating new DBCP connection factory, readonly=" + readOnly);

    // Set up connection pool
    AbandonedConfig config = new AbandonedConfig();
    if (timeout > 0) {
      config.setRemoveAbandoned(true);
      config.setRemoveAbandonedTimeout(timeout);<commit_msg>Modified default connection pooling settings<commit_after>/*
 * #!
 * Ontopia Engine
 * #-
 * Copyright (C) 2001 - 2013 The Ontopia Project
 * #-
 * 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.
 * !#
 */

package net.ontopia.persistence.proxy;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import net.ontopia.utils.OntopiaRuntimeException;
import net.ontopia.utils.PropertyUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.dbcp.AbandonedConfig;
import org.apache.commons.dbcp.AbandonedObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
/** 
 * INTERNAL: Apache Commons DBCP connection factory implementation.
 */

public class DBCPConnectionFactory extends AbstractConnectionFactory {

  public static final String EXHAUSED_BLOCK = "block";
  public static final String EXHAUSED_GROW = "grow";
  public static final String EXHAUSED_FAIL = "fail";

  // configuration and defaul values
  public static final String ROOT = "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.";

  public static final String MIN_IDLE = ROOT + "MinimumSize";
  public static final int DEFAULT_MIN_IDLE = GenericObjectPool.DEFAULT_MIN_IDLE; // 0

  public static final String MAX_ACTIVE = ROOT + "MaximumSize";
  public static final int DEFAULT_MAX_ACTIVE = 50;

  public static final String MAX_IDLE = ROOT + "MaximumIdle";
  public static final int DEFAULT_MAX_IDLE = 20;

  public static final String USER_TIMEOUT = ROOT + "UserTimeout";
  public static final int DEFAULT_USER_TIMEOUT = 10000; // 10s

  public static final String IDLE_TIMEOUT = ROOT + "IdleTimeout";
  public static final int DEFAULT_IDLE_TIMEOUT = 5 * 60 * 1000; // 5m

  public static final String SOFT_MAXIMUM = ROOT + "SoftMaximum";
  public static final boolean DEFAULT_SOFT_MAXIMUM = false;

  public static final String EXHAUSTED_ACTION = ROOT + "WhenExhaustedAction";
  public static final String POOL_STATEMENTS = ROOT + "PoolStatements";
  public static final String VALIDATION_QUERY = ROOT + "ValidationQuery";

  // Define a logging category.
  private static final Logger log = LoggerFactory.getLogger(DBCPConnectionFactory.class.getName());

  protected AbandonedObjectPool pool;
  protected DataSource datasource;  
  protected TraceablePoolableConnectionFactory pcfactory;
  protected boolean readOnly;
  protected int defaultTransactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
  
  public DBCPConnectionFactory(Map<String, String> properties, boolean readOnly) {
    super(properties);
    this.readOnly = readOnly;
    // set up connection pool
    initPool();
  }
  
  protected void initPool() {
    // Read/Write by default
    log.debug("Creating new DBCP connection factory, readonly=" + readOnly);

    // Set up connection pool
    AbandonedConfig config = new AbandonedConfig();
    if (timeout > 0) {
      config.setRemoveAbandoned(true);
      config.setRemoveAbandonedTimeout(timeout);
{'fraction_non_alphanumeric': 0.08200455580865604, 'fraction_numerical': 0.0070734923870039565, 'mean_word_length': 4.717614804660727, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3144833', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2338, 'n_words': 703}