views:

20

answers:

1

We're using TIBCO EMS from our ASP.NET 3.5 app for one interface to an external system, and it appears to be working just fine - except that the guys running the other side tells us we're racking up connections like crazy and never closing them....

What I'm doing is routing all TIBCO traffic through a single class with static member variables for both the TIBCO ConnectionFactory and the Connection itself, having been told that constructing them is pretty resource- and time-intensive:

private static ConnectionFactory Factory
{
    get
    {
        if (HttpContext.Current.Application["EMSConnectionFactory"] == null)
        {
           ConnectionFactory connectionFactory = CreateTibcoFactory();
           HttpContext.Current.Application["EMSConnectionFactory"] = connectionFactory;
        }

        return HttpContext.Current.Application["EMSConnectionFactory"] as ConnectionFactory;
    }
}

private static Connection EMSConnection
{
    get
    {
        if (HttpContext.Current.Application["EMSConnection"] == null)
        {
           Connection connection = Factory.CreateConnection(*username*,  *password*);
           connection.ExceptionHandler += new EMSExceptionHandler(TibcoConnectionExceptionHandler);
           connection.Start();
           HttpContext.Current.Application["EMSConnection"] = connection;
        }

        return HttpContext.Current.Application["EMSConnection"] as Connection;
     }
 }

Now my trouble is: where and how could I

  • tell the TIBCO connection to "auto-close" when no longer needed (like with the SqlConnection)
  • close the TIBCO connection on an error
  • close the TIBCO connection before our ASP.NET app finishes (or the user logs off)

I don't really seem to find much useful information on how to use TIBCO EMS from the C# / .NET world...... any takers?? Thanks!!

+1  A: 

Firstly, I don't understand how you could be running out of connections. Since you're storing the connection in the application, you should only have a single connection for the entire IIS application.

That put aside, I would do the following:

  • When the connection is retrieved, create the connection as you do now;
  • After you've created the connection, spin up a background thread;
  • Set a DateTime to DateTime.Now;
  • Let the background check (e.g. every second or every 10 seconds) what the difference is between the date you've set and DateTime.Now. If that's longer than a specific timeout, kill the connection and set Application["EMSConnectionFactory"] to null;
  • When the background thread kills the connection, close the background thread;
  • Every time the connection gets requested, reset the DateTimetoDateTime.Now`.

This way, the connections should be closed automatically.

Note that you will have to introduce locking. You can use Application.Lock() and Application.Unlock() for this.

Concerning closing on an error: I see that you've attached an exception handler to the connection instance. Can't you close the connection with that?

Pieter
Thanks for your thoughts - I agree, I was under the impression there ought to be a single connection myself - but web programming is somewhere between a black art and voodoo to me (I'm a Winforms and backend SQL Server programmer, mostly)
marc_s
There may be a concurrency issue here. You should at least surround the `if` statement in an `Application.Lock()` and `Application.Unlock()`. This is the same as `lock` in normal WinForms programming. Could you try logging the times you create a new connection? That may be a hint.
Pieter