Bra*_*ews 2 postgresql ado.net npgsql
因此,我正在运行一个简单的选择查询:
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
Run Code Online (Sandbox Code Playgroud)
SQL命令是:
Select id, application, datetimestamp, status, data, attemptcount from queue where application='reportengine' and status=4 and datetimestamp <= now() order by datetimestamp limit 1
Run Code Online (Sandbox Code Playgroud)
我有时会返回0行。
如果我在程序中失败时在pgAdmin中运行完全相同的查询,它将返回预期的行。
如果我关闭并重新打开该连接,它将起作用,但我无法事先确定该连接是否有问题。
我可以每次都重新打开连接,但是我不想按需要的频率重新创建连接。
我还收到间歇性错误,例如“未知服务器响应”,该错误正在捕获并重新打开连接。
有什么想法为什么连接如此脆弱并且有一种便宜的方法来检查实际的连接状态吗?
Thx,布拉德
您是否在多个线程之间共享连接?与其他数据提供程序一样,Npgsql也不是线程安全的。我认为这是造成您问题的最可能原因。您应该打开一个连接,使用它,然后再关闭它。使用连接池时,这是最可扩展的模式。希望对您有所帮助。