Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected results for statement with closeOnCompletion #684

Closed
shawjef3 opened this issue Nov 11, 2016 · 3 comments
Closed

unexpected results for statement with closeOnCompletion #684

shawjef3 opened this issue Nov 11, 2016 · 3 comments

Comments

@shawjef3
Copy link

@shawjef3 shawjef3 commented Nov 11, 2016

If a statement with closeOnCompletion() is executed a second time before its results have been consumed, it throws a NullPointerException. I am unsure what the appropriate error is, but it should be more descriptive. Something like, org.postgresql.util.PSQLException: Statement with closeOnCompletion can not be executed twice..

    val s = connection.prepareStatement("SELECT 1")
    s.closeOnCompletion()
    s.execute()
    s.execute()

The following, where I close the result set, throws org.postgresql.util.PSQLException: This statement has been closed., as I would expect.

    val s = connection.prepareStatement("SELECT 1")
    s.closeOnCompletion()
    s.executeQuery().close()
    s.execute()
@shawjef3

This comment has been minimized.

Copy link
Author

@shawjef3 shawjef3 commented Nov 11, 2016

The NullPointerException is thrown from PgStatement#closeForNextExecution(). Perhaps firstUnclosedResult is set to null from another thread after it is verified to be not null.

@lightswitch05

This comment has been minimized.

Copy link

@lightswitch05 lightswitch05 commented Apr 5, 2017

I've recreated this same exception under different conditions using version 42.0.0. I have a generic getPreparedStatement function that calls closeOnCompletion() for all created statements. This is to help reduce any resource leakage. I then have a generic closeStatement function that attempts to always close a statement - called in all finally blocks. If I call statement.close(); on a statement that already has already had closeOnCompletion() called, then it always throws this exception, even if I check !statement.isClosed() first. The workaround is to also check !isCloseOnCompletion() before trying to close

Exception

java.lang.NullPointerException
        at org.postgresql.jdbc.PgStatement.closeForNextExecution(PgStatement.java:321)
        at org.postgresql.jdbc.PgStatement.close(PgStatement.java:603)
        at org.postgresql.jdbc.PgPreparedStatement.close(PgPreparedStatement.java:202)
        at com.<redacted>.closeStatement(<redacted>.java:73)

Example code

    protected PreparedStatement getPreparedStatement(final String sql) throws ConnectException {
        Connection connection = getConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.closeOnCompletion();
            return statement;
        } catch (SQLException ex) {
            this.closeStatement(statement);
            throw new ConnectException("Unable to get PreparedStatement.", ex);
        }
    }

    protected void closeStatement(final Statement statement) {
        try {
            if (statement != null && !statement.isClosed() && !statement.isCloseOnCompletion()) {
                // this close will cause the null pointer if I don't check isCloseOnCompletion first
                statement.close();
            }
        } catch (SQLException ex) {
            LOGGER.warning(String.format("Error closing statement: %s", ex.getMessage()));
        }
    }
davecramer added a commit to davecramer/pgjdbc that referenced this issue Nov 18, 2019
davecramer added a commit that referenced this issue Nov 18, 2019
…es issue #684 (#1610)
@davecramer

This comment has been minimized.

Copy link
Member

@davecramer davecramer commented Nov 18, 2019

fixed by #1610

@davecramer davecramer closed this Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.