C# Question about thread safety - c#

I'm using a threadpool to do some heavy processing and also bits of sql. Currently I open sql connections when I need them, run a query and then close them. This works fine. The application has been running with no issues. As more work is being done by this application it's using more threads. More threads mean more opening/closing of SQL connections. In SQL 2005 this actually hammers the server. My test server is doing around 175 transactions / sec. Approx 150 of these are being run in the master database and are "ValidateSQLLogin".
I'm going to change the app so that each thread has it's own connection and then this connection is passed around the thread.
So my question is:
If a SQL connection object is created locally in a thread and is then passed by ref to a static function of another class would this be unsafe?
void ThreadA()
{
SqlConnection a = new SqlConnection(....);
MyStaticClass.DoStuff(ref a);
}
void ThreadB()
{
SqlConnection b = new SqlConnection(....);
MyStaticClass.DoStuff(ref b);
}
static void MyStaticClass.DoStuff(ref SqlConnection sql)
{
// Do stuff with sql
}
My initial thought is that it would be unsafe as 10 threads could all call the same static function at the same time, each passing their own connection object.
Previously the static functions opened their own connections and closed them when they were done.
If it is unsafe whats the best method to work round this. I need to try and minimize open/close of Sql connections.
Thanks
Gareth

A parameter to a static function is not the same as a static field. Each execution of the static function will use different copy of the connection. You don't even need to have the parameter as reference (you need reference parameters only if you want to change them).

Each thread has it's own stack, and parameters and local variables are stored on the stack, so there is no problem having several threads calling the same static method. Each method call has it's own parameters and local variables.
There is no reason to pass the SqlConnection by reference to the method, though.

As said by others, calling a static method from multiple threads is not in itself a danger. However, if the static method modifies/accesses static fields* instead of just working with the parameters you pass it, you will need to make it thread-safe.
exception: some value types use atomic operations for access/writing and are implicitly thread-safe for those operations. However, race conditions may still occur in the logic that retrieves and updates these.

If you have that load of transactions I would recommend to keep one static connection, that remains open while you have this transactions, and when, the load greatly decreases or is null, it automatically closes, and with new requests it opens again.

Try the following:
You could use the lock statement to prevent other threads from entering the code block where you are using this connection, the other threads are automatically waiting till the previous thread is done and then enter this segment. Just make sure that the object you are locking is private to your static class.

In .Net all the static members are thread-safe.

Related

Are 'using' blocks thread safe?

If I am calling some data access methods from multiple threads, do I need to lock the code around the DB calls to ensure consistency, or are the using statements below atomic?
public static DataRow GetData(Int32 id)
{
using (SqlConnection con = new SqlConnection(connectionString);)
{
con.Open();
SqlCommand cmd = ...
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(...)
cmd.Parameters.Add(...)
DataTable dt = new DataTable();
return new SqlDataAdapter(cmd).FillWithRetry(dt, sqlGetEmail.CommandText);
}
}
I don't think one thread can affect the connection object defined and 'in use' from another.
Using statements have nothing to do with thread-safety (or lack of).
They merely ensure that Dispose method of the used object is called when the block ends; but are otherwise equivalent to a manual try..finally Dispose.
In this particular case: since a new connection is opened on each thread then it is 'thread safe'. It still might not be atomic wrt. the database or other shared state.
I don't think one thread can affect the connection object defined and 'in use' from another.
So I suppose you are worrying your connection's concurrent access. The using statement is not a lock. If you want to use exclusively a connection or any other instance you should use:
lock(myConnection)
{
// your code
}
What the using keyword is for that's described here
However I think there is other misunderstandings here:. In your example your connection is a local variable what is instantiated as many times as the control flow enters to your GetData method (even in different threads). So even the control flow reenters multiple times (and in different threads) to the method, no shared connection instance will be used, each entering to the method creates its own instance.
It would be different the case if the connection instance would be a parameter. Then you should worry about concurrency, and use a lock.
Conclusion: In your sample you do NOT need to worry about your connection instance concurrent access, and you do not need to use any locking semantics.
Interestingly in your sample using of using is indeed correct, because Connection is IDisposable, so you should apply a deterministic dispose guard around its usage with try/finally or better with its shortcut: the using keyword.

