<commit_before>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */
package org.biojava.utils;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DataSourceConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

/**
* Returns a DataSource that implements connection pooling
*
* Uses Jakarta Commons DBCP and Pool packages.
* See the description of the dbcp package at 
* http://jakarta.apache.org/commons/dbcp/api/overview-summary.html#overview_description
*
* @author <NAME>
* @author <NAME>
*/

public class JDBCPooledDataSource {

  public static DataSource getDataSource(final String driver, 
                                         final String url,
                                         final String user,
                                         final String pass)
    throws Exception {
    
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(url);
    ds.setDriverClassName(driver);
    ds.setUsername(user);
    ds.setPassword(<PASSWORD>);
    // Set BasicDataSource properties such as maxActive and maxIdle, as described in
    // http://jakarta.apache.org/commons/dbcp/api/org/apache/commons/dbcp/BasicDataSource.html
    ds.setMaxActive(10);
    ds.setMaxIdle(5);
    ds.setMaxWait(10000);
  
    // Create a PoolableDataSource as described in http://jakarta.apache.org/commons/dbcp/api/overview-summary.html#overview_description
    ObjectPool connectionPool = new GenericObjectPool(null);
    ConnectionFactory connectionFactory = new DataSourceConnectionFactory(ds); 	 
    PoolableObjectFactory poolableConnectionFactory = new 	 
       PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); 	 
    PoolingDataSource dataSource = new MyPoolingDataSource(connectionPool, url + user);

    return dataSource;
  }


  // Adds simple equals and hashcode methods so that we can compare if
  // two connections are to the same database. This will fail if the
  // DataSource is redirected to another database etc (I doubt this is
  // ever likely to be used).
  static class MyPoolingDataSource extends PoolingDataSource {
    final String source;
    public MyPoolingDataSource(ObjectPool connectionPool, String source) {
      super(connectionPool);
      this.source = source;
    }
    public boolean equals(Object o2) {
      if ((o2 == null) || !(o2 instanceof MyPoolingDataSource)) {
        return false;
      }
      MyPoolingDataSource b2 = (MyPoolingDataSource) o2;
      return source.equals(b2.source);
    }
    public int hashCode() {
      return source.hashCode();
    }
  }


  public static void main(String[] args) {
    try {
      DataSource ds1 = getDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:/tmp/hsqldb/biosql", "sa", "");
      DataSource ds2 = getDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:/tmp/hsqldb/biosql", "sa", "");
      System.err.println(ds1);
      System.err.println(ds2);
      System.err.println(ds1.equals(ds2));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
<commit_msg>Patched as per <NAME>' suggestions on biojava-dev.<commit_after>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */
package org.biojava.utils;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.ObjectPool;

/**
* Returns a DataSource that implements connection pooling
*
* Uses Jakarta Commons DBCP and Pool packages.
* See the description of the dbcp package at 
* http://jakarta.apache.org/commons/dbcp/api/overview-summary.html#overview_description
*
* @author <NAME>
* @author <NAME>
* @author <NAME>
* @author <NAME>
*/

public class JDBCPooledDataSource {

  public static DataSource getDataSource(final String driver, 
                                         final String url,
                                         final String user,
                                         final String pass)
    throws Exception {
    
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(url);
    ds.setDriverClassName(driver);
    ds.setUsername(user);
    ds.setPassword(<PASSWORD>);
    // Set BasicDataSource properties such as maxActive and maxIdle, as described in
    // http://jakarta.apache.org/commons/dbcp/api/org/apache/commons/dbcp/BasicDataSource.html
    ds.setMaxActive(10);
    ds.setMaxIdle(5);
    ds.setMaxWait(10000);

    return ds;
  }


  // Adds simple equals and hashcode methods so that we can compare if
  // two connections are to the same database. This will fail if the
  // DataSource is redirected to another database etc (I doubt this is
  // ever likely to be used).
  /**
   * @depercated This is no longer used in favor of {@link BasicDataSource}
   * from DBCP
   */
  static class MyPoolingDataSource extends PoolingDataSource {
    final String source;
    public MyPoolingDataSource(ObjectPool connectionPool, String source) {
      super(connectionPool);
      this.source = source;
    }
    public boolean equals(Object o2) {
      if ((o2 == null) || !(o2 instanceof MyPoolingDataSource)) {
        return false;
      }
      MyPoolingDataSource b2 = (MyPoolingDataSource) o2;
      return source.equals(b2.source);
    }
    public int hashCode() {
      return source.hashCode();
    }
  }


  public static void main(String[] args) {
    try {
      DataSource ds1 = getDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:/tmp/hsqldb/biosql", "sa", "");
      DataSource ds2 = getDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:/tmp/hsqldb/biosql", "sa", "");
      System.err.println(ds1);
      System.err.println(ds2);
      System.err.println(ds1.equals(ds2));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

{'fraction_non_alphanumeric': 0.09781083356721863, 'fraction_numerical': 0.005613247263541959, 'mean_word_length': 2.966054535336672, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3393394', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2012, 'n_words': 591}