开发者

Auto-closing TIBCO EMS connections when no longer needed

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 Connecti开发者_Python百科on;
     }
 }

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!!


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