res*_*es1 3 java hibernate transactions
我需要使用hibernate在数据库中插入大量数据,我正在查看来自hibernate的批量插入,我使用的是类似于手册中的示例:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
Run Code Online (Sandbox Code Playgroud)
但我发现flush不会在数据库上写入数据.阅读它,如果代码在事务中,那么在事务执行提交之前不会将任何内容提交给数据库.
那么需要使用flush/clear是什么?似乎没用,如果数据没有写在数据库上,那么它们就在内存中.
我如何强制hibernate在数据库中写入数据?
谢谢
数据被发送到数据库,不再存在于内存中.在事务提交之前,它并没有明确持久化.它与在任何数据库工具中执行以下语句序列完全相同:
begin;
insert into ...
insert into ...
insert into ...
// here, three inserts have been done on the database. But they will only be made
// definitively persistent at commit time
...
commit;
Run Code Online (Sandbox Code Playgroud)
flush包含执行insert语句.
提交包括执行commit语句.
| 归档时间: |
|
| 查看次数: |
3607 次 |
| 最近记录: |