Skip to content

Commit

Permalink
Define abstract PercentXXFailConnectionTester, factor implementations…
Browse files Browse the repository at this point in the history
… into subclasses.
  • Loading branch information
swaldman committed May 6, 2024
1 parent d2d6f0d commit 7fcd4e4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,9 @@
import com.mchange.v2.log.MLog;
import com.mchange.v2.log.MLogger;

public final class Percent20FailConnectionTester implements QueryConnectionTester
public final class Percent20FailConnectionTester extends PercentXXFailConnectionTester
{
final static MLogger logger = MLog.getLogger( Percent20FailConnectionTester.class );

{
//logger.log(MLevel.WARNING, "Instantiated: " + this, new Exception("Instantiation Stack Trace.") );
}

private int roulette()
{
if (Math.random() < 0.20d)
return CONNECTION_IS_INVALID;
else
return CONNECTION_IS_OKAY;
}

public int activeCheckConnection(Connection c)
{
//logger.warning(this + ": activeCheckConnection(Connection c)");
return roulette();
}

public int statusOnException(Connection c, Throwable t)
{
//logger.warning(this + ": statusOnException(Connection c, Throwable t)");
return roulette();
}

public int activeCheckConnection(Connection c, String preferredTestQuery)
{
//logger.warning(this + ": activeCheckConnection(Connection c, String preferredTestQuery)");
return roulette();
}

public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
public int hashCode() { return this.getClass().getName().hashCode(); }
public Percent20FailConnectionTester()
{ super(20); }
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,9 @@
import com.mchange.v2.log.MLog;
import com.mchange.v2.log.MLogger;

public final class Percent80FailConnectionTester implements QueryConnectionTester
public final class Percent80FailConnectionTester extends PercentXXFailConnectionTester
{
final static MLogger logger = MLog.getLogger( Percent80FailConnectionTester.class );

{
//logger.log(MLevel.WARNING, "Instantiated: " + this, new Exception("Instantiation Stack Trace.") );
}

private int roulette()
{
if (Math.random() < 0.80d)
return CONNECTION_IS_INVALID;
else
return CONNECTION_IS_OKAY;
}

public int activeCheckConnection(Connection c)
{
//logger.warning(this + ": activeCheckConnection(Connection c)");
return roulette();
}

public int statusOnException(Connection c, Throwable t)
{
//logger.warning(this + ": statusOnException(Connection c, Throwable t)");
return roulette();
}

public int activeCheckConnection(Connection c, String preferredTestQuery)
{
//logger.warning(this + ": activeCheckConnection(Connection c, String preferredTestQuery)");
return roulette();
}

public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
public int hashCode() { return this.getClass().getName().hashCode(); }
public Percent80FailConnectionTester()
{ super(80); }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mchange.v2.c3p0.test;

import java.sql.Connection;
import com.mchange.v2.c3p0.QueryConnectionTester;
import com.mchange.v2.log.MLevel;
import com.mchange.v2.log.MLog;
import com.mchange.v2.log.MLogger;

public abstract class PercentXXFailConnectionTester implements QueryConnectionTester
{
//final static MLogger logger = MLog.getLogger( PercentXXFailConnectionTester.class );

double threshold;

protected PercentXXFailConnectionTester(int pct)
{
if (pct < 0 || pct > 100)
throw new IllegalArgumentException("Percentage must be between 0 and 100, found " + pct + ".");
this.threshold = pct / 100d;
//logger.log(MLevel.WARNING, "Instantiated: " + this, new Exception("Instantiation Stack Trace.") );
}

private int roulette()
{
if (Math.random() < threshold)
return CONNECTION_IS_INVALID;
else
return CONNECTION_IS_OKAY;
}

public int activeCheckConnection(Connection c)
{
//logger.warning(this + ": activeCheckConnection(Connection c)");
return roulette();
}

public int statusOnException(Connection c, Throwable t)
{
//logger.warning(this + ": statusOnException(Connection c, Throwable t)");
return roulette();
}

public int activeCheckConnection(Connection c, String preferredTestQuery)
{
//logger.warning(this + ": activeCheckConnection(Connection c, String preferredTestQuery)");
return roulette();
}

public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
public int hashCode() { return this.getClass().getName().hashCode(); }
}

0 comments on commit 7fcd4e4

Please sign in to comment.