boolean batch = false;
// TODO Set the batch flag based on whether we have no other SQL being invoked in here just our UPDATE
try
{
ManagedConnection mconn = storeMgr.getConnection(om);
SQLController sqlControl = storeMgr.getSQLController();
try
{
// Perform the update
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, batch);
try
{
Object currentVersion = sm.getTransactionalVersion(sm.getObject());
Object nextVersion = null;
if (versionMetaData != null)
{
// Set the next version in the object
if (versionMetaData.getFieldName() != null)
{
// Version field
AbstractMemberMetaData verfmd = cmd.getMetaDataForMember(table.getVersionMetaData().getFieldName());
if (currentVersion instanceof Integer)
{
// Cater for Integer-based versions TODO Generalise this
currentVersion = new Long(((Integer)currentVersion).longValue());
}
nextVersion =
VersionMapping.getNextVersion(versionMetaData, currentVersion);
if (verfmd.getType() == Integer.class || verfmd.getType() == int.class)
{
// Cater for Integer-based versions TODO Generalise this
nextVersion = new Integer(((Long)nextVersion).intValue());
}
sm.replaceField(verfmd.getAbsoluteFieldNumber(), nextVersion, false);
}
else
{
// Surrogate version column
nextVersion = VersionMapping.getNextVersion(versionMetaData, currentVersion);
}
sm.setTransactionalVersion(nextVersion);
}
// SELECT clause - set the required fields to be updated
if (updateFieldNumbers != null)
{
sm.provideFields(updateFieldNumbers, new ParameterSetter(sm, ps, mappingStatementIndex.getFields(), true));
}
if (versionMetaData != null && versionMetaData.getFieldName() == null)
{
// SELECT clause - set the surrogate version column to the new version
table.getVersionMapping(false).setObject(om, ps,
mappingStatementIndex.getVersion().getParameterIndex(), nextVersion);
}
// WHERE clause - primary key fields
if (table.getIdentityType() == IdentityType.DATASTORE)
{
// a). datastore identity
table.getDataStoreObjectIdMapping().setObject(om, ps, mappingStatementIndex.getDatastoreId().getParameterIndex(),
sm.getInternalObjectId());
}
else if (table.getIdentityType() == IdentityType.APPLICATION)
{
// b). application identity
sm.provideFields(pkFieldNumbers, new ParameterSetter(sm, ps, mappingStatementIndex.getPrimaryKeys(),true));
}
if (optimisticChecks)
{
if (currentVersion == null)
{
// Somehow the version is not set on this object (not read in ?) so report the bug
String msg = LOCALISER.msg("052201",
sm.getInternalObjectId(), table);
JPOXLogger.PERSISTENCE.error(msg);
throw new JPOXException(msg);
}
// WHERE clause - current version discriminator
JavaTypeMapping verMapping = mappingStatementIndex.getVersion2().getMapping();
verMapping.setObject(om, ps,
mappingStatementIndex.getVersion2().getParameterIndex(), currentVersion);
}
int[] rcs = sqlControl.executeStatementUpdate(mconn, stmt, ps, !batch);
if (rcs[0] == 0 && optimisticChecks)
{
// No object updated so either object disappeared or failed optimistic version checks
// TODO Batching : when we use batching here we need to process these somehow
String msg = LOCALISER.msg("052203",
StringUtils.toJVMIDString(sm.getObject()), sm.getInternalObjectId(),
"" + sm.getTransactionalVersion(sm.getObject()));
JPOXLogger.PERSISTENCE.error(msg);
throw new JPOXOptimisticException(msg, sm.getObject());
}
}
finally
{
sqlControl.closeStatement(mconn, ps);
}
}
finally
{
mconn.release();