不再需要时自动关闭 TIBCO EMS 连接

发布于 2024-09-29 06:03:54 字数 1456 浏览 4 评论 0原文

我们正在使用 ASP.NET 3.5 应用程序中的 TIBCO EMS 作为与外部系统的一个接口,它似乎工作得很好 - 除了运行另一端的人告诉我们,我们正在疯狂地建立连接,而且从来没有关闭它们......

我正在做的是通过一个类路由所有 TIBCO 流量,该类具有 TIBCO ConnectionFactoryConnection 本身的静态成员变量,告诉我构建它们非常耗费资源和时间:

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;
     }
 }

现在我的麻烦是:在哪里以及如何

  • 告诉 TIBCO 连接在不再需要时“自动关闭”(例如使用 SqlConnection
  • 发生错误时关闭 TIBCO 连接
  • 在我们的 ASP.NET 应用程序完成(或用户注销)之前关闭 TIBCO 连接

我似乎没有真正找到有关如何从 C# / .NET 世界使用 TIBCO EMS 的有用信息。 ......有接受者吗?谢谢!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-10-06 06:03:54

首先,我不明白你怎么会耗尽连接。由于您将连接存储在应用程序中,因此整个 IIS 应用程序应该只有一个连接。

抛开这一点,我会执行以下操作:

  • 检索连接时,像现在一样创建连接;
  • 创建连接后,启动后台线程;
  • DateTime 设置为 DateTime.Now
  • 让后台检查(例如每秒或每 10 秒)您设置的日期与 DateTime.Now 之间的差异。如果超过特定超时,则终止连接并将 Application["EMSConnectionFactory"] 设置为 null;
  • 当后台线程杀死连接时,关闭后台线程;
  • 每次请求连接时,将 DateTime重置为DateTime.Now`。

这样,连接应该自动关闭。

请注意,您必须引入锁定。您可以使用 Application.Lock()Application.Unlock() 来实现此目的。

关于关闭错误:我看到您已将异常处理程序附加到连接实例。你不能关闭它的连接吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文