I have a server-client program with a database.
The client sends data to the server and the server saves the data into a database.
Now the question is if more than one client sends data to the server at the same time and the server wants to write it into the database, could there be a problem?
For example:
client1 calls a method:
client.save_data(data1);
client2 calls a method:
client.save_data(data2);
And the server tries:
save_data(data){
sql.insert_intoDB(data);
}
insert_intoDB(data)
{
//not real code, just to show what i mean
query = "insert into tablename values "+data;
query.execute;
}
What is a good way to handle this in c#?
Or is there no risk at all?
thx, best regards.
In general, yes, this problem is known as Concurrency. Different databases handle this problem in different ways. It’s usually more a concern when dealing with updates rather than inserts.
I think most, if not all, database engines would handle each insert in isolation so you shouldnt have to worry about it. However, to be on the safe side you should always use Transactions so you can rollback if anything does go wrong.
I would recommend using a TransactionScope if your using C#.