简体   繁体   English

多个线程写入单个数据库表

[英]Multiple threads writing into single database table

i am having a issue with multiple threads inserting into a single table in database using hibernate. 我在使用休眠将多个线程插入数据库的单个表时遇到问题。 Every thread generates some data and then inserts them into a table. 每个线程都会生成一些数据,然后将它们插入表中。 Problem is that inserting is processed by only 1 thread, because of database locks. 问题是由于数据库锁定,插入仅由1个线程处理。 What is the best workaround, so the threads can all insert in the same time? 最好的解决方法是什么,以便所有线程都可以同时插入? I tried to have 1 dedicated thread that will be writing into table and others will be generating data. 我试图有1个专用线程将写入表,而其他线程将生成数据。 But the data is generated way faster then inserted, so it doesn't solve my problem. 但是生成数据的速度比插入数据快,因此无法解决我的问题。

My only idea is to create own database/tables for each thread, but that seems kinda weird to me, because i don't know how many threads i will be creating in advance. 我唯一的想法是为每个线程创建自己的数据库/表,但这对我来说有点奇怪,因为我不知道我会提前创建多少个线程。 Is there a better solution? 有更好的解决方案吗?

You have to keep a queue to keep data to be inserted. 您必须保持队列以保留要插入的数据。 So all processing thread can insert data into queue. 因此,所有处理线程都可以将数据插入队列。 Once your inserting thread insert data into DB, you can remove it from queue. 一旦您的插入线程将数据插入数据库,就可以将其从队列中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM