473,326 Members | 2,182 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,326 software developers and data experts.

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 3375

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*********@yahoo.comwrote in message
news:11********************@k70g2000cwa.googlegrou ps.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*********@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.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
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...
4
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...
9
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...
17
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...
6
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...
13
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
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...
13
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.