It is ok to store a Thread in a static variable?

I want to make sure that I always create only one instance of a Thread so I built this:
private static volatile Thread mdmFetchThread = null;
private static object Locker = new object();
public void myMethod(){
string someParameter = getParameterDynamically();
lock(Locker)
{
// If an mdmFetchThread is already running, we do not start a new one.
if(mdmFetchThread != null && mdmFetchThread.ThreadState != ThreadState.Stopped)
{
// warn...
}
else
{
mdmFetchThread = new Thread(() => { doStuff(someParameter); });
mdmFetchThread.Start();
}
}
}
Is this ok to do or what could be possible pitfalls?
//Edit: As requested below a bit context: doStuff() is calling some external system. This call might timeout but I cant specify the timeout. So I call it in mdmFetchThread and do a mdmFetchThread.join(20000) later. To avoid that I call the external system twice, I created the static variable so that I can check if a call is currently ongoing.
Storing a thread in a static variable is OK (if you need at most one such thread per AppDomain). You can store whatever you want in static storage.
The condition mdmFetchThread.ThreadState != ThreadState.Stopped is racy. You might find it to be false 1 nanosecond before the thread exits. Then you accidentally do nothing. Maintain your own boolean status variable and synchronize properly. Abandon volatile because it is more complicated than necessary.
Consider switching to Task. It is more modern. Less pitfalls.
Consider using a Lazy<Task> to create the singleton behavior you want.
Add error handling. A crash in a background thread terminates the process without notifying the developer of the error.
Generally speaking if you are using statics to store state (such as a thread), then you might have a design flaw when attempting to scale out or when trying to manage the lifetime of the object. I usually try to avoid statics whenever possible.
An alternative might be to create a class that only manages a single thread to perform your task as an instance. This class might be responsible for passing data to your Thread or managing the state of it. For example, ensuring it is only run once, stopping the thread gracefully, or handling when the thread completes. If you wanted to scale out, then you'd just create multiple instances of your class each with their own thread that they manage. If you only wanted one, then just pass around a single instance.
If you're looking for ways to make this instance available to your entire application (which is usually the issue people are trying to solve when using static variables), then take a look into patterns like using ServiceContainers and IServiceProvider.

LINQ to SQL and open connections

I've always had in my data access layer the following kind of code setup (made up example)
public static class LoadData
{
private static SomeDataContext db = new SomeDataContext();
public static void LoadData(DropDownList ddl)
{
(from ls in db.MyLookup
select ls).OrderBy(ls=>ls.theId).ToList()
.ForEach(ls=>ddl.Items.Add(new ListItem(ls.theText, ls.theValue.ToString())));
}
}
Is the DataContext "smart" enough to cleanup after itself or should I be wrapping my query with a using statement to make sure connections are closed?
You should most definitely be, at least, using a using block when accessing the database. The Data Context does not automatically open and close the connections for you. You're still responsible for your resources.
You might want to look into using the Unit of Work pattern in case you need to access the database multiple times using a single connection (related data, etc.).
Is the DataContext "smart" enough to cleanup after itself
Data Context's are designed to be used in a small scope; that of a single transaction. As such they won't release any of their resources until you dispose of them and allow the object to go out of scope. Using a single context through your entire application like this will result in the consumption of a significant amount of resources (including a network connection as well as memory (both managed and unmanaged)).
You should instead create a new data context for each LoadData call, and wrap the context in a using to ensure that it's properly disposed after each call.
As noted in comments the data context is also not thread safe, so that is yet another reason not to be re-using the same context across multiple logical transactions.

What is difference between this two C# methods

