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

  • SEARCH
  • Home
  • 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 718953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:35:13+00:00 2026-05-14T05:35:13+00:00

Here is an example of my code in a DAL. All calls to the

  • 0

Here is an example of my code in a DAL. All calls to the database’s Stored Procedures are structured this way, and there is no in-line SQL.

Friend Shared Function Save(ByVal s As MyClass) As Boolean

    Dim cn As SqlClient.SqlConnection = Dal.Connections.MyAppConnection
    Dim cmd As New SqlClient.SqlCommand

    Try
        cmd.Connection = cn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "proc_save_my_class"

        cmd.Parameters.AddWithValue("@param1", s.Foo)
        cmd.Parameters.AddWithValue("@param2", s.Bar)

        Return True

    Finally
        Dal.Utility.CleanupAdoObjects(cmd, cn)
    End Try

End Function

Here is the Connection factory (if I am using the correct term):

Friend Shared Function MyAppConnection() As SqlClient.SqlConnection

    Dim cn As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ToString)
    cn.Open()

    If cn.State <> ConnectionState.Open Then
        ' CriticalException is a custom object inheriting from Exception.
        Throw New CriticalException("Could not connect to the database.")
    Else
        Return cn
    End If

End Function

Here is the Dal.Utility.CleaupAdoObjects() function:

Friend Shared Sub CleanupAdoObjects(ByVal cmd As SqlCommand, ByVal cn As SqlConnection)
    If cmd IsNot Nothing Then cmd.Dispose()
    If cn IsNot Nothing AndAlso cn.State <> ConnectionState.Closed Then cn.Close()
End Sub

I am getting a lot of “Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.” error messages reported by the users. The application’s DAL opens a connection, reads or saves data, and closes it. No connections are ever left open – intentionally!

There is nothing obvious on the Windows 2000 Server hosting the SQL Server 2000 that would indicate a problem. Nothing in the Event Logs and nothing in the SQL Server logs.

The timeouts happen randomly – I cannot reproduce. It happens early in the day with only 1 to 5 users in the system. It also happens with around 50 users in the system. The most connections to SQL Server via Performance Monitor, for all databases, has been about 74.

The timeouts happen in code that both saves to, and reads from, the database in different parts of the application. The stack trace does not point to one or two offending DAL functions. It’s happened in many different places.

Does my ADO.NET code appear to be able to leak connections? I’ve goolged around a bit, and I’ve read that if the connection pool fills up, this can happen. However, I’m not explicitly setting any connection pooling. I’ve even tried to increase the Connection Timeout in the connection string, but timeouts happen long before the 300 second (5 minute) value:

<add name="MyConnectionString" connectionString="Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=SSPI;Connection Timeout=300;"/>

I’m at a total loss already as to what is causing these Timeout issues. Any ideas are appreciated.

EDIT: This is a WinForms application.

  • 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-14T05:35:13+00:00Added an answer on May 14, 2026 at 5:35 am

    From here:

    Unlike Finalize, developers should call Dispose explicitly to free unmanaged resources. In fact, you should call the Dispose method explicitly on any object that implements it to free any unmanaged resources for which the object may be holding references.

    SqlConnection, SqlCommand, SqlDataReader, etc… all implement IDisposable. If you enclose all these instances in a Using block, then Dispose will be called automatically, your connections will be closed, and you won’t have to worry about issues like this. Fire up Reflector and take a look for yourself: (SqlConnection.Dispose)

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            this._userConnectionOptions = null;
            this._poolGroup = null;
            this.Close();
        }
        this.DisposeMe(disposing);
        base.Dispose(disposing);
    }
    

    This also makes the code shorter in that you don’t have to manually add a Finally block to clean up your ADO.NET objects.

    Using connection As SqlConnection("your connection string")
        Using command As New SqlCommand("your sql", connection)
            connection.Open()
            Using dataReader As SqlDataReader = command.ExecuteReader()
                'Your stuff here
            End Using
        End Using
    End Using
    

    Using the Using approach forces you to keep your ADO.NET objects local, which to me is a good thing.

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

Sidebar

Related Questions

here is my example code: Public Class Parent Private _TestProperty As String Private WithEvents
I'm looking for something like break for loops. Here's some example code (using Symfony's
Here's an example: Double d = (1/3); System.out.println(d); This returns 0, not 0.33333... as
Here is an example that a forum poster gave, I can't tell if this
OK, probably best to give an example here of what I mean. Imagine a
Here is an example of what I've got going on: CREATE TABLE Parent (id
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
Here is an example: <h:outputText value=#{myBean.myMoney}> <f:convertNumber type=currency currencySymbol=$ /> </h:outputText> Given that I
Here's an example: >git status # On branch master nothing to commit (working directory
Here's an example of the query I'm trying to convert to LINQ: SELECT *

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.