FileDocCategorySizeDatePackage
BatchTest.javaAPI DocHibernate 3.2.52413Tue Dec 12 16:22:26 GMT 2006org.hibernate.test.batch

BatchTest

public class BatchTest extends org.hibernate.junit.functional.FunctionalTestCase
This is how to do batch processing in Hibernate. Remember to enable JDBC batch updates, or this test will take a Very Long Time!
author
Gavin King

Fields Summary
Constructors Summary
public BatchTest(String str)

		super( str );
	
Methods Summary
public voidconfigure(org.hibernate.cfg.Configuration cfg)

		cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "20" );
	
public java.lang.StringgetCacheConcurrencyStrategy()

		return null;
	
public java.lang.String[]getMappings()

		return new String[] { "batch/DataPoint.hbm.xml" };
	
public static junit.framework.Testsuite()

		return new FunctionalTestClassTestSuite( BatchTest.class );
	
public voidtestBatchInsertUpdate()

		long start = System.currentTimeMillis();
		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

		Session s = openSession();
		s.setCacheMode( CacheMode.IGNORE );
		Transaction t = s.beginTransaction();
		for ( int i = 0; i < N; 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 ) {
				s.flush();
				s.clear();
			}
		}
		t.commit();
		s.close();

		s = openSession();
		s.setCacheMode( CacheMode.IGNORE );
		t = s.beginTransaction();
		int i = 0;
		ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
				.scroll( ScrollMode.FORWARD_ONLY );
		while ( sr.next() ) {
			DataPoint dp = ( DataPoint ) sr.get( 0 );
			dp.setDescription( "done!" );
			if ( ++i % 20 == 0 ) {
				s.flush();
				s.clear();
			}
		}
		t.commit();
		s.close();
		System.out.println( System.currentTimeMillis() - start );