001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.utils;
022
023import javax.sql.DataSource;
024
025import org.apache.commons.dbcp.BasicDataSource;
026import org.apache.commons.dbcp.PoolingDataSource;
027import org.apache.commons.pool.ObjectPool;
028
029/**
030* Returns a DataSource that implements connection pooling
031*
032* Uses Jakarta Commons DBCP and Pool packages.
033* See the description of the dbcp package at 
034* http://jakarta.apache.org/commons/dbcp/api/overview-summary.html#overview_description
035*
036* @author Simon Foote
037* @author Len Trigg
038* @author Andy Yates
039* @author Thomas Down
040*/
041
042public class JDBCPooledDataSource {
043
044  public static DataSource getDataSource(final String driver, 
045                                         final String url,
046                                         final String user,
047                                         final String pass)
048    throws Exception {
049    
050    BasicDataSource ds = new BasicDataSource();
051    ds.setUrl(url);
052    ds.setDriverClassName(driver);
053    ds.setUsername(user);
054    ds.setPassword(pass);
055    // Set BasicDataSource properties such as maxActive and maxIdle, as described in
056    // http://jakarta.apache.org/commons/dbcp/api/org/apache/commons/dbcp/BasicDataSource.html
057    ds.setMaxActive(10);
058    ds.setMaxIdle(5);
059    ds.setMaxWait(10000);
060
061    return ds;
062  }
063
064
065  // Adds simple equals and hashcode methods so that we can compare if
066  // two connections are to the same database. This will fail if the
067  // DataSource is redirected to another database etc (I doubt this is
068  // ever likely to be used).
069  /**
070   * @depercated This is no longer used in favor of {@link BasicDataSource}
071   * from DBCP
072   */
073  static class MyPoolingDataSource extends PoolingDataSource {
074    final String source;
075    public MyPoolingDataSource(ObjectPool connectionPool, String source) {
076      super(connectionPool);
077      this.source = source;
078    }
079    public boolean equals(Object o2) {
080      if ((o2 == null) || !(o2 instanceof MyPoolingDataSource)) {
081        return false;
082      }
083      MyPoolingDataSource b2 = (MyPoolingDataSource) o2;
084      return source.equals(b2.source);
085    }
086    public int hashCode() {
087      return source.hashCode();
088    }
089  }
090
091
092  public static void main(String[] args) {
093    try {
094      DataSource ds1 = getDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:/tmp/hsqldb/biosql", "sa", "");
095      DataSource ds2 = getDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:/tmp/hsqldb/biosql", "sa", "");
096      System.err.println(ds1);
097      System.err.println(ds2);
098      System.err.println(ds1.equals(ds2));
099    } catch (Exception e) {
100      e.printStackTrace();
101    }
102  }
103}