Protect JMS connection creation against prepareConnection errors
This commit uses a local variable for the creation of a new JMS
Connection so that a rare failure in prepareConnection(...) does not
leave the connection field in a partially initialized state.
If such a JMSException occurs, the intermediary connection is closed.
This commit further defends against close() failures at that point,
by logging the close exception at DEBUG level. As a result, the original
JMSException is always re-thrown.
Closes gh-29116
See gh-29115
@ -346,8 +346,26 @@ public class SingleConnectionFactory implements ConnectionFactory, QueueConnecti
@@ -346,8 +346,26 @@ public class SingleConnectionFactory implements ConnectionFactory, QueueConnecti
if(this.connection!=null){
closeConnection(this.connection);
}
this.connection=doCreateConnection();
prepareConnection(this.connection);
// Create new (method local) connection, which is later assigned to instance connection
// - prevention to hold instance connection without exception listener, in case when
// some subsequent methods (after creation of connection) throws JMSException
Connectioncon=doCreateConnection();
try{
prepareConnection(con);
this.connection=con;
}
catch(JMSExceptionex){
// Attempt to close new (not used) connection to release possible resources
try{
con.close();
}
catch(Throwableth){
if(logger.isDebugEnabled()){
logger.debug("Could not close newly obtained JMS Connection that failed to prepare",th);