如何检测到TadoConnection与服务器的通信丢失? [英] How can I detect that a TadoConnection lost the communication with the server?

查看:138
本文介绍了如何检测到TadoConnection与服务器的通信丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测何时 TAdoConnection 组件已经失去与服务器的连接。我尝试使用 OnDisconnect 事件,但这仅在调用Close方法或将Connected属性设置为false时触发。



我尝试过的另一个选项是使用 TTimer 并执行这样的查询



<$ p $在OnTimer事件中,选择1结果从DUAL

,捕获任何异常



有更好的选择来检测连接是否丢失?

解决方案

我看到了DUAL表。意味着你正在使用Oracle:)



对于大多数(所有?)客户端/服务器DBMS,没有办法检测到连接丢失,除了请求DBMS采取一些行动。而且有很多原因,为什么连接丢失了。可能是网络故障,可能是...,可能是DBA关闭了一个DB。



许多DBMS API(包括Oracle OCI)都有特殊功能,允许ping一个DBMS。 ping是对DBMS的最小可能请求。上述SELECT需要比这样的ping更多的工作。



但是并不是所有数据访问组件(包括ADO)都可以使用DBMS API ping调用来ping数据库。那么你必须使用一些SQL命令。所以,ADO上面的SELECT是正确的。其他选项 - BEGIN NULL;结束;。它可能使用较少的DBMS资源(无需优化器,无需描述结果集等)。



TTimer可以。该查询应该在使用相应连接的线程中执行。不是一个必须的,但这是一个不同的问题。



当连接丢失时,潜在的问题可能是关闭连接。由于连接关闭可能引起异常,因为DBMS API可能处于失败状态。



其中的种类...


I need to detect when a TAdoConnection component has lost the connection with the server. I've tried using the OnDisconnect event but this only fires when the Close method is called or the Connected property is set to false.

Another option I've tried is using a TTimer and executing a query like this

SELECT 1 RESULT FROM DUAL

in the OnTimer event, catching any exception that occurs.

Is there a better option to detect that the connection was lost?

解决方案

I see the DUAL table. Means, you are using the Oracle :)

For most (all ?) client / server DBMS's there is no way to detect, that a connection is losted, other than to ask a DBMS for some action. And there are a lot of reasons, why a connection is losted. May be a network failure, may be ..., may be a DBA shutdowned a DB.

Many DBMS API's, including Oracle OCI, have the special functions, allowing to ping a DBMS. The "ping" is a smallest possible request to a DBMS. The above SELECT requires much more job, than such ping.

But not all data access components, including ADO, allows to ping a DBMS, using the DBMS API ping call. Then you have to use some SQL command. So, the above SELECT is correct with ADO. Other option - BEGIN NULL; END;. It may be using less DBMS resources (no need for optimizer, no need to describe a result set, etc).

TTimer is OK. The query should be performed in a thread, where the corresponding connection is used. Not a must although, but it is a different issue.

The potential problem may be to close a connection, when a connection is losted. As a connection closing may raise an exception due to the DBMS API may be in a failed state.

Kind of that ...

这篇关于如何检测到TadoConnection与服务器的通信丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