QueryResult qr = null;
try
{
RDBMSManager storeMgr = (RDBMSManager)om.getStoreManager();
ManagedConnection mconn = storeMgr.getConnection(om);
SQLController sqlControl = storeMgr.getSQLController();
try
{
StatementText stmtText = null;
if (query.getType() == Query.SELECT)
{
stmtText = queryStmt.toStatementText(useUpdateLock);
}
else if (query.getType() == Query.BULK_UPDATE)
{
stmtText = queryStmt.toUpdateStatementText();
throw new JPOXException("JPOX doesnt currently support bulk update statements");
}
else if (query.getType() == Query.BULK_DELETE)
{
// TODO Distinguish between update and delete
stmtText = queryStmt.toDeleteStatementText();
throw new JPOXException("JPOX doesnt currently support bulk delete statements");
}
PreparedStatement ps = getStatement(mconn, stmtText);
try
{
// Apply timeouts, result set constraints etc
prepareStatementForExecution(ps);
// Add a limit on the number of rows to include the maximum we may need
long toExclNo = query.getRangeToExcl();
if (toExclNo != 0 && toExclNo != Long.MAX_VALUE)
{
if (toExclNo > Integer.MAX_VALUE)
{
// setMaxRows takes an int as input so limit to the correct range
ps.setMaxRows(Integer.MAX_VALUE);
}
else
{
ps.setMaxRows((int)toExclNo);
}
}
if (query.getType() != Query.SELECT)
{
JPOXLogger.JDO.debug(">> SQLEvaluator.evaluate BULK operation SELECT");
}
if (query.getType() == Query.SELECT)
{
// SELECT query
ResultSet rs = sqlControl.executeStatementQuery(mconn, stmtText.toString(), ps);
try
{
// Check the type of result set needed
if (getResultSetType().equals("scroll-insensitive") ||
getResultSetType().equals("scroll-sensitive"))
{
qr = new ScrollableQueryResult(queryStmt, query, rof, rs,
distinct ? null : candidateCollection);
}
else
{
qr = new ForwardQueryResult(queryStmt, query, rof, rs,
distinct ? null : candidateCollection);
}
final QueryResult qr1 = qr;
final ManagedConnection mconn1 = mconn;
ManagedConnectionResourceListener listener = new ManagedConnectionResourceListener()
{
public void managedConnectionPreClose(){}
public void managedConnectionPostClose(){}
public void managedConnectionFlushed()
{
// Disconnect the query from this ManagedConnection (read in unread rows etc)
qr1.disconnect();
}
public void resourcePostClose()
{
mconn1.removeListener(this);
}
};
mconn.addListener(listener);
((AbstractRDBMSQueryResult)qr).addConnectionListener(listener);
}