1 package org.hibernate.test.batch; 3 4 import java.math.BigDecimal ; 5 6 import junit.framework.Test; 7 import junit.framework.TestSuite; 8 9 import org.hibernate.CacheMode; 10 import org.hibernate.ScrollMode; 11 import org.hibernate.ScrollableResults; 12 import org.hibernate.Session; 13 import org.hibernate.Transaction; 14 import org.hibernate.cfg.Configuration; 15 import org.hibernate.cfg.Environment; 16 import org.hibernate.test.TestCase; 17 18 25 public class BatchTest extends TestCase { 26 27 public BatchTest(String str) { 28 super(str); 29 } 30 31 public void testBatchInsertUpdate() { 32 33 35 long start = System.currentTimeMillis(); 36 37 final boolean flushInBatches = true; 38 final int N = 50000; 42 Session s = openSession(); 43 s.setCacheMode(CacheMode.IGNORE); 44 Transaction t = s.beginTransaction(); 45 for ( int i=0; i<N; i++ ) { 46 DataPoint dp = new DataPoint(); 47 dp.setX( new BigDecimal (i * 0.1d) ); 48 dp.setY( new BigDecimal ( Math.cos( dp.getX().doubleValue() ) ) ); 49 s.save(dp); 50 if ( flushInBatches && i % 20 == 0 ) { 51 s.flush(); 52 s.clear(); 53 } 54 } 55 t.commit(); 56 s.close(); 57 58 s = openSession(); 59 s.setCacheMode(CacheMode.IGNORE); 60 t = s.beginTransaction(); 61 int i = 0; 62 ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc") 63 .scroll(ScrollMode.FORWARD_ONLY); 64 while ( sr.next() ) { 65 DataPoint dp = (DataPoint) sr.get(0); 66 dp.setDescription("done!"); 67 if ( flushInBatches && ++i % 20 == 0 ) { 68 s.flush(); 69 s.clear(); 70 } 71 } 72 t.commit(); 73 s.close(); 74 System.out.println( System.currentTimeMillis() - start ); 75 } 76 77 protected void configure(Configuration cfg) { 78 cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "20"); 79 } 80 81 protected String [] getMappings() { 82 return new String [] { "batch/DataPoint.hbm.xml" }; 83 } 84 85 public static Test suite() { 86 return new TestSuite(BatchTest.class); 87 } 88 89 public String getCacheConcurrencyStrategy() { 90 return null; 91 } 92 93 } 94 95 | Popular Tags |