Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:17:18+00:00 2026-05-28T08:17:18+00:00

I have the below mentioned code in a seperate class file for establishing connection

  • 0

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 ConnectDB() in a loop without calling DisconnectDB(). But I expected that the condition OraConn.State = ConnectionState.Closed 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?

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

The class is refered in other classes or directly in the aspx.vb pages like this.

Public Function InsertData(ByVal var1 As String, ByVal var2 As String) As Integer
    conn.ConnectDB()
    Dim qryInsert As String

    qryInsert = " INSERT INTO TABLE VALUES ('" & var1 & "', " 
    qryInsert = qryInsert & var2 & "')"        

    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

The connection is again opened in function updData(). 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 If OraConn.State = ConnectionState.Closed is not helping.

UPDATE

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?

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:17:18+00:00Added an answer on May 28, 2026 at 8:17 am

    You must close all the connections as soon as you are done with it, no matter what.

    Suggestion:

    The best practice for closing the connection is to do it in finally block. So that even if there is any error, catch it (log it if required) in catch block, and then connection will get close in finally block.

    UPDATE

    You can put one private static counter in your Connection class. When ever ConnectDB() is called you increment this counter and decrement it in every DisconnectDB(). Now in ConnectDB() you check the value of counter, if it exceeds a minimum threshold you throw error, by doing this way; you can come to know idle connection present in your code and refactor it. On production keep this threshold value high or ignore it in code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a part of code mentioned like below. //Reading from a file and
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have a parent class and child class as mentioned below. Ext.ns(MyUi); MyUi.ParentPanel =
As mentioned below I have made changes to the code which looks like following
I have used code described on below mentioned SO answer Change the image source
I have tried using the code mentioned in the link below to mass disable
I am facing an issue here as shown in code is mentioned below Inside
I have a simple requirement as mentioned below: A ListView or any control displays
I have a C++ array declared as mentioned below: CString carray[] = { A,
I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.