HHH-5778 : Wire in new batch code

This commit is contained in:
Gail Badner 2010-12-16 17:36:18 -08:00
parent 7262276fa9
commit 1f0c74cb5e
4 changed files with 57 additions and 9 deletions

View File

@ -50,6 +50,11 @@ public class BatchingBatch extends AbstractBatchImpl {
private static final Logger log = LoggerFactory.getLogger( BatchingBatch.class );
private final int batchSize;
// TODO: A Map is used for expectations so it is possible to track when a batch
// is full (i.e., when the batch for a particular statement exceeds batchSize)
// Until HHH-5797 is fixed, there will only be 1 statement in a batch, so it won't
// be necessary to track expectations by statement.
private Map<String, List<Expectation>> expectationsBySql;
private int maxBatchPosition;
@ -88,6 +93,10 @@ public class BatchingBatch extends AbstractBatchImpl {
}
expectations.add( expectation );
maxBatchPosition = Math.max( maxBatchPosition, expectations.size() );
// TODO: When HHH-5797 is fixed the following if-block should probably be moved before
// adding the batch to the current statement (to detect that we have finished
// with the previous entity).
if ( maxBatchPosition == batchSize ) {
notifyObserversImplicitExecution();
doExecuteBatch();

View File

@ -52,15 +52,16 @@ public interface Batch {
public void addObserver(BatchObserver observer);
/**
* Get a statement which is part of the batch, creating if necessary (and storing for next time).
* Get a statement which is part of the batch.
*
* @param sql The SQL statement.
* @return The prepared statement instance, representing the SQL statement.
* @return the prepared statement representing the SQL statement, if the batch contained it;
* null, otherwise.
*/
public PreparedStatement getBatchStatement(Object key, String sql);
/**
* Store a statement in the batch.
* Add a prepared statement to the batch.
*
* @param sql The SQL statement.
*/

View File

@ -10,6 +10,7 @@ import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.testing.junit.functional.FunctionalTestCase;
import org.hibernate.testing.junit.functional.FunctionalTestClassTestSuite;
import org.hibernate.cfg.Configuration;
@ -49,16 +50,37 @@ public class BatchTest extends FunctionalTestCase {
final int N = 5000; //26 secs with batch flush, 26 without
//final int N = 100000; //53 secs with batch flush, OOME without
//final int N = 250000; //137 secs with batch flush, OOME without
int batchSize = ( ( SessionFactoryImplementor ) getSessions() ).getSettings().getJdbcBatchSize();
doBatchInsertUpdate( N, batchSize );
System.out.println( System.currentTimeMillis() - start );
}
public void testBatchInsertUpdateSizeEqJdbcBatchSize() {
int batchSize = ( ( SessionFactoryImplementor ) getSessions() ).getSettings().getJdbcBatchSize();
doBatchInsertUpdate( 50, batchSize );
}
public void testBatchInsertUpdateSizeLtJdbcBatchSize() {
int batchSize = ( ( SessionFactoryImplementor ) getSessions() ).getSettings().getJdbcBatchSize();
doBatchInsertUpdate( 50, batchSize - 1 );
}
public void testBatchInsertUpdateSizeGtJdbcBatchSize() {
long start = System.currentTimeMillis();
int batchSize = ( ( SessionFactoryImplementor ) getSessions() ).getSettings().getJdbcBatchSize();
doBatchInsertUpdate( 50, batchSize + 1 );
}
public void doBatchInsertUpdate(int nEntities, int nBeforeFlush) {
Session s = openSession();
s.setCacheMode( CacheMode.IGNORE );
Transaction t = s.beginTransaction();
for ( int i = 0; i < N; i++ ) {
for ( int i = 0; i < nEntities; i++ ) {
DataPoint dp = new DataPoint();
dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) );
dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) );
s.save( dp );
if ( i % 20 == 0 ) {
if ( i + 1 % nBeforeFlush == 0 ) {
s.flush();
s.clear();
}
@ -75,15 +97,30 @@ public class BatchTest extends FunctionalTestCase {
while ( sr.next() ) {
DataPoint dp = ( DataPoint ) sr.get( 0 );
dp.setDescription( "done!" );
if ( ++i % 20 == 0 ) {
if ( ++i % nBeforeFlush == 0 ) {
s.flush();
s.clear();
}
}
t.commit();
s.close();
System.out.println( System.currentTimeMillis() - start );
}
s = openSession();
s.setCacheMode( CacheMode.IGNORE );
t = s.beginTransaction();
i = 0;
sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY );
while ( sr.next() ) {
DataPoint dp = ( DataPoint ) sr.get( 0 );
s.delete( dp );
if ( ++i % nBeforeFlush == 0 ) {
s.flush();
s.clear();
}
}
t.commit();
s.close();
}
}

View File

@ -9,7 +9,8 @@ log4j.rootLogger=info, stdout
log4j.logger.org.hibernate.test=info
log4j.logger.org.hibernate.tool.hbm2ddl=debug
log4j.logger.org.hibernate.engine.jdbc.internal=trace
log4j.logger.org.hibernate.engine.jdbc=trace
log4j.logger.org.hibernate.engine.jdbc.internal.proxy=trace
log4j.logger.org.hibernate.engine.jdbc.batch.internal=trace
log4j.logger.org.hibernate.hql.ast.QueryTranslatorImpl=trace
log4j.logger.org.hibernate.hql.ast.HqlSqlWalker=trace
log4j.logger.org.hibernate.hql.ast.SqlGenerator=trace