Android Open Source - DKO Mirrored Data Source






From Project

Back to project page DKO.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project DKO listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.kered.dko.datasource;
/*  ww  w .ja v a2 s.  c  o  m*/
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import java.util.logging.Logger;

import javax.sql.DataSource;


/**
 * This class wraps other {@code javax.sql.DataSource} instances.  It usually passes
 * through calls to the primary, but if {@code getMirroredConnection()}
 * is called a random mirror is used instead.
 *
 * @author Derek Anderson
 */
public class MirroredDataSource implements MatryoshkaDataSource {

  private final DataSource primary;
  private DataSource[] mirrors;
  private static final Logger log = Logger.getLogger("org.kered.dko.datasource.MirroredDataSource");

  /**
     * This class usually passes
     * through calls to the primary, but if {@code getMirroredConnection()}
     * is called a random mirror is used instead.
   * @param primary
   * @param mirrors
   */
  public MirroredDataSource(final DataSource primary, final DataSource... mirrors) {
    this.primary = primary;
    if (mirrors == null) this.mirrors = new DataSource[0];
    else this.mirrors = mirrors;
  }

  @Override
  public PrintWriter getLogWriter() throws SQLException {
    return primary.getLogWriter();
  }

  @Override
  public int getLoginTimeout() throws SQLException {
    return primary.getLoginTimeout();
  }

  @Override
  public void setLogWriter(final PrintWriter arg0) throws SQLException {
    primary.setLogWriter(arg0);
  }

  @Override
  public void setLoginTimeout(final int arg0) throws SQLException {
    primary.setLoginTimeout(arg0);
  }

  @Override
  public boolean isWrapperFor(final Class<?> arg0) throws SQLException {
    if (primary.getClass().equals(arg0)) return true;
    return primary.isWrapperFor(arg0);
  }

  @SuppressWarnings("unchecked")
  @Override
  public <T> T unwrap(final Class<T> arg0) throws SQLException {
    if (primary.getClass().equals(arg0)) return (T) primary;
    return primary.unwrap(arg0);
  }

  @Override
  public Connection getConnection() throws SQLException {
    return primary.getConnection();
  }

  /**
   * Returns a connection from a randomly selected mirror. &nbsp;
   * Feel free to override this method to implement other
   * load balancing stratigies in your own code.
   * @return
   * @throws SQLException
   */
  public Connection getMirroredConnection() throws SQLException {
    // for now we randomly select the mirror
    // TODO: implement other strategies
    if (mirrors.length == 0) return getConnection();
    final Random random = new Random();
    final int i = random.nextInt(mirrors.length);
    for (int j=0; j<mirrors.length; ++j) {
      final DataSource mirror = mirrors[(i+j)%mirrors.length];
      try {
        return mirror.getConnection();
      } catch (final SQLException e) {
        // db down - try another
        log.warning("could not connect to "+ mirror +": "+ e.toString());
      }
    }
    try {
      return primary.getConnection();
    } catch (final SQLException e) {
      log.warning("could not connect to "+ primary +": "+ e.toString());
    }
    throw new SQLException("could not connect to any mirror: "
        + Arrays.asList(mirrors) +" or the primary: "+ primary);
  }

  @Override
  public Connection getConnection(final String arg0, final String arg1)
      throws SQLException {
    return primary.getConnection(arg0, arg1);
  }

  @Override
  public String toString() {
    return "[MirroredDataSource primary="+ primary +" mirrors="
        + Util.join(",", mirrors) +"]";
  }

  public DataSource getPrimaryDataSource() {
    return primary;
  }

  //@Override
  public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    throw new SQLFeatureNotSupportedException();
  }

  @Override
  public DataSource getPrimaryUnderlying() {
    return primary;
  }

  @Override
  public Collection<DataSource> getAllUnderlying() {
    final Collection<DataSource> ret = new ArrayList<DataSource>();
    ret.add(primary);
    for (final DataSource mirror : mirrors) ret.add(mirror);
    return ret;
  }
}




Java Source Code List

