KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > batch > BatchTest


1 //$Id: BatchTest.java,v 1.4 2005/04/11 19:32:42 oneovthafew Exp $
2
package org.hibernate.test.batch;
3
4 import java.math.BigDecimal JavaDoc;
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 /**
19  * This is how to do batch processing in Hibernate.
20  * Remember to enable JDBC batch updates, or this
21  * test will take a Very Long Time!
22  *
23  * @author Gavin King
24  */

25 public class BatchTest extends TestCase {
26     
27     public BatchTest(String JavaDoc str) {
28         super(str);
29     }
30     
31     public void testBatchInsertUpdate() {
32         
33         //remember to set hibernate.jdbc.batch_size=20
34

35         long start = System.currentTimeMillis();
36         
37         final boolean flushInBatches = true;
38         final int N = 50000; //26 secs with batch flush, 26 without
39
//final int N = 100000; //53 secs with batch flush, OOME without
40
//final int N = 250000; //137 secs with batch flush, OOME without
41

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 JavaDoc(i * 0.1d) );
48             dp.setY( new BigDecimal JavaDoc( 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 JavaDoc[] getMappings() {
82         return new String JavaDoc[] { "batch/DataPoint.hbm.xml" };
83     }
84
85     public static Test suite() {
86         return new TestSuite(BatchTest.class);
87     }
88
89     public String JavaDoc getCacheConcurrencyStrategy() {
90         return null;
91     }
92
93 }
94
95
Popular Tags