Skip to content

Commit

Permalink
Force the session to be invalidated when XA enlist outcome is unknown
Browse files Browse the repository at this point in the history
Under any error or indeterminate outcome on create of an XA session force
the session to be closed and invalidated instead of returning it to the pull.
  • Loading branch information
tabish121 committed Apr 26, 2024
1 parent 468530d commit ac81809
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ public JmsPoolSession(PooledSessionKey key, PooledSessionHolder sessionHolder, K

@Override
public void close() throws JMSException {
if (!ignoreClose && closed.compareAndSet(false, true)) {
boolean invalidate = cleanupSession();
if (!ignoreClose) {
internalClose(false);
}
}

public void internalClose(boolean forceInvalidate) throws JMSException {
if (closed.compareAndSet(false, true)) {
final boolean invalidate = cleanupSession() || forceInvalidate;

if (invalidate) {
// lets close the session and not put the session back into the pool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Session createSession(boolean transacted, int ackMode) throws JMSExceptio
throw new JMSException("Enlistment of Pooled Session into transaction failed");
}
} catch (Exception ex) {
sync.close();
sync.fail();
throw ex;
}
}
Expand Down Expand Up @@ -116,6 +116,18 @@ private JmsPooledXASessionSynchronization(JmsPoolSession session) {
this.session = session;
}

public void fail() throws JMSException {
if (closed.compareAndSet(false, true)) {
// Force the session to close and invalidate itself.
try {
session.internalClose(true);
} finally {
session = null;
decrementReferenceCount();
}
}
}

public void close() throws JMSException {
if (closed.compareAndSet(false, true)) {
// This will return session to the pool.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public void testCreateXASessionFailsOnAddSynchronization() throws Exception {

assertThrows(JMSException.class, () -> connection.createSession());

// Session not should be ignoring close at this stage
assertEquals(1, connection.getNumtIdleSessions());
// Session should be invalidated as we don't know the state after failed register
assertEquals(0, connection.getNumtIdleSessions());
}

@Test
Expand All @@ -134,7 +134,7 @@ public void testCreateXASessionFailsOnEnlist() throws Exception {

assertThrows(JMSException.class, () -> connection.createSession());

// Session not should be ignoring close at this stage
assertEquals(1, connection.getNumtIdleSessions());
// Session should be invalidated as we don't know the state after failed enlist
assertEquals(0, connection.getNumtIdleSessions());
}
}

0 comments on commit ac81809

Please sign in to comment.