473,614 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ADO.Net thread safety question

Hi,

I have a question that I wonder if someone might be able to help me with...

I have an application which handles real-time financial data from a third
party source. The data comes in via events which are fired on an arbitrary
thread, and I then take the data, generate update commands for a SQL Server
database, and add them to a queue using a lock on a sync object to ensure
thread safety when writing to the queue.

I then have a background worker thread which watches the queue and executes
the queries inside it sequentially. Again, the reading and dequeuing is
synchronised with the writing.

So far, so good.

I also have a need to execute some other, fairly slow running, queries
against the database. These queries will return results and will be running
in an arbitrary thread. The question I have is about thread safety around
the SQLConnection object. So far, I've avoided locking on it, as I only
have one thread (the worker) accessing it at any point.

Do I need to synchronise access to the connection object? If so, can I open
a separate connection and sync that, leaving the first connection free to
process the updates, or will connection pooling interfere and give me any
problems?

Thanks in advance for any advice you may have,

James Cane

ps. .Net 2.0, C#, SQL Server 2005.

Oct 2 '06 #1
5 3402

jzlondon wrote:
Hi,

I have a question that I wonder if someone might be able to help me with...

I have an application which handles real-time financial data from a third
party source. The data comes in via events which are fired on an arbitrary
thread, and I then take the data, generate update commands for a SQL Server
database, and add them to a queue using a lock on a sync object to ensure
thread safety when writing to the queue.

I then have a background worker thread which watches the queue and executes
the queries inside it sequentially. Again, the reading and dequeuing is
synchronised with the writing.

So far, so good.

I also have a need to execute some other, fairly slow running, queries
against the database. These queries will return results and will be running
in an arbitrary thread. The question I have is about thread safety around
the SQLConnection object. So far, I've avoided locking on it, as I only
have one thread (the worker) accessing it at any point.

Do I need to synchronise access to the connection object? If so, can I open
a separate connection and sync that, leaving the first connection free to
process the updates, or will connection pooling interfere and give me any
problems?

Thanks in advance for any advice you may have,

James Cane

ps. .Net 2.0, C#, SQL Server 2005.

Hi,
Just for precaution, I think you should sync the connection
object. Actually, I faced a lot of weird errors which originated from
these kind of thread sync problem. The queries don't execute when such
exceptions occur. As you told that the application was related to some
financial domain, I would stress on synchronizing the connection
object.
If you don't want to synchronize connection object then, you will
have to open create and use new connection object each time when you
want to fire some query. To read more about my personal experience
about the exceptions please visit my blog -
http://anants-blog.blogspot.com/

Hope you don't get such kind of errors !

Thanks !

Oct 2 '06 #2

jzlondon wrote:
Do I need to synchronise access to the connection object? If so, can I open
a separate connection and sync that, leaving the first connection free to
process the updates, or will connection pooling interfere and give me any
problems?
James,

You need to synchronize access to a SqlConnection object only when it
is accessed from more than one thread. The presence of a connection
pool does not change that rule. If you have gotten in the habit of
ensuring that all SqlConnection objects are local to a method then
you're fine.

Brian

Oct 2 '06 #3
Hi Brian,

Thanks. It will be accessed from more than one thread.

The reason for this is that I'm reusing a connection to save some overhead
(at peak times, I can be generating almost 1000 database updates per
second).

The reason I was asking about the thread safety with connection pooling is
that, if I'm not mistaken, connection pooling works by allocating a
pre-existing connection when you request a new one. My concern is that two
separate threads could request a new connection, but each could receive the
same connection from the pool.

In this case, you'd think there'd be no need to sync the connection, as it's
local to the method, but it actually represents an underlying shared object
and concurrency issues may arise.
"Brian Gideon" <br*********@ya hoo.comwrote in message
news:11******** ************@k7 0g2000cwa.googl egroups.com...
>
jzlondon wrote:
>Do I need to synchronise access to the connection object? If so, can I
open
a separate connection and sync that, leaving the first connection free to
process the updates, or will connection pooling interfere and give me any
problems?

James,

You need to synchronize access to a SqlConnection object only when it
is accessed from more than one thread. The presence of a connection
pool does not change that rule. If you have gotten in the habit of
ensuring that all SqlConnection objects are local to a method then
you're fine.

