Skip to content

Commit

Permalink
DBCP-515 Added support for obtaining a reference to the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjenkinson committed Jul 23, 2018
1 parent 23f6717 commit 186518a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
Expand Up @@ -203,7 +203,7 @@ public void close() throws SQLException {
*/
protected class CompletionListener implements TransactionContextListener {
@Override
public void afterCompletion(final TransactionContext completedContext, final boolean commited) {
public void afterCompletion(final TransactionContext completedContext) {
if (completedContext == transactionContext) {
transactionComplete();
}
Expand Down
Expand Up @@ -111,23 +111,27 @@ public void setSharedConnection(final Connection sharedConnection) throws SQLExc
* if a problem occurs adding the listener to the transaction
*/
public void addTransactionContextListener(final TransactionContextListener listener) throws SQLException {
try {
getTransaction().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// empty
}
if (isActive()) {
try {
getTransaction().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// empty
}

@Override
public void afterCompletion(final int status) {
listener.afterCompletion(TransactionContext.this, status == Status.STATUS_COMMITTED);
}
});
} catch (final RollbackException e) {
// JTA spec doesn't let us register with a transaction marked rollback only
// just ignore this and the tx state will be cleared another way.
} catch (final Exception e) {
throw new SQLException("Unable to register transaction context listener", e);
@Override
public void afterCompletion(final int status) {
listener.afterCompletion(TransactionContext.this);
}
});
} catch (final RollbackException e) {
// JTA spec doesn't let us register with a transaction marked rollback only
// just ignore this and the tx state will be cleared another way.
} catch (final Exception e) {
throw new SQLException("Unable to register transaction context listener", e);
}
} else {
listener.afterCompletion(this);
}
}

Expand Down
Expand Up @@ -28,8 +28,6 @@ public interface TransactionContextListener {
*
* @param transactionContext
* the transaction context that completed
* @param commited
* true if the transaction committed; false otherwise
*/
void afterCompletion(TransactionContext transactionContext, boolean commited);
void afterCompletion(TransactionContext transactionContext);
}
Expand Up @@ -36,6 +36,7 @@
import java.sql.ResultSet;
import java.sql.PreparedStatement;

import javax.transaction.Synchronization;
import javax.transaction.Transaction;

/**
Expand All @@ -59,6 +60,36 @@ public void tearDown() throws Exception {
super.tearDown();
}

@Test
public void testGetConnectionInAfterCompletion() throws Exception {

final DelegatingConnection<?> connection = (DelegatingConnection<?>) newConnection();
// Don't close so we can check it for warnings in afterCompletion
transactionManager.getTransaction().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {

}

@Override
public void afterCompletion(int i) {
try {
Connection connection1 = ds.getConnection();
try {
connection1.getWarnings();
fail("Could operate on closed connection");
} catch (SQLException e) {
// This is expected
}
} catch (SQLException e) {
fail("Should have been able to get connection");
}
}
});
connection.close();
transactionManager.commit();
}

/**
* @see #testSharedConnection()
*/
Expand Down

0 comments on commit 186518a

Please sign in to comment.