Ken Pritchard created DBCP-601:
----------------------------------

             Summary: LocalXAResource.prepare() leaves a read-only connection 
enlisted after voting XA_RDONLY, so the next transaction fails to enlist
                 Key: DBCP-601
                 URL: https://issues.apache.org/jira/browse/DBCP-601
             Project: Commons DBCP
          Issue Type: Bug
    Affects Versions: 2.14.0
            Reporter: Ken Pritchard


When a transaction enlists a read-only {{BasicManagedDataSource}} connection 
together with at least one other XA resource, the transaction manager runs 
two-phase commit. {{LocalXAConnectionFactory.LocalXAResource.prepare()}} 
returns {{XA_RDONLY}} for a read-only connection, but it does not clear 
{{currentXid}}.

Under the XA specification, a resource that votes {{XA_RDONLY}} takes no part 
in phase 2: the transaction manager does not call {{commit()}} or 
{{rollback()}} on it. Those are the only places {{currentXid}} is cleared, so 
the connection goes back to the pool still associated with a transaction that 
has already completed.

The next transaction that borrows that connection fails in {{start()}}:

{noformat}
javax.transaction.xa.XAException: Already enlisted in another transaction with 
xid ...
java.sql.SQLException: Unable to enlist connection in transaction: 
enlistResource returns 'false'.
javax.transaction.RollbackException: Unable to commit: transaction marked for 
rollback
{noformat}

The pool then destroys that connection, so the transaction after it succeeds. 
Each read-only two-phase transaction therefore causes one later, unrelated 
transaction to fail and roll back, which makes the cause hard to trace.

*Steps to reproduce*
# Configure a {{BasicManagedDataSource}} with {{defaultReadOnly=true}} and 
{{maxTotal=1}}.
# Transaction 1: begin, get a connection, enlist a second {{XAResource}} that 
votes {{XA_OK}}, commit. It commits normally through two-phase commit.
# Transaction 2: begin, get a connection. Enlisting fails as above, and commit 
throws {{RollbackException}}.
# Transaction 3: works, on a new physical connection.

Reproduced on master (7b07108c and 4f68273a) with the Geronimo transaction 
manager that DBCP's own tests use.

*Not affected:* single-resource transactions (one-phase commit calls 
{{commit(xid, true)}}, which clears {{currentXid}}) and read-write connections.

The read-only branch in {{prepare()}} dates from DBCP-228. The existing tests 
don't reach it because H2's {{Connection.isReadOnly()}} always returns 
{{false}}.

The same mistake, clearing per-transaction state on commit and rollback but not 
after a read-only prepare, has been fixed in other Apache projects: AMQ-2556 
(ActiveMQ 5.4.0; transaction state "not released ... at prepare time where 
result is XAResource.XA_RDONLY") and DERBY-5562 (Derby 10.8.3.0 / 10.9.1.0; 
"When XA_RDONLY is returned, no commit or rollback will be received").

Apache Tomcat bundles this code as {{org.apache.tomcat.dbcp.dbcp2.managed}}, so 
it has the same behavior until it next syncs from a DBCP release.

*Proposed fix:* on the read-only path in {{prepare()}}, clear {{currentXid}} 
before returning {{XA_RDONLY}} (still returning {{XA_RDONLY}}, so the 
transaction manager skips phase 2 as it should). I have a patch with tests and 
will open a pull request.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to