Brian

Oct 2 '06 #4

JamesC wrote:
Hi Brian,

Thanks. It will be accessed from more than one thread.
Then you definitely need to synchronize access to it.
>
The reason for this is that I'm reusing a connection to save some overhead
(at peak times, I can be generating almost 1000 database updates per
second).

The reason I was asking about the thread safety with connection pooling is
that, if I'm not mistaken, connection pooling works by allocating a
pre-existing connection when you request a new one. My concern is that two
separate threads could request a new connection, but each could receive the
same connection from the pool.
Basically what happens is that Open attempts to acquire a connection
from the pool. Once the connection is acquired it is removed (or
marked as unavailable). If no connection is available then a new one
is established. When Close or Dispose is called the connection is then
returned (or marked as available) to the pool. The connection pool
manager itself is thread-safe so two simultaneous calls to Open from
two different SqlConnection objects will race for an available
connection from the pool, but only one will win resulting in the other
having to establish a brand new connection. In other words, two
different SqlConnection objects cannot simultaneously hold the same
underlying connection.

Oct 2 '06 #5
That's awesome. Not only is that a clear and knowledgeable answer, it's
also the answer I was hoping for!

Thanks Brian

James

"Brian Gideon" <br*********@ya hoo.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>
JamesC wrote:
>Hi Brian,

Thanks. It will be accessed from more than one thread.

Then you definitely need to synchronize access to it.
>>
The reason for this is that I'm reusing a connection to save some
overhead
(at peak times, I can be generating almost 1000 database updates per
second).

The reason I was asking about the thread safety with connection pooling
is
that, if I'm not mistaken, connection pooling works by allocating a
pre-existing connection when you request a new one. My concern is that
two
separate threads could request a new connection, but each could receive
the
same connection from the pool.

Basically what happens is that Open attempts to acquire a connection
from the pool. Once the connection is acquired it is removed (or
marked as unavailable). If no connection is available then a new one
is established. When Close or Dispose is called the connection is then
returned (or marked as available) to the pool. The connection pool
manager itself is thread-safe so two simultaneous calls to Open from
two different SqlConnection objects will race for an available
connection from the pool, but only one will win resulting in the other
having to establish a brand new connection. In other words, two
different SqlConnection objects cannot simultaneously hold the same
underlying connection.

Oct 2 '06 #6

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

Similar topics

9
6008
by: rnn98 | last post by:
hi, my multithread application, running under solaris box, is crashing eventually. I tried to spot and substitute functions not "thread safe", but I guess my search wasn't good enough. I have put localtime_r and asctime_r where I was using localtime and asctime. But I didn't find any correspondent _r function to "time".Also, I found that "readdir" is not thread safe. But what about "scandir"? Thanks for any help
4
6640
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause potential problems for thread-safety. So far, I'm only confused. I need a proper explanation for the concept so I can understand how to write thread-safe functions in the future. My apologies for posting a long routine.
9
2082
by: Alexander Fleck | last post by:
Hi, I' ve to make a software module thread safe. I know how to realize that and what' re the main topics of thread safety. But I don' t know how thread safety can be tested. I read about a test for web servers. These apps' re tested with a stress test. That doesn' t work for my module. I searched the web but didn' t find a solution that satisfies me. I think that thread safety errors don' t occur reproduceable and so they' re hard to test and...
17
5315
by: Rainer Queck | last post by:
Hi NG, one more question about thread safety of generic lists. Let's assume a generic list: List<MyTyp> aList = new List<MyType>(); Would it be a problem if one thread removes elements from the list like MyTyp x = aList; aList.Remove(x);
6
3132
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
13
3580
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
6
3873
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or variables, just static members static void FirstFunction( args & ); static void SecondFunction( args & ); static void ThirdFunction( args & );
13
11449
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
2
2191
by: k3xji | last post by:
Hi all, This will probably be a long question/short answer, sorry, but I have wandered net about the subject and really feel cannot find just enough information.I want to ask my question by giving an example to explicitly express my understanding which may be also wrong: So, let's have a string in a module like: st = 'IAmAstring' My understanding: Python allocates a string object and binds the st to
0
8197
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8287
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8443
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5548
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2573
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.