Skip to content

Commit

Permalink
Be a bit more rigorous about implementing equals() and hashCode() to …
Browse files Browse the repository at this point in the history
…support canonicalization of ConnectionTester, ConnectionCustomizer, and TaskRunnerFactory.
  • Loading branch information
swaldman committed Feb 25, 2024
1 parent 31e41c2 commit 9abd109
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/com/mchange/v2/c3p0/AbstractConnectionCustomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ public void onCheckOut( Connection c, String parentDataSourceIdentityToken ) th

public void onCheckIn( Connection c, String parentDataSourceIdentityToken ) throws Exception
{}

public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
public int hashCode() { return this.getClass().getName().hashCode(); }
}
3 changes: 3 additions & 0 deletions src/com/mchange/v2/c3p0/AbstractConnectionTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ public int statusOnException(Connection c, Throwable t, Throwable[] rootCauseOut

public int statusOnException(Connection c, Throwable t, String preferredTestQuery)
{ return statusOnException( c, t, preferredTestQuery, null); }

public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
public int hashCode() { return this.getClass().getName().hashCode(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,7 @@ public String getStackTraces()
return sb.toString();
}
}

public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
public int hashCode() { return this.getClass().getName().hashCode(); }
}
22 changes: 22 additions & 0 deletions src/com/mchange/v2/c3p0/ConnectionCustomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,26 @@ public void onCheckOut( Connection c, String parentDataSourceIdentityToken )
*/
public void onCheckIn( Connection c, String parentDataSourceIdentityToken )
throws Exception;

/**
* Define an equals(...) method so that multiple instances
* of your customizer can be canoncalized and shared.
*
* Often something like...
* <code><pre>
* public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
* </pre><code>
*/
public boolean equals( Object o );

/**
* keep consistent with equals()
*
* Often something like...
* <code><pre>
* public int hashCode() { return this.getClass().getName().hashCode(); }
* </pre><code>
*
*/
public int hashCode();
}
16 changes: 13 additions & 3 deletions src/com/mchange/v2/c3p0/ConnectionTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,24 @@ public interface ConnectionTester extends Serializable
public int statusOnException(Connection c, Throwable t);

/**
* Multiple testers that are of the same
* class and use the same criteria for determining fatality
* should test as equals().
* Define an equals(...) method so that multiple instances
* of your ConnectionTester can be canoncalized and shared.
*
* Often something like...
* <code><pre>
* public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
* </pre><code>
*/
public boolean equals( Object o );

/**
* keep consistent with equals()
*
* Often something like...
* <code><pre>
* public int hashCode() { return this.getClass().getName().hashCode(); }
* </pre><code>
*
*/
public int hashCode();
}
22 changes: 22 additions & 0 deletions src/com/mchange/v2/c3p0/TaskRunnerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,26 @@ public ThreadPoolReportingAsynchronousRunner createTaskRunner(
ConnectionPoolDataSource cpds,
Timer timer
);

/**
* Define an equals(...) method so that multiple instances
* of your factory can be canoncalized and shared.
*
* Often something like...
* <code><pre>
* public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); }
* </pre><code>
*/
public boolean equals( Object o );

/**
* keep consistent with equals()
*
* Often something like...
* <code><pre>
* public int hashCode() { return this.getClass().getName().hashCode(); }
* </pre><code>
*
*/
public int hashCode();
}
2 changes: 1 addition & 1 deletion src/com/mchange/v2/c3p0/impl/C3P0PooledConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void initAfterResourcePoolConstructed()
if (logger.isLoggable(MLevel.WARNING))
logger.log( MLevel.WARNING,
"Although no ConnectionTester is set, preferredTestQuery (or automaticTestTable) is also set, which can only be supported by a ConnectionTester. " +
"Reverting to use of ConnectionTester com.mchange.v2.c3p0.impl.DefaultConnectionTester instead." );
"Reverting to use of ConnectionTester com.mchange.v2.c3p0.impl.DefaultConnectionTester." );
this.connectionTestPath = new ConnectionTesterConnectionTestPath( rp, C3P0Registry.getConnectionTester(DefaultConnectionTester.class.getName()), scache, testQuery, c3p0PooledConnections );
}
else
Expand Down
3 changes: 0 additions & 3 deletions src/com/mchange/v2/c3p0/impl/DefaultConnectionTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public final class DefaultConnectionTester extends AbstractConnectionTester
private final static String PROP_KEY = "com.mchange.v2.c3p0.impl.DefaultConnectionTester.querylessTestRunner";
private final static String IS_VALID_TIMEOUT_KEY = "com.mchange.v2.c3p0.impl.DefaultConnectionTester.isValidTimeout";


final static MLogger logger = MLog.getLogger( DefaultConnectionTester.class );

final static int IS_VALID_TIMEOUT; // see static initializer
Expand Down Expand Up @@ -387,8 +386,6 @@ public int statusOnException(Connection c, Throwable t, String query, Throwable[
private static String queryInfo(String query)
{ return (query == null ? "[using Connection.isValid(...) if supported, or else traditional default query]" : "[query=" + query + "]"); }



public boolean equals( Object o )
{ return ( o != null && o.getClass() == DefaultConnectionTester.class ); }

Expand Down
3 changes: 3 additions & 0 deletions src/com/mchange/v2/c3p0/impl/DefaultTaskRunnerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ public void run()

return outHolder[0];
}

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

0 comments on commit 9abd109

Please sign in to comment.