CachingConnectionFactory - Invalid session in session cache [SPR-16450] #20995
Labels
in: messaging
Issues in messaging modules (jms, messaging)
status: backported
An issue that has been backported to maintenance branches
type: bug
A general bug
Milestone
Radek Kraus opened SPR-16450 and commented
I have a suspicion there is a small possibility, that invalid session (session, which belongs to the "old" connection) is returned into session cache (
cachedSessions
map). Let suppose following scenario.Two threads - T1 and T2. T1 asks for connection and session and then close the acquired session (session is put into the session cache).
Now, the thread T2 invokes
onException()
method, because current connection is invalid (disconnection from JMS Provider or whatever ...).The
onException
method caused thatCachingConnectionFactory.resetConnection()
is invoked (clear the session cache,active
boolean flag settings). At the moment the thread T1 comes back and asks next JMS session (createSession(false, Session.AUTO_ACKNOWLEDGE)
), exactly in a moment, when thread T2 already cleared the session cache and setactive
boolean flag back ontrue
value (see my comment bellow). It causes that new session list is registered into cache session map (sessionList
and it is passed intoCachedSessionInvocationHandler
-getSession()
method).I know that invocation of
createSession(...)
method (done by T1) probably fails, because connection is already invalid, but we suppose that new session is successfully created. After T1 receives new session, theresetConnection()
method is finished by invocation ofsuper.resetConnection()
. Now we suppose that thread T1 (or some another thread) invokescreateConnection()
method, which caused that new connection is established.Now, the session, which was acquired by T1 in a moment when
resetConnection()
method was running, is closed =>CachedSessionInvocationHandler.logicalClose()
is invoked. Again, I know that there is a big probability, that this operation fails (message consumer close or session rolback throws JMSException, which is handled now - #16762). But when the "close" actions finish successfully, the session, which belong to "old" connection is returned back into session cache.I attached "demonstration" test, which shows this behavior. Unfortunately I am unable to write this test, without modification of
CachingConnectionFactory
, because I need to simulate "some delay" inresetConnection()
method, so as first I modifiedCachingConnectionFactory
- new method clearSessionCache() was created (but this modification doesn't change the behavior).Content of "demonstration" zip package:
CachingConnectionFactory
, which is a start point for problem simulation)I know that there is very small chance to reach this scenario (small time range, all close operations must be success ...), but anyway, I still feel "synchrozation" problem here. In addition my fix proposal is too "invasive" (signature of protected methods was changed ...). Maybe there is a better way how to fix this problem:
doCreateConnection()
methodcloseConnection()
method, which is guarded byconnectionMonitor
lock (but it is not probably wanted - delay in create new connection till old connection is invalidated)The main motivation for creating this issue was, that we have strange problems in old Spring (3.0.x), which can be caused by this problem. Maybe/probably the situation should be better from a moment when #16762 was done. But I think, that "synchronization" problem still exists.
Affects: 4.3.13
Attachments:
Issue Links:
Referenced from: commits b6ecfcf, d8a2672
Backported to: 4.3.15
The text was updated successfully, but these errors were encountered: