Allow configuration of the exception class used when conflicts arise at UnitOfWork commit time to enable clients to provide custom/advanced/specific exceptions without having to do a bunch of encapsulation.

This commit is contained in:
Greg Burd 2017-09-13 12:28:12 -04:00
parent 66578d77d5
commit efca86783c
4 changed files with 16 additions and 2 deletions

View file

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>net.helenus</groupId> <groupId>net.helenus</groupId>
<artifactId>helenus-core</artifactId> <artifactId>helenus-core</artifactId>
<version>2.0.30-SNAPSHOT</version> <version>2.0.31-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>helenus</name> <name>helenus</name>

View file

@ -75,6 +75,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C
Executor executor, Executor executor,
boolean dropSchemaOnClose, boolean dropSchemaOnClose,
ConsistencyLevel consistencyLevel, ConsistencyLevel consistencyLevel,
Class<? extends Exception> conflictExceptionClass,
MetricRegistry metricRegistry, MetricRegistry metricRegistry,
Tracer tracer) { Tracer tracer) {
this.session = session; this.session = session;
@ -88,6 +89,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C
this.executor = executor; this.executor = executor;
this.dropSchemaOnClose = dropSchemaOnClose; this.dropSchemaOnClose = dropSchemaOnClose;
this.defaultConsistencyLevel = consistencyLevel; this.defaultConsistencyLevel = consistencyLevel;
UnitOfWork.conflictExceptionClass = conflictExceptionClass;
this.metricRegistry = metricRegistry; this.metricRegistry = metricRegistry;
this.zipkinTracer = tracer; this.zipkinTracer = tracer;

View file

@ -43,6 +43,7 @@ public final class SessionInitializer extends AbstractSessionOperations {
private Tracer zipkinTracer; private Tracer zipkinTracer;
private PrintStream printStream = System.out; private PrintStream printStream = System.out;
private Executor executor = MoreExecutors.directExecutor(); private Executor executor = MoreExecutors.directExecutor();
private Class<? extends Exception> conflictingUnitOfWorkClass = ConflictingUnitOfWorkException.class;
private SessionRepositoryBuilder sessionRepository; private SessionRepositoryBuilder sessionRepository;
@ -110,6 +111,11 @@ public final class SessionInitializer extends AbstractSessionOperations {
return this; return this;
} }
public SessionInitializer setConflictingUnitOfWorkException(Class<? extends Exception> e) {
this.conflictingUnitOfWorkClass = e;
return this;
}
public SessionInitializer consistencyLevel(ConsistencyLevel consistencyLevel) { public SessionInitializer consistencyLevel(ConsistencyLevel consistencyLevel) {
this.consistencyLevel = consistencyLevel; this.consistencyLevel = consistencyLevel;
return this; return this;
@ -235,6 +241,7 @@ public final class SessionInitializer extends AbstractSessionOperations {
executor, executor,
autoDdl == AutoDdl.CREATE_DROP, autoDdl == AutoDdl.CREATE_DROP,
consistencyLevel, consistencyLevel,
conflictingUnitOfWorkClass,
metricRegistry, metricRegistry,
zipkinTracer); zipkinTracer);
} }

View file

@ -9,6 +9,7 @@ import java.util.*;
/** Encapsulates the concept of a "transaction" as a unit-of-work. */ /** Encapsulates the concept of a "transaction" as a unit-of-work. */
public class UnitOfWork implements AutoCloseable { public class UnitOfWork implements AutoCloseable {
protected static Class<? extends Exception>conflictExceptionClass = ConflictingUnitOfWorkException.class;
private final List<UnitOfWork> nested = new ArrayList<>(); private final List<UnitOfWork> nested = new ArrayList<>();
private final HelenusSession session; private final HelenusSession session;
private final UnitOfWork parent; private final UnitOfWork parent;
@ -74,7 +75,7 @@ public class UnitOfWork implements AutoCloseable {
* @return a function from which to chain work that only happens when commit is successful * @return a function from which to chain work that only happens when commit is successful
* @throws ConflictingUnitOfWorkException when the work overlaps with other concurrent writers. * @throws ConflictingUnitOfWorkException when the work overlaps with other concurrent writers.
*/ */
public PostCommitFunction<Void, Void> commit() throws ConflictingUnitOfWorkException { public PostCommitFunction<Void, Void> commit() throws Exception {
// All nested UnitOfWork should be committed (not aborted) before calls to commit, check. // All nested UnitOfWork should be committed (not aborted) before calls to commit, check.
boolean canCommit = true; boolean canCommit = true;
TreeTraverser<UnitOfWork> traverser = TreeTraverser.using(node -> node::getChildNodes); TreeTraverser<UnitOfWork> traverser = TreeTraverser.using(node -> node::getChildNodes);
@ -120,6 +121,10 @@ public class UnitOfWork implements AutoCloseable {
return new PostCommitFunction(this, null); return new PostCommitFunction(this, null);
} }
} }
// else {
// Constructor<?> ctor = clazz.getConstructor(conflictExceptionClass);
// Object object = ctor.newInstance(new Object[] { String message });
// }
return new PostCommitFunction(this, postCommit); return new PostCommitFunction(this, postCommit);
} }