Skip to content

Guard ManagedConnection cleanup against a torn-down physical connection#26104

Open
renatsaf wants to merge 1 commit into
eclipse-ee4j:mainfrom
renatsaf:issue-24232-cleanup-torndown-npe
Open

Guard ManagedConnection cleanup against a torn-down physical connection#26104
renatsaf wants to merge 1 commit into
eclipse-ee4j:mainfrom
renatsaf:issue-24232-cleanup-torndown-npe

Conversation

@renatsaf

Copy link
Copy Markdown
Contributor

Problem

ManagedConnectionImpl.cleanup()resetConnectionProperties() resets the transaction isolation level and the auto-commit value so a pooled connection can be safely returned to the pool. ManagedConnectionFactoryImpl.resetIsolation() re-fetches the physical connection through getActualConnection() and already tolerates a null result, but resetAutoCommit() dereferenced actualConnection directly.

When cleanup() runs on a pooled/XA ManagedConnection whose physical connection has already been torn down — for instance one marked for removal whose transaction completed (transactionCompleted() nulls actualConnection) and which was then destroyed (destroy() nulls pooledConnection) — both fields are null. resetAutoCommit() then throws:

java.lang.NullPointerException: Cannot invoke "java.sql.Connection.setAutoCommit(boolean)" because "this.actualConnection" is null
        at com.sun.gjc.spi.ManagedConnectionImpl.resetAutoCommit(...)
        at com.sun.gjc.spi.ManagedConnectionImpl.resetConnectionProperties(...)
        at com.sun.gjc.spi.ManagedConnectionImpl.cleanup(...)
        at com.sun.enterprise.resource.allocator.AbstractConnectorAllocator.cleanup(...)

This is the same code path reported in #24232. The originally reported NPE in getActualConnection() itself was already fixed by an earlier guard; this addresses the remaining latent NPE in resetAutoCommit() on the same scenario.

Fix

Guard resetConnectionProperties() with the same null-connection check resetIsolation() already relies on. When getActualConnection() returns null there is no physical connection to reset, so cleanup becomes a no-op instead of dereferencing a null connection.

Test

Adds ManagedConnectionImplTest.testCleanupOnTornDownPooledConnectionDoesNotThrow, which destroys a pooled ManagedConnection (nulling both connection fields) after the application changed auto-commit, then calls cleanup(). The test fails with the exact reported NPE on main and passes with this change.

Fixes #24232

🤖 Generated with Claude Code

cleanup() -> resetConnectionProperties() resets the transaction isolation
level and auto-commit value so a pooled connection can be safely reused.
resetIsolation() re-fetches the physical connection via getActualConnection()
and tolerates a null result, but resetAutoCommit() dereferenced
actualConnection directly.

When cleanup() runs on a pooled/XA ManagedConnection whose physical
connection has already been torn down - for instance one marked for removal
whose transaction completed and which was then destroyed - both
actualConnection and pooledConnection are null. resetAutoCommit() then threw
a NullPointerException.

Guard resetConnectionProperties() with the same null-connection check
resetIsolation() already relies on, so cleanup becomes a no-op when there is
no physical connection to reset. Adds a regression test that reproduces the
NPE on the reported code path.

Fixes eclipse-ee4j#24232

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NPE in server.log when running jdbc_all tests

1 participant