Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program which connects a user to a database via an application role as follows.

VB
ConnectionString = "WW"
con1.ConnectionString = connectionString
con1.Open()
cmd.Connection = con1
Call GetPermissions()
cmd.CommandText = "sp_setapprole"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@rolename", "XX")
cmd.Parameters.AddWithValue("@password", "XX")
cmd.ExecuteNonQuery()


Between 13:30 and 14:00 I want to terminate the connection to the database then display a messagebox telling the user the GUI is unavailable.
The reason is to allow housekeeping jobs to run on the database.
I do so using this code.

VB
con1.Close()
GC.Collect()
MessageBox.Show("Database housekeeping job in progress.  GUI unavailable")
End


However when looking at the activity monitor on the database it seems as though the connection is only terminated after the application ends. Alternatively it may take a few minutes for GC.collect to work.
Therefore at the time that the message box appears the connection is still active.
How can I make sure the connection has terminated before the messagebox appears.

[Modified: added the pre tags for code formatting...pre tags are your friend...]
Posted
Updated 2-Nov-10 5:58am
v2
Comments
William Winner 2-Nov-10 11:59am    
Not sure I can answer your question, but I'm curious as to what you think keeping the connection open does...that's not the standard way to handle database operations.

By default ADO.net Connection object uses connection pooling

"ADO.NET 2.0 introduced two new methods to clear the pool: ClearAllPools and ClearPool. ClearAllPools clears the connection pools for a given provider, and ClearPool clears the connection pool that is associated with a specific connection. If there are connections being used at the time of the call, they are marked appropriately. When they are closed, they are discarded instead of being returned to the pool."

taken from this article MSDN: SQL Server Connection Pooling ADO.NET[^]
 
Share this answer
 
v2
You do not need to call GC.Collect at all. Doing so screws with the internal tuning of the GC and can actually degrade the performance of your app.
 
Share this answer
 
There is an internal connection pool which holds connections to database opened for some time. So look for a way how to reinitialize this pool (if possible).
 
Share this answer
 
Thanks. I set pooling=false in the config file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900