Skip to content

Commit 1f211ca

Browse files
committed
Add support for batching in AbstractDao.update
Add a missing clear() in create() to remove outstanding entities from the session which have already been flushed to the database.
1 parent de0b060 commit 1f211ca

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

gemma-core/src/main/java/ubic/gemma/persistence/service/AbstractDao.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public abstract class AbstractDao<T extends Identifiable> extends HibernateDaoSu
4343

4444
protected static final Log log = LogFactory.getLog( TaxonServiceImpl.class );
4545

46+
/**
47+
* Batch size to reach before flushing the Hibernate session.
48+
*
49+
* See https://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/batch.html for more details.
50+
*/
51+
private static final int BATCH_SIZE = 100;
52+
4653
protected final Class<T> elementClass;
4754

4855
protected AbstractDao( Class<T> elementClass, SessionFactory sessionFactory ) {
@@ -55,8 +62,10 @@ public Collection<T> create( Collection<T> entities ) {
5562
int i = 0;
5663
for ( T t : entities ) {
5764
this.create( t );
58-
if ( ++i % 100 == 0 )
65+
if ( ++i % BATCH_SIZE == 0 ) {
5966
this.getSessionFactory().getCurrentSession().flush();
67+
this.getSessionFactory().getCurrentSession().clear();
68+
}
6069
}
6170
return entities;
6271
}
@@ -125,8 +134,13 @@ public void remove( T entity ) {
125134
@Override
126135
@Transactional
127136
public void update( Collection<T> entities ) {
137+
int i = 0;
128138
for ( T entity : entities ) {
129139
this.update( entity );
140+
if ( ++i % BATCH_SIZE == 0 ) {
141+
this.getSessionFactory().getCurrentSession().flush();
142+
this.getSessionFactory().getCurrentSession().clear();
143+
}
130144
}
131145
}
132146

0 commit comments

Comments
 (0)