// Process the delete of this object
try
{
ManagedConnection mconn = storeMgr.getConnection(om);
SQLController sqlControl = storeMgr.getSQLController();
try
{
// Perform the delete
boolean batch = true;
if (optimisticChecks || !om.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)
{
table.getDataStoreObjectIdMapping().setObject(om, ps,
mappingStatementIndex.getDatastoreId().getParameterIndex(), sm.getInternalObjectId());
}
else if (cmd.getIdentityType() == IdentityType.APPLICATION)
{
sm.provideFields(pkFieldNumbers, new ParameterSetter(sm, ps,
mappingStatementIndex.getPrimaryKeys(),true));
}
if (optimisticChecks)
{
// WHERE clause - current version discriminator
JavaTypeMapping verMapping = mappingStatementIndex.getVersion2().getMapping();
Object currentVersion = sm.getTransactionalVersion(sm.getObject());
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);
JPOXLogger.PERSISTENCE.error(msg);
throw new JPOXException(msg);
}
verMapping.setObject(om, ps,
mappingStatementIndex.getVersion2().getParameterIndex(), 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",
StringUtils.toJVMIDString(sm.getObject()), sm.getInternalObjectId(),
"" + sm.getTransactionalVersion(sm.getObject()));
JPOXLogger.DATASTORE.error(msg);
throw new JPOXOptimisticException(msg, sm.getObject());
}
}
finally
{
sqlControl.closeStatement(mconn, ps);
}
}
finally
{
mconn.release();