Results 1 to 2 of 2

Thread: Closing database connections in 3 tier

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Closing database connections in 3 tier

    Im a big fan of 3 tier architecture. As a result of that I perform all my db readings in the datalayer... And when a reading is finished or the reading resulted in error I always close my connection
    Is this the best way to do it? Either before I return my data or in the finally statement:

    VB Code:
    1. If Not myConnectionOra Is Nothing AndAlso myConnectionOra.State = ConnectionState.Open Then
    2.                 myConnectionOra.Close()
    3.             End If

    kind regards
    Henrik

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I find it best to use a Try Catch block for data access and the close the connection in the Finally block. The Finally block executes at the end of normal execution or before exception moves on if there is an exception so it is a good fail safe way of making sure you clean up your connection.

    VB Code:
    1. Dim cnn As SQLConnection
    2. Try
    3.   cnn=New SQLConnection("myconnectionstring")
    4.   cnn.Open()
    5.   'my data operation
    6. Catch ex As Exception
    7.   'my exception handling
    8. Finally
    9.   If Not cnn Is Nothing AndAlso cnn.State=Open Then
    10.      cnn.Close()
    11.   End If
    12. End Try

    PS This is a good idea regardless of the Tiers you use or using the Application Blocks are good as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width