473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SqlConnection and threads

I have an SqlConnection object defined as a member of main form. I also have
a thread that creates an SqlDataReader but uses the same SqlConnection
object. The SqlDataReader simple loops round for each row in the result set.

The problem I have is that after running the thread many times, the
SqlConnection object simply closes the connection to the database (picked up
from the StateChange event). No exceptions are generated. Sometimes the
thread can be run a dozen times before this happens, sometime it take 50+
times.

Is this a threading issue? Are there any special requirements I have to
consider to allow a thread to use an SqlConnection object that isn't defined
as a member of the thread?

From the thread I don't actually call any SqlConnection methods directly but
the SqlDataReader must do - do I have to syncronize all SqlDataReader method
calls using a delegate? If so, I can see this slowing everything down. Do I
have to give each thread it's own SqlConnection to the database?

--
Best regards
Mark
Nov 17 '05 #1
1 2369
> I have an SqlConnection object defined as a member of main form. I also
have
a thread that creates an SqlDataReader but uses the same SqlConnection
object. The SqlDataReader simple loops round for each row in the result set.
The problem I have is that after running the thread many times, the
SqlConnection object simply closes the connection to the database (picked up from the StateChange event). No exceptions are generated. Sometimes the
thread can be run a dozen times before this happens, sometime it take 50+
times.

Is this a threading issue? Are there any special requirements I have to
consider to allow a thread to use an SqlConnection object that isn't defined as a member of the thread?

From the thread I don't actually call any SqlConnection methods directly but the SqlDataReader must do - do I have to syncronize all SqlDataReader method calls using a delegate? If so, I can see this slowing everything down. Do I have to give each thread it's own SqlConnection to the database?


I'd considered option to open SQL connection only before and close it
immediatelly after use. SqlConnection implementation caches database
connections internaly.
--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: www.nesterovsky-bros.com
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donnie Darko | last post by:
I'm trying to understand SqlConnection(), SqlCommand() For example. I use SqlConnection() as a parameter when I create a new SqlCommand. Then I open SqlConnection(), then I execute the...
16
by: ed_p | last post by:
Hello, I have implemented the singleton pattern for a class to hold a SqlConnection object that will be used thruout the application to create commands. My application is a simple Windows Form...
11
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
3
by: Catadmin | last post by:
Hello, Everyone. I have a strange one here. I'm a DBA with relatively low program experience. Recently I was given an assignment to locate why our SQL Server has "orphaned" connections. The...
4
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
1
by: Daniel | last post by:
are System.Data.SqlClient.SqlConnection thread safe? can many threads share a System.Data.SqlClient.SqlConnection instance without any synchronization?
3
by: pratham | last post by:
Hi and regards What is best method to have an connection to sql, iam using sqlserver 1. have an sqlconnection open when the application start event in global.asax so that i dont have to open...
4
by: Victor | last post by:
Hi Guys I have a problem here. I want to improve the performance for a website. When I looked into the system, I have found that the system made the "SqlConnection Object" static. That mean only...
9
by: =?Utf-8?B?VE9NX1Bhc2FkZW5h?= | last post by:
Hello, In my ASP.Net app I'm launching a BackgroundWorker thread in my Page_Load function. In that thread I'm attempting to connect to a SQL server using this connection string "Initial...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.