我已经使用npgsql几年了。
直到最近PostgreSQL服务器上的负载急剧增加时,我再也没有遇到过问题。
此刻每秒我的服务器上都有数百个postgres.exe“正在运行”和“正在终止”的实例。
当我打开池时,它与创建/终止的实例数量没有关系,几分钟后,我得到一个错误:
The connection pool has been exhausted, either raise MaxPoolSize (currently 100) or Timeout (currently 15 seconds)
我现在在问我使用npgsql的方法是否正确。
我使用nuget将npgsql添加到我的项目中,并创建了以下类,该类在每次运行查询时都会使用:
Public Class dlNpgSQL
Dim _sqlCommand As NpgsqlCommand
Dim _sqlDataAdapter As NpgsqlDataAdapter
Dim _dataset As DataSet
Public Sub New()
_sqlConnection = New NpgsqlConnection(ConfigurationManager.
ConnectionStrings("connstring").ConnectionString)
End Sub
Public Function GetDataSet(ByVal strQuery As String) As DataSet
_dataset = New DataSet
_sqlDataAdapter = New NpgsqlDataAdapter(strQuery, OpenConnection)
_sqlDataAdapter.Fill(_dataset)
Return _dataset
End Function
Public Function OpenConnection() As NpgsqlConnection
If _sqlConnection.State = ConnectionState.Closed Then
_sqlConnection.Open()
End If
End Function
Public Sub CloseConnection()
If _sqlConnection.State = ConnectionState.Open Then
_sqlConnection.Close()
End If
End Function
End Class
我运行查询如下:
Dim ds As DataSet
Dim objDB As New dlNpgSQL("connstring")
tmpSQL = "SELECT * FROM table1"
Try
ds = objDB.GetDataSet(tmpSQL)
Catch ex As Exception
If (objDB IsNot Nothing) Then
objDB.CloseConnection()
End If
Return Nothing
Finally
If (objDB IsNot Nothing) Then
objDB.CloseConnection()
End If
End Try
我使用的连接字符串是:
<connectionStrings>
<add name = "connstring" connectionString="Server=localhost;Port=5432;User Id=postgres;Password=password;Database=test_db;CommandTimeout=600;Pooling=True;"/>
</connectionStrings>
所以我的问题是:
我已经为此奋斗了好几天-任何帮助将不胜感激!