简体   繁体   English

Hibernate的“ session.flush”上的多线程

[英]Multithreading on Hibernate's 'session.flush'

First of all, is this possibly ready-made? 首先,这可能是现成的吗?

Hibernate 3.6 , JDBC batch_size 500 , using Hilo generator on the 200.000 entities. 使用Hilo生成器在200.000个实体上使用Hibernate 3.6和JDBC batch_size 500。

In my example case, I have a request that takes 56 seconds, and I am creating 200,000 entities in the session. 在我的示例案例中,我有一个耗时56秒的请求,并且我正在会话中创建200,000个实体。 So, the session.flush() command takes 32 of those 56 seconds with only one CPU core at %100. 因此, session.flush()命令花费了这56秒中的32秒,其中只有一个CPU内核为%100。

Is there a way to get the list of entities that need to be updated and create the SQL statements, say in four threads? 有没有办法获取需要更新的实体列表并创建SQL语句(例如在四个线程中)?

You cannot simply flush() in different threads, because what flush() does is basically sending all pending SQL INSERT statements to the database using underlying connection. 您不能简单地在不同线程中进行flush() ,因为flush()所做的基本上是使用基础连接将所有未决的SQL INSERT语句发送到数据库。 JDBC connections aren't thread safe, which means you would have to use 4 different connections and thus 4 different transactions. JDBC连接不是线程安全的,这意味着您必须使用4个不同的连接,因此要使用4个不同的事务。 If all inserts need to take place in one transaction, there is nothing you can do here. 如果所有插入操作都需要在一个事务中进行,那么您将无法执行任何操作。

If you can live with 4 separate transactions, just create a thread pool and store records in smaller batches. 如果您可以处理4个独立的事务,则只需创建一个线程池并将记录分批存储。 Pool will distribute INSERT operations across several threads. 池将在多个线程之间分配INSERT操作。

Also are you sure this will really help? 您还确定这真的有帮助吗? I would guess flush() is not CPU-bound but I/O or network bound. 我猜flush()不受CPU限制,但受I / O或网络限制。 However your experience is different with 100% usage, so I might be wrong. 但是,您的使用率与100%使用率不同,因此我可能是错的。 Also try optimizing INSERT s - using stateless session, raw JDBC/Native queries, batch inserts, etc. Spliting into separate threads is much harder. 也可以尝试使用无状态会话,原始JDBC /本机查询,批处理插入等来优化INSERT 。拆分成单独的线程要困难得多。

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

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