Tom
Tom

Reputation: 1373

SQL Transaction with C#

I have C# windows application.When 'Button 1' click in 'Form 1' I have a process that written data to few tables. That process normally taking 10 minutes. When one user click that 'Button 1' , another user who using same DB and program clicking another button in another form which inserting data to same tables.

When Button 1 clicking I'm starting the SQL Transaction and commiting that after finish the process.

Problem is when running that process, 2nd user getting a time out error and he can't complete his process.

What can I do for this? When SQL transaction starting is thet locking the tables which using for the process?

Upvotes: 0

Views: 297

Answers (3)

Poomrokc The 3years
Poomrokc The 3years

Reputation: 1099

Simple solution but may need user to wait for a long time.

If you are using a thread to accomplish this(winform) with label1 as the working state

   void transaction(data)
    { 
     try
     {
       thread.sleep(100);
       sqltransaction(data);//whatever your code is
     }
    catch(Exception)
     {
       transaction(data);
       label1.Invoke((MethodInvoker)(() => label1.Text = "Requested pending , do not close the program";
     }
       label1.Invoke((MethodInvoker)(() => label1.Text = "Working...";
    }

This is the way I usually handle error that I can't really fix it.

Thanks

Upvotes: 1

TomTom
TomTom

Reputation: 62093

What can I do for this?

Rework your transaction handling. Seriously.

That process normally taking 10 minutes

A 10 minutes transaction is not practical by any means. I fail to see any reason for this outside bad comceptionalization - and I am doing database work for maybe 20 years all in all. Never tolerated and had a real need for transactions that long, normally there is a way around it.

Yes, you could change a timeout, but that means user 2 staring at the screen for 15 minutes. Not a real solution.

Rework your transaction from a logical point of view. What the heck do you do there that takes 10 minutes to start with?

Upvotes: 4

Allan S. Hansen
Allan S. Hansen

Reputation: 4081

A transaction is by definition blocking other transactions access to the same resources because it's the way the SQL server guarantees that data is persisted at the commit and isn't changed by somebody else. It's the

So - what you can do is either make your transaction run faster, so it doesn't block and lock for long. Apply longer wait for second user. Or not use transactions at all. (or handle the situation "better" in your code layer, if it is 'expected behaviour')

You can also check your transactions that it doesn't block too much, but if the second process really needs to access the same resources it will have to wait.

However, If really wanting to get "dirty" you can change isolation levels for the server to allow reading of dirty data but that's absolutely not something I'd advice unless you know what you're doing. But it's something which can speed up read queries but it can have rather strange consequences reading dirty data and dirty data pages.

Upvotes: 0

Related Questions