What is the difference between these two cases. Firstly, if I open the connection and pass it into my method as a parameter, compared to opening the connection directly in the method?
cnn.open()
func(cnn,param1,param2);
vs
func(cnn, param1,param2)
{
cnn.open();
//open connection here
}
There's no difference from the code you've posted other than in one case, your calling function needs to take care of opening/closing the connection, in the other, you'd expect the function to do it.
The difference is that in the second method, you open the connection.
In the first method you expect the method to only use a connection not caring about cleaning up the resources.
No functional difference but the lines for opening and closing a connection should usually be as close together as possible hence they should be in the same method.
The difference is in how you want to use the connection and performance. If the function is a once off call and you are calling no other functions, or not doing anything else with the connection, then the second version of the function could even be reduced to:
func(param1, param2) {
Connection c = ....
c.Open(...);
...
c.Close();
}
However if you are calling many functions on the connection, even calling the function many times on the connection or if the creation and configuration of the connection is at a higher layer in your code, then you should use the first version of the function, with the addition of throwing an exception if the connection is not opened.
Well, I think you shouldn't ask for the different rather you should explain the situation you are in and ask for recommendation for what case must be used.
Anyway, As Everybody told you In case 2 the connection object and its life cycle is encapsulated within the callee function. This is recommended if database operation out side this function is not desired.
Otherwise if you have any other database activity to be done out side this function scope like in caller function or any other function(other than func) being called from caller function then you should use the Case 1.

How to restrict to one method call at a time?

I'd like to know how to implement the following restriction: One method in my Windows Service should not be called again before the earlier call has been finished. The method in question goes thru couple of database tables and it's very important that this process won't be called again before it's finished. I have a setting that defines how often my service will activate and under normal circumstances it never activates before the earlier call has been finished (because whole process should not take more than couple of minutes and the interval is set to 10 minutes) but thats not sure enough. I guess.
How to implement this?
You can use a named Mutex or a named Semaphore to ensure that only one holder of the Mutex/Semaphore is executing at once. As a commenter pointed out, keep in mind you must be careful not to abandon a mutex or improperly acquire/release a semaphore.
One way would be to use locking:
private readonly object myLock = new object();
private void MyMethod()
{
lock(myLock)
{
//code goes here
}
}
This ensures that this method can never be running more that once at a time.
I second the Mutex suggestion, but you might also want to take a look at transactions. Wrap your entire code in a transaction (this requires a using System.Transactions):
using(TransactionScope scope = new TransactionScope())
{
try
{
/* ... your current code here */
scope.Complete();
}
catch (Exception e)
{
/* Any appropriate error handling/logging here */
}
finally
{
}
}
A transactionscope automatically locks all related tables. You can reduce the restrictions and allow other processes to read, but not write to the data that your process is touching. You do this by passing options to the TransactionsScope constructor.
Well if all the code is localized you can set a boolean and check the boolean before executing the method, otherwise you can IPC and request the state before execution.
Some alternatives:
You can put a check in the call to check some flag or call Monitor.TryEnter and return with an error/do nothing if negative.
You can queue up calls (if you need this method to execute more than once) and only invoke when Monitor has been signaled.
If you don't mind blocking, and the method is on a separate thread, you can join the thread of the method you want to wait.
I'm sure there are others.
If you don't mind restricting one thread at a time to the entire object, then you can use:
Synchronization Contexts
Have your class inherit from ContextBoundObject
Apply a [Synchronization] attribute to the class.
The CLR will only allow one thread at a time to execute code per instance of this class. The others will block until the lock is released by the current thread.
This sounds like a serial workflow... Have you considered using a workflow framework?
If you want your function run with await/async
private static readonly SemaphoreSlim yourLock = new SemaphoreSlim(1, 1); //allow only 1 thread at time
...
private async Task<string> YourFunction() {
await yourLock.WaitAsync();
try
{
//your code go here
}
finally
{
yourLock.Release();
}
}

Categories

Resources