我使用PostgreSQL 9.5和Npgsql 2.2.5.0从用VB.NET编写的ASP.NET Web服务连接到PostgreSQL服务器。
网络服务每秒大约收到15个请求。
我使用的连接字符串是:
<connectionStrings>
<add name="PostgreConnectionStringPCMRead"
connectionString="Server=xxx.xxx.xxx.xxx;Port=5432;User
Id=the_user;Password=the_password;Database=example;
CommandTimeout=600;Timeout=30;ConnectionLifeTime=30;
Pooling=False;"/>
</connectionStrings>
当我查看Windows资源监视器时,我看到数百个终止连接。
我意识到我正在使用Pooling = False,但是当我设置Pooling = True时,我从池中获取连接时出现错误&#34;超时。&#34;
对于每个查询,我都使用以下类(我只剩下基本代码):
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 objDBRead As New dlNpgSQL("connstring")
tmpSQL = "SELECT * FROM table1"
ds = objDBRead.GetDataSet(tmpSQL)
If (objDBRead IsNot Nothing) Then
objDBRead.CloseConnection()
End If
很明显,我打开和关闭每个查询的连接。
这是正确的做事方式吗?
我是否应该使用池,如果是,为什么我会收到错误以及如何解决问题?
谢谢!