Daniel
Daniel

Reputation: 383

multi thread database insertion

I have an application(C#) which is inserting data to database(MySQL). The program runs on multithreads with a timer.

The question of mine is the following, what would be the best way to insert data to the database.

Example of the application:

The timer tick each second, I have data which I would like to insert to the database in each second, but I have some which I want to insert rearer, like every half minute. The data always goes to different tables, but same database.

I did always open and close the database connection after each insertion. In that case I had problems when I want to open an already open connection or use a connection which is already closed by the other thread. To fix that problem I open the connection when I started the timer and closed it when I stopped it. It is working the way like that but not so safe.

With this solution my database still get confused sometimes. I just tried it with local database, I do not know if it would be different with remote one or not(be honest it should work with local and remote DB as well).

There was an idea to have an extra method which is collect the data end it uploads after each tick(or after X tick) to the database. I am not sure if that would be a better solution or not, but I do not really have better idea just now. The problem with that solution is that it could get messy, (I guess I would need a global list what could be accessed by all of the threads and insert data in it, It could cause of a problem when I upload/clear the list while one of the threads might want to access to it).

Please let me know if it is not clear enough. Any suggestions will be appreciated.

Thanks

My code for databse insertion is the following:

con.Open();
MySqlCommand cmd = new MySqlCommand(command, con);
cmd.ExecuteNonQuery();
con.Close();

I think I should insert here some pseudo code as well, to make my problem more understandable:

Timer
foreach(sensor)
{
if sensor reading finished
{
check the type of the sensor
run the right thread
}

Example thread:

Thread
Read out data from sensor
insert data to DB

Response of the sensors are different, but still can be the same. This is why I have problem inserting to the database.

Upvotes: 0

Views: 2884

Answers (2)

Gaurav Gandhi
Gaurav Gandhi

Reputation: 3201

Use single connection only,
use one function whom you pass your data for insertion, that function will do insertion work.

like

func(strQry){ Command cmd = new ...(strQry, conn); cmd.execute...(); }

Reuse conn obj , but create new cmd obj every time..

Comment if stuck...

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273274

I did always open and close the database connection after each insertion [...] It is working the way like that but not so safe.

It is certainly the best way. Use the Connection in a using () {} block and it will be safe.

With this solution my database still get confused sometimes.

You will have to describe that a lot better.

There was an idea to have an extra method which is collect the data end it uploads after each tick

Yes, you can (should) probably use a extra Thread to push things to the Db. Look up "Producer/Consumer Pattern"

Upvotes: 2

Related Questions