From 2dac6dea22e8ad78999913c4b4b1dd2e87c43700 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 23 Jul 2008 20:06:17 +0000 Subject: Detect if reload after insert fails and try to provide explanation as to cause. --- .../carbonado/repo/jdbc/JDBCStorableGenerator.java | 33 +++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java index 0dc9d38..d2b5a1e 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java @@ -27,10 +27,12 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import java.util.EnumSet; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; + import org.cojen.classfile.ClassFile; import org.cojen.classfile.CodeBuilder; import org.cojen.classfile.Label; @@ -43,6 +45,8 @@ import org.cojen.util.ClassInjector; import org.cojen.util.KeyFactory; import org.cojen.util.SoftValuedHashMap; +import org.joda.time.ReadableInstant; + import com.amazon.carbonado.FetchException; import com.amazon.carbonado.OptimisticLockException; import com.amazon.carbonado.PersistException; @@ -792,7 +796,34 @@ class JDBCStorableGenerator { b.invokeVirtual(MasterStorableGenerator.DO_TRY_LOAD_MASTER_METHOD_NAME, TypeDesc.BOOLEAN, new TypeDesc[] {jdbcSupportType, connectionType, lobArrayType}); - b.pop(); + Label reloaded = b.createLabel(); + b.ifZeroComparisonBranch(reloaded, "!="); + + String message = "Reload after insert failed, " + + "possibly because database changed the primary key: "; + + for (JDBCStorableProperty prop : mInfo.getPrimaryKeyProperties().values()) { + Class type = prop.getType(); + if (Date.class.isAssignableFrom(type) || + ReadableInstant.class.isAssignableFrom(type)) + { + message += "Property type of date may have been truncated: " + + prop.getName() + ": "; + } + } + + TypeDesc persistExType = TypeDesc.forClass(PersistException.class); + b.newObject(persistExType); + b.dup(); + b.loadConstant(message); + b.loadThis(); + b.invokeVirtual(TO_STRING_KEY_ONLY_METHOD_NAME, TypeDesc.STRING, null); + b.invokeVirtual(TypeDesc.STRING, "concat", + TypeDesc.STRING, new TypeDesc[] {TypeDesc.STRING}); + b.invokeConstructor(persistExType, new TypeDesc[] {TypeDesc.STRING}); + b.throwObject(); + + reloaded.setLocation(); } // Note: yieldConAndHandleException is not called, allowing any -- cgit v1.2.3