ExecutionContext ec = ownerSM.getExecutionContext();
Transaction tx = ec.getTransaction();
boolean useUpdateLock = tx.lockReadObjects();
StatementClassMapping resultDefinition = null;
StatementParameterMapping paramDefinition = null;
String stmt = null;
if (startIdx < 0 && endIdx < 0)
{
// Iteration of all elements - cached
if (iteratorStmtLocked == null)
{
synchronized (this)
{
// Generate the statement, and statement mapping/parameter information
iteratorMappingDef = new StatementClassMapping();
iteratorMappingParams = new StatementParameterMapping();
SQLStatement sqlStmt = getSQLStatementForIterator(ownerSM, startIdx, endIdx,
iteratorMappingDef, iteratorMappingParams);
iteratorStmtUnlocked = sqlStmt.getSelectStatement().toSQL();
sqlStmt.addExtension("lock-for-update", true);
iteratorStmtLocked = sqlStmt.getSelectStatement().toSQL();
}
}
resultDefinition = iteratorMappingDef;
paramDefinition = iteratorMappingParams;
stmt = (useUpdateLock ? iteratorStmtLocked : iteratorStmtUnlocked);
}
else
{
// Iteration over a range so generate statement on the fly (uncached)
resultDefinition = new StatementClassMapping();
paramDefinition = new StatementParameterMapping();
SQLStatement sqlStmt = getSQLStatementForIterator(ownerSM, startIdx, endIdx, resultDefinition,
paramDefinition);
sqlStmt.addExtension("lock-for-update", useUpdateLock);
stmt = sqlStmt.getSelectStatement().toSQL();
}
try
{
ManagedConnection mconn = storeMgr.getConnection(ec);
SQLController sqlControl = ((RDBMSStoreManager)storeMgr).getSQLController();
try
{
// Create the statement and set the owner
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
StatementMappingIndex ownerIdx = paramDefinition.getMappingForParameter("owner");
int numParams = ownerIdx.getNumberOfParameterOccurrences();
for (int paramInstance=0;paramInstance<numParams;paramInstance++)
{
ownerIdx.getMapping().setObject(ec, ps,
ownerIdx.getParameterPositionsForOccurrence(paramInstance), ownerSM.getObject());