}
}
// Choose the statement based on whether optimistic or not
String stmt = null;
ExecutionContext ec = sm.getExecutionContext();
RDBMSStoreManager storeMgr = (RDBMSStoreManager)ec.getStoreManager();
boolean optimisticChecks = (versionMetaData != null && ec.getTransaction().getOptimistic() && versionChecks);
if (optimisticChecks)
{
stmt = deleteStmtOptimistic;
}
else
{
stmt = deleteStmt;
}
// Process the delete of this object
try
{
ManagedConnection mconn = storeMgr.getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try
{
// Perform the delete
boolean batch = true;
if (optimisticChecks || !ec.getTransaction().isActive())
{
// Turn OFF batching if doing optimistic checks (since we need the result of the delete)
// or if using nontransactional writes (since we want it sending to the datastore now)
batch = false;
}
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, batch);
try
{
// provide primary key field(s)
if (cmd.getIdentityType() == IdentityType.DATASTORE)
{
StatementMappingIndex mapIdx = mappingStatementIndex.getDatastoreId();
for (int i=0;i<mapIdx.getNumberOfParameterOccurrences();i++)
{
table.getDatastoreObjectIdMapping().setObject(ec, ps,
mapIdx.getParameterPositionsForOccurrence(i), sm.getInternalObjectId());
}
}
else if (cmd.getIdentityType() == IdentityType.APPLICATION)
{
StatementClassMapping mappingDefinition = new StatementClassMapping();
StatementMappingIndex[] idxs = mappingStatementIndex.getPrimaryKeys();
for (int i=0;i<idxs.length;i++)
{
if (idxs[i] != null)
{
mappingDefinition.addMappingForMember(i, idxs[i]);
}
}
sm.provideFields(pkFieldNumbers,
storeMgr.getFieldManagerForStatementGeneration(sm, ps, mappingDefinition, true));
}
if (optimisticChecks)
{
// WHERE clause - current version discriminator
JavaTypeMapping verMapping = mappingStatementIndex.getVersion2().getMapping();
Object currentVersion = sm.getTransactionalVersion();
if (currentVersion == null)
{
// Somehow the version is not set on this object (not read in ?) so report the bug
String msg = LOCALISER.msg("052202",
sm.getInternalObjectId(), table);
NucleusLogger.PERSISTENCE.error(msg);
throw new NucleusException(msg);
}
StatementMappingIndex mapIdx = mappingStatementIndex.getVersion2();
for (int i=0;i<mapIdx.getNumberOfParameterOccurrences();i++)
{
verMapping.setObject(ec, ps,
mapIdx.getParameterPositionsForOccurrence(i), currentVersion);
}
}
int[] rcs = sqlControl.executeStatementUpdate(mconn, stmt, ps, !batch);
if (optimisticChecks && rcs[0] == 0)
{
// No object deleted so either object disappeared or failed optimistic version checks
String msg = LOCALISER.msg("052203",
sm.toPrintableID(), sm.getInternalObjectId(),
"" + sm.getTransactionalVersion());
NucleusLogger.DATASTORE.error(msg);
throw new NucleusOptimisticException(msg, sm.getObject());
}
if (relatedObjectsToDelete != null && !relatedObjectsToDelete.isEmpty())
{
// Delete any related objects that need deleting after the delete of this object
Iterator iter = relatedObjectsToDelete.iterator();
while (iter.hasNext())
{
Object relatedObject = iter.next();
ec.deleteObjectInternal(relatedObject);
}
}
}
finally
{