Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Framework Data Provider for Oracle multiple open connection
    primarykey
    data
    text
    <p>I have the below mentioned code in a seperate class file for establishing connection and carry out DB transactions. I have an issue where multiple connections being opened which sometime exceed the connection pool. When I stepped through the code I found that there are codes which call <code>ConnectDB()</code> in a loop without calling <code>DisconnectDB()</code>. But I expected that the condition <code>OraConn.State = ConnectionState.Closed</code> should handle the situation. Somehow the condition is always satisfied hence openning another set of connection. Can you suggest where am I going wrong and also what best practice can be adopted here?</p> <pre><code>Public Class Connection Dim Str_conn As String = "Data Source=...; User=...; password=...; Min Pool Size=10; Max Pool Size=500;" Public OraConn As OracleConnection Dim cmd As OracleCommand Dim dr As OracleDataReader Dim data_adapt As OracleDataAdapter Dim dt As DataTable Dim ds As DataSet Public Sub ConnectDB() OraConn = New OracleConnection(Str_conn) If OraConn.State = ConnectionState.Closed Then OraConn.Open() End If End Sub Public Sub DisconnectDB() If OraConn.State = ConnectionState.Open Then OraConn.Close() End If End Sub Public Function get_dataset(ByVal query As String, ByRef ds As DataSet) As DataSet data_adapt = New OracleDataAdapter(query, OraConn) data_adapt.Fill(ds) Return ds End Function Public Function get_datareader(ByVal query As String) As OracleDataReader cmd = New OracleCommand(query, OraConn) dr = cmd.ExecuteReader() Return dr End Function Public Sub UpdateDB(ByVal query As String) cmd = New OracleCommand(query, OraConn) cmd.ExecuteNonQuery() cmd.Dispose() End Sub </code></pre> <p>The class is refered in other classes or directly in the aspx.vb pages like this.</p> <pre><code>Public Function InsertData(ByVal var1 As String, ByVal var2 As String) As Integer conn.ConnectDB() Dim qryInsert As String qryInsert = " INSERT INTO TABLE VALUES ('" &amp; var1 &amp; "', " qryInsert = qryInsert &amp; var2 &amp; "')" Try conn.UpdateDB(qryInsert) Catch ex As OracleException If ex.Code = 1 Then updData(var1, var2) ElseIf ex.Code = 2091 Then msgprompt("Duplicate Unique Key!", "Warning") End If Finally conn.DisconnectDB() End Try Return count End Function </code></pre> <p>The connection is again opened in function <code>updData()</code>. While I understand that it has to be closed correctly but keeping tab on every developer is not possible. Hence I want to control it directly from the connection class by using the same connection but the condition <code>If OraConn.State = ConnectionState.Closed</code> is not helping.</p> <p><strong>UPDATE</strong></p> <p>I have put the code in UpdateDB under a Using block and removed call to ConnectDB and DisconnectDB from function like InsertData(...). It seems that the issue has been resolved. But I would like to know in case of exception will the connection remain open? and also OraConn is a public variable defined outside Using block so will it be disposed of by the GC?</p> <pre><code>Public Sub UpdateDB(ByVal query As String) Using OraConn = New OracleConnection(Str_conn) cmd = New OracleCommand(query, OraConn) Try OraConn.Open() cmd.ExecuteNonQuery() Catch ex As Exception Throw Finally cmd.Dispose() End Try End Using End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload