datanucleus / datanucleus-rdbms

DataNucleus support for persistence to RDBMS Datastores
29 stars 66 forks source link

JDOQLQuery should throw exception if datastore does not support query canceling #490

Closed mboapache closed 3 months ago

mboapache commented 3 months ago

The JDO query implementation class JDOQLQuery in package org.datanucleus.store.rdbms.query calls method cancelTaskObject, that delegates to the JDBC PreparedStatement.cancel method. The code catches a SQLException thrown by the datastore method and logs a warning message:

    protected boolean cancelTaskObject(Object obj)
    {
        Statement ps = (Statement)obj;
        try
        {
            ps.cancel();
            return true;
        }
        catch (  sqle)
        {
            NucleusLogger.DATASTORE_RETRIEVE.warn("Error cancelling query", sqle);
            return false;
        }
    }

I propose to change method cancelTaskObject to rethrow the datastore exception wrapped into an JDOUnsupportedOptionException in case of a SQLFeatureNotSupportedException and NucleusDataStoreException otherwise:

    protected boolean cancelTaskObject(Object obj)
    {
        Statement ps = (Statement) obj;
        try
    {
            ps.cancel();
            return true;
        }
    catch (SQLFeatureNotSupportedException sqlfnse)
    {
            NucleusLogger.DATASTORE_RETRIEVE.warn("Feature not supported", sqlfnse);
            throw new JDOUnsupportedOptionException("Feature not supported", sqlfnse);
        }
    catch (SQLException sqle)
    {
            NucleusLogger.DATASTORE_RETRIEVE.warn("Error cancelling query", sqle);
            throw new NucleusDataStoreException("Error cancelling query", sqle);
        }
    }

You might consider removing the boolean return of method cancelTaskObject since the caller just uses the return value for a debug log message.

To reproduce the issue please go to the Apache JDO TCK github project https://github.com/apache/db-jdo and use branch JDO-836. Run the following command that executes the TCK test class QueryCancel: mvn -Djdo.tck.cfglist=cancel.conf clean install