因此,我正在运行一个简单的选择查询:
Private Function ReturnTableQuery(ByVal SQL As String) As DataTable
Dim rs As DataTable = New DataTable
Dim Adapter As NpgsqlDataAdapter = New NpgsqlDataAdapter
Try
If conn Is Nothing Then
ConnectDatabase()
End If
If conn.State <> ConnectionState.Open Then
ConnectDatabase()
End If
Adapter.SelectCommand = New NpgsqlCommand(SQL, conn)
Adapter.SelectCommand.CommandTimeout = 10
Adapter.Fill(rs)
Catch ex As Exception
PreserveStackTrace(ex)
Throw ex
End Try
Return rs
End Function
SQL 命令是:
Select id, application, datetimestamp, status, data, attemptcount from queue where application='reportengine' and status=4 and datetimestamp <= now() order by datetimestamp limit 1
我有时会返回 0 行。
如果我在程序中失败时在 pgAdmin 中运行完全相同的查询,它会按预期返回一行。
如果我关闭并重新打开连接,它可以工作,但我无法事先确定连接是否有任何问题。
我可以每次都重新打开连接,但我不想像我需要的那样频繁地重新创建连接。
我还遇到了间歇性错误,例如“未知的服务器响应”,我正在捕获并重新打开连接。
任何想法为什么连接如此脆弱,是否有一种廉价的方法来检查实际连接状态?
谢谢,布拉德