From df8eb2861e8ab9d6989bc369b95a44af1ca4394d Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 6 Dec 2006 20:28:21 +0000 Subject: Enhanced OptimisticLockException message. --- .../amazon/carbonado/OptimisticLockException.java | 46 +++++++++++++++++++--- .../carbonado/spi/MasterStorableGenerator.java | 8 ++-- 2 files changed, 46 insertions(+), 8 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/amazon/carbonado/OptimisticLockException.java b/src/main/java/com/amazon/carbonado/OptimisticLockException.java index 949537e..909e0c6 100644 --- a/src/main/java/com/amazon/carbonado/OptimisticLockException.java +++ b/src/main/java/com/amazon/carbonado/OptimisticLockException.java @@ -30,20 +30,26 @@ public class OptimisticLockException extends PersistException { private static final long serialVersionUID = 4081788711829580886L; + private final Storable mStorable; + public OptimisticLockException() { super(); + mStorable = null; } public OptimisticLockException(String message) { super(message); + mStorable = null; } public OptimisticLockException(String message, Throwable cause) { super(message, cause); + mStorable = null; } public OptimisticLockException(Throwable cause) { super(cause); + mStorable = null; } /** @@ -68,14 +74,44 @@ public class OptimisticLockException extends PersistException { * @param savedVersion actual persistent version number of storable */ public OptimisticLockException(Object expectedVersion, Object savedVersion) { - super(makeMessage(expectedVersion, savedVersion)); + this(expectedVersion, savedVersion, null); + } + + /** + * @param expectedVersion version number that was expected for persistent + * record when update was executed + * @param savedVersion actual persistent version number of storable + * @param s Storable which was acted upon + */ + public OptimisticLockException(Object expectedVersion, Object savedVersion, Storable s) { + super(makeMessage(expectedVersion, savedVersion, s)); + mStorable = s; } - private static String makeMessage(Object expectedVersion, Object savedVersion) { + /** + * Returns the Storable which was acted upon, or null if not available. + */ + public Storable getStorable() { + return mStorable; + } + + private static String makeMessage(Object expectedVersion, Object savedVersion, Storable s) { + String message; if (expectedVersion == null && savedVersion == null) { - return null; + message = null; + } else { + message = "Update acted on version " + expectedVersion + + ", but canonical version is " + savedVersion; } - return "Update acted on version " + expectedVersion + - ", but canonical version is " + savedVersion; + + if (s != null) { + if (message == null) { + message = s.toStringKeyOnly(); + } else { + message = message + ": " + s.toStringKeyOnly(); + } + } + + return message; } } diff --git a/src/main/java/com/amazon/carbonado/spi/MasterStorableGenerator.java b/src/main/java/com/amazon/carbonado/spi/MasterStorableGenerator.java index 11657de..b3bc830 100644 --- a/src/main/java/com/amazon/carbonado/spi/MasterStorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/spi/MasterStorableGenerator.java @@ -546,7 +546,7 @@ public final class MasterStorableGenerator { // if (version support enabled) { // if (this.getVersionNumber() != saved.getVersionNumber()) { // throw new OptimisticLockException - // (this.getVersionNumber(), saved.getVersionNumber()); + // (this.getVersionNumber(), saved.getVersionNumber(), this); // } // } if (mFeatures.contains(MasterFeature.VERSIONING)) { @@ -565,8 +565,10 @@ public final class MasterStorableGenerator { b.loadLocal(savedVar); b.invoke(mInfo.getVersionProperty().getReadMethod()); b.convert(versionType, TypeDesc.OBJECT); - b.invokeConstructor(optimisticLockType, - new TypeDesc[] {TypeDesc.OBJECT, TypeDesc.OBJECT}); + b.loadThis(); + b.invokeConstructor + (optimisticLockType, + new TypeDesc[] {TypeDesc.OBJECT, TypeDesc.OBJECT, storableType}); b.throwObject(); sameVersion.setLocation(); } -- cgit v1.2.3