OK, so finally (!!!!) I've got it.

There seems to be some kind of race condition around closing sessions and
connections. I haven't been through the code enough to figure out where it
is, but I noticed that with this bit of code:

                    if (session != null)
                    {
                        session.Close();
                        session.Dispose();
                    }

                    if (connection != null)
                    {
                        connection.Close();
                        connection.Dispose();
                    }

..putting a breakpoint on the if(connection != null) line stopped the
connection leak. Removing the breakpoint meant the leak came back. So it
seems that the connection may be getting closed before the session, which
leaves the connection hanging around on the server side (or something like
that).

I then had a look at the Connection class, and found that the close() method
closes all sessions, so tried removing my explicit session.close()...and
voila, no more connection leaks!

Jim


Jim_Cross wrote:
> 
> OK, just tried a copy and paste of your code against a remote Windows box,
> and the connection leak appears there as well, so for me it only works
> correctly when run against ActiveMQ on my local box.
> Does it work for you on a remote machine also? If so, could you please
> mail me your NMS.dll and ActiveMQ.dll?
> 
> Thanks,
> 
> Jim
> 
> 
> 
> Jim_Cross wrote:
>> 
>> Thanks Chris.
>> 
>> I just tried this against the Active MQ running on my Windows box, and it
>> worked fine. Then tried it against Active MQ running on the Linux server,
>> and the connections leak appeared again.
>> So it either seems to be related to the OS it's running on, or the fact
>> that when running against my windows box the connections are effectively
>> local...
>> 
>> 
>> 
>> Dris wrote:
>>> 
>>> OK, I compiled the following against a recent-ish version of NMS. I
>>> haven't done a more recent compile because as I pointed out in the NMS
>>> issue tracker there was a bug in the use of selectors which I had fixed
>>> in my source but which hadn't been fixed in the SVN Repo (though it
>>> might have been very recently). This is just your original post with
>>> your disconnect logic shoved in the finally block. When I monitor this
>>> in JConsole the connection gets cleaned up fine and there is no
>>> Connection folder under localhost in the MBeans treeview. Hope this
>>> helps (though obviously it doesn't I guess!). Happy to mail you my
>>> NMS.dll and ActiveMQ.dll compiles for you to try.
>>> 
>>> Chris
>>> 
>>> 
>>> using System;
>>> using System.Threading;
>>> using NMS;
>>> 
>>> namespace ActiveMQ
>>> {
>>>     class TestMain
>>>     {
>>>             static void Main(string[] args)
>>>             {
>>>                     Uri uri = new Uri("tcp://bhw152:61616");
>>>                     
>>>                     ConnectionFactory factory = new ConnectionFactory(uri);
>>>                     
>>>                     IConnection connection = null;
>>>                     ISession session = null;
>>>                     
>>>                     try
>>>                     {
>>>                             connection = factory.CreateConnection();
>>>                             Console.WriteLine("Connection Created");
>>>                             
>>>                             connection.ClientId = "[test1] " + 
>>> connection.ClientId;
>>>                             
>>>                             session = connection.CreateSession();
>>>                             Console.WriteLine("Session Created");
>>>                             
>>>                             Thread.Sleep(10000);
>>>                     }
>>>                     finally
>>>                     {
>>>                             try
>>>                             {
>>>                                     if (session != null)
>>>                                     {
>>>                                         session.Close();
>>>                                         session.Dispose();
>>>                                     }
>>>                                     
>>>                                     if (connection != null)
>>>                                     {
>>>                                         connection.Dispose();
>>>                                     }
>>>                             }
>>>                             catch (Exception ex)
>>>                             {
>>>                                     Console.WriteLine("ERROR DISCONNECTING: 
>>> " + ex.StackTrace);
>>>                             }
>>>                             Console.WriteLine("Connection Closed");
>>>                     }
>>>             }
>>>     }
>>> }
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Orphan-connections-from-.NET-clients-tf3879502s2354.html#a11323469
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Reply via email to