.HelloWorld.java
org.kered.contactlensfinder.DB.java
org.kered.contactlensfinder.MainActivity.java
org.kered.contactlensfinder.ViewManufacturersActivity.java
org.kered.contactlensfinder.ViewProductsActivity.java
org.kered.contactlensfinder.ViewPropertiesActivity.java
org.kered.dko.AbstractQuery.java
org.kered.dko.Bulk.java
org.kered.dko.CSV.java
org.kered.dko.ClosableIterator.java
org.kered.dko.Condition.java
org.kered.dko.Constants.java
org.kered.dko.Context.java
org.kered.dko.DBQuery.java
org.kered.dko.DBRowIterator.java
org.kered.dko.Diff.java
org.kered.dko.DualIterator.java
org.kered.dko.Expression.java
org.kered.dko.Field.java
org.kered.dko.FilteringQuery.java
org.kered.dko.Function.java
org.kered.dko.InMemoryQuery.java
org.kered.dko.Join.java
org.kered.dko.LazyCacheIterable.java
org.kered.dko.LocalJoin.java
org.kered.dko.M.java
org.kered.dko.Main.java
org.kered.dko.MatryoshkaQuery.java
org.kered.dko.PeekableClosableIterator.java
org.kered.dko.PeekableIterator.java
org.kered.dko.QueryAddField.java
org.kered.dko.QueryFactory.java
org.kered.dko.QuerySnapshot.java
org.kered.dko.Query.java
org.kered.dko.SQLFunction.java
org.kered.dko.SelectAsMapIterable.java
org.kered.dko.SelectFromOAI.java
org.kered.dko.SelectSingleColumn.java
org.kered.dko.SoftJoinUtil.java
org.kered.dko.SoftJoin.java
org.kered.dko.SqlContext.java
org.kered.dko.Statistics.java
org.kered.dko.SubQueryField.java
org.kered.dko.TableInfo.java
org.kered.dko.TableWrapper.java
org.kered.dko.Table.java
org.kered.dko.TemporaryTableFactory.java
org.kered.dko.TmpTableBuilder.java
org.kered.dko.Tuple.java
org.kered.dko.UsageMonitor.java
org.kered.dko.UsageStats.java
org.kered.dko.Util.java
org.kered.dko.ant.ClassGenerator.java
org.kered.dko.ant.CodeGeneratorBase.java
org.kered.dko.ant.CodeGenerator.java
org.kered.dko.ant.DataSourceGenerator.java
org.kered.dko.ant.GsonGenerator.java
org.kered.dko.ant.JoinGenerator.java
org.kered.dko.ant.Main.java
org.kered.dko.ant.SchemaExtractorBase.java
org.kered.dko.ant.SchemaExtractor.java
org.kered.dko.ant.Util.java
org.kered.dko.datasource.CheapConnectionPoolingDataSource.java
org.kered.dko.datasource.ConnectionCountingDataSource.java
org.kered.dko.datasource.JDBCDriverDataSource.java
org.kered.dko.datasource.MatryoshkaDataSource.java
org.kered.dko.datasource.MirroredDataSource.java
org.kered.dko.datasource.ReflectedDataSource.java
org.kered.dko.datasource.SingleConnectionDataSource.java
org.kered.dko.datasource.SingleThreadedDataSource.java
org.kered.dko.datasource.UnClosableConnection.java
org.kered.dko.datasource.Util.java
org.kered.dko.json.CDL.java
org.kered.dko.json.CookieList.java
org.kered.dko.json.Cookie.java
org.kered.dko.json.HTTPTokener.java
org.kered.dko.json.HTTP.java
org.kered.dko.json.JSONArray.java
org.kered.dko.json.JSONException.java
org.kered.dko.json.JSONML.java
org.kered.dko.json.JSONObject.java
org.kered.dko.json.JSONString.java
org.kered.dko.json.JSONStringer.java
org.kered.dko.json.JSONTokener.java
org.kered.dko.json.JSONWriter.java
org.kered.dko.json.Pickle.java
org.kered.dko.json.XMLTokener.java
org.kered.dko.json.XML.java
org.kered.dko.junk.DerbyLoadTestSchema.java
org.kered.dko.junk.OracleCreateTestUser.java
org.kered.dko.junk.OracleLoadTestSchema.java
org.kered.dko.persistence.Util.java
org.kered.dko.util.DumpDatabase.java
sakila.Example0.java
sakila.Example1.java
sakila.Example2.java
sakila.Util.java