| 
4 | 4 |  */  | 
5 | 5 | package org.hibernate.orm.test.batch;  | 
6 | 6 | 
 
  | 
7 |  | -import java.math.BigDecimal;  | 
8 |  | - | 
9 | 7 | import org.hibernate.CacheMode;  | 
10 | 8 | import org.hibernate.ScrollMode;  | 
11 | 9 | 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;  | 
 | 10 | +import org.hibernate.cfg.AvailableSettings;  | 
 | 11 | +import org.hibernate.testing.orm.junit.DomainModel;  | 
 | 12 | +import org.hibernate.testing.orm.junit.ServiceRegistry;  | 
 | 13 | +import org.hibernate.testing.orm.junit.SessionFactory;  | 
 | 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope;  | 
 | 15 | +import org.hibernate.testing.orm.junit.Setting;  | 
 | 16 | +import org.junit.jupiter.api.Test;  | 
16 | 17 | 
 
  | 
17 |  | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;  | 
18 |  | -import org.junit.Test;  | 
 | 18 | +import java.math.BigDecimal;  | 
 | 19 | +import java.math.RoundingMode;  | 
19 | 20 | 
 
  | 
20 | 21 | /**  | 
21 | 22 |  * This is how to do batch processing in Hibernate. Remember to enable JDBC batch updates, or this test will take a  | 
22 | 23 |  * VeryLongTime!  | 
23 | 24 |  *  | 
24 | 25 |  * @author Gavin King  | 
25 | 26 |  */  | 
26 |  | -public class BatchTest extends BaseCoreFunctionalTestCase {  | 
27 |  | - | 
28 |  | -	@Override  | 
29 |  | -	protected String getBaseForMappings() {  | 
30 |  | -		return "org/hibernate/orm/test/";  | 
31 |  | -	}  | 
32 |  | - | 
33 |  | -	@Override  | 
34 |  | -	public String[] getMappings() {  | 
35 |  | -		return new String[] { "batch/DataPoint.hbm.xml" };  | 
36 |  | -	}  | 
37 |  | - | 
38 |  | -	@Override  | 
39 |  | -	public void configure(Configuration cfg) {  | 
40 |  | -		cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, 20 );  | 
41 |  | -	}  | 
 | 27 | +@SuppressWarnings("JUnitMalformedDeclaration")  | 
 | 28 | +@ServiceRegistry(settings = {  | 
 | 29 | +		@Setting(name= AvailableSettings.STATEMENT_BATCH_SIZE, value = "20")  | 
 | 30 | +})  | 
 | 31 | +@DomainModel(xmlMappings = "org/hibernate/orm/test/batch/DataPoint.xml")  | 
 | 32 | +@SessionFactory  | 
 | 33 | +public class BatchTest {  | 
42 | 34 | 
 
  | 
43 | 35 | 	@Test  | 
44 |  | -	public void testBatchInsertUpdate() {  | 
 | 36 | +	public void testBatchInsertUpdate(SessionFactoryScope factoryScope) {  | 
45 | 37 | 		long start = System.currentTimeMillis();  | 
46 | 38 | 		final int N = 5000; //26 secs with batch flush, 26 without  | 
47 | 39 | 		//final int N = 100000; //53 secs with batch flush, OOME without  | 
48 | 40 | 		//final int N = 250000; //137 secs with batch flush, OOME without  | 
49 |  | -		int batchSize = sessionFactory().getSessionFactoryOptions().getJdbcBatchSize();  | 
50 |  | -		doBatchInsertUpdate( N, batchSize );  | 
 | 41 | +		int batchSize = factoryScope.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();  | 
 | 42 | +		doBatchInsertUpdate( N, batchSize, factoryScope );  | 
51 | 43 | 		System.out.println( System.currentTimeMillis() - start );  | 
52 | 44 | 	}  | 
53 | 45 | 
 
  | 
54 | 46 | 	@Test  | 
55 |  | -	public void testBatchInsertUpdateSizeEqJdbcBatchSize() {  | 
56 |  | -		int batchSize = sessionFactory().getSessionFactoryOptions().getJdbcBatchSize();  | 
57 |  | -		doBatchInsertUpdate( 50, batchSize );  | 
 | 47 | +	public void testBatchInsertUpdateSizeEqJdbcBatchSize(SessionFactoryScope factoryScope) {  | 
 | 48 | +		int batchSize = factoryScope.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();  | 
 | 49 | +		doBatchInsertUpdate( 50, batchSize, factoryScope );  | 
58 | 50 | 	}  | 
59 | 51 | 
 
  | 
60 | 52 | 	@Test  | 
61 |  | -	public void testBatchInsertUpdateSizeLtJdbcBatchSize() {  | 
62 |  | -		int batchSize = sessionFactory().getSessionFactoryOptions().getJdbcBatchSize();  | 
63 |  | -		doBatchInsertUpdate( 50, batchSize - 1 );  | 
 | 53 | +	public void testBatchInsertUpdateSizeLtJdbcBatchSize(SessionFactoryScope factoryScope) {  | 
 | 54 | +		int batchSize = factoryScope.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();  | 
 | 55 | +		doBatchInsertUpdate( 50, batchSize - 1, factoryScope );  | 
64 | 56 | 	}  | 
65 | 57 | 
 
  | 
66 | 58 | 	@Test  | 
67 |  | -	public void testBatchInsertUpdateSizeGtJdbcBatchSize() {  | 
68 |  | -		int batchSize = sessionFactory().getSessionFactoryOptions().getJdbcBatchSize();  | 
69 |  | -		doBatchInsertUpdate( 50, batchSize + 1 );  | 
 | 59 | +	public void testBatchInsertUpdateSizeGtJdbcBatchSize(SessionFactoryScope factoryScope) {  | 
 | 60 | +		int batchSize = factoryScope.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();  | 
 | 61 | +		doBatchInsertUpdate( 50, batchSize + 1, factoryScope );  | 
70 | 62 | 	}  | 
71 | 63 | 
 
  | 
72 |  | -	public void doBatchInsertUpdate(int nEntities, int nBeforeFlush) {  | 
73 |  | -		Session s = openSession();  | 
74 |  | -		s.setCacheMode( CacheMode.IGNORE );  | 
75 |  | -		Transaction t = s.beginTransaction();  | 
76 |  | -		for ( int i = 0; i < nEntities; i++ ) {  | 
77 |  | -			DataPoint dp = new DataPoint();  | 
78 |  | -			dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) );  | 
79 |  | -			dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) );  | 
80 |  | -			s.persist( dp );  | 
81 |  | -			if ( ( i + 1 ) % nBeforeFlush == 0 ) {  | 
82 |  | -				s.flush();  | 
83 |  | -				s.clear();  | 
 | 64 | +	public void doBatchInsertUpdate(int nEntities, int nBeforeFlush, SessionFactoryScope factoryScope) {  | 
 | 65 | +		factoryScope.inTransaction( (session) -> {  | 
 | 66 | +			session.setCacheMode( CacheMode.IGNORE );  | 
 | 67 | +			for ( int i = 0; i < nEntities; i++ ) {  | 
 | 68 | +				DataPoint dp = new DataPoint();  | 
 | 69 | +				dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, RoundingMode.DOWN ) );  | 
 | 70 | +				dp.setY( BigDecimal.valueOf( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, RoundingMode.DOWN ) );  | 
 | 71 | +				session.persist( dp );  | 
 | 72 | +				if ( ( i + 1 ) % nBeforeFlush == 0 ) {  | 
 | 73 | +					session.flush();  | 
 | 74 | +					session.clear();  | 
 | 75 | +				}  | 
84 | 76 | 			}  | 
85 |  | -		}  | 
86 |  | -		t.commit();  | 
87 |  | -		s.close();  | 
 | 77 | +		} );  | 
88 | 78 | 
 
  | 
89 |  | -		s = openSession();  | 
90 |  | -		s.setCacheMode( CacheMode.IGNORE );  | 
91 |  | -		t = s.beginTransaction();  | 
92 |  | -		int i = 0;  | 
93 |  | -		try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )  | 
94 |  | -				.scroll( ScrollMode.FORWARD_ONLY )) {  | 
95 |  | -			while ( sr.next() ) {  | 
96 |  | -				DataPoint dp = (DataPoint) sr.get();  | 
97 |  | -				dp.setDescription( "done!" );  | 
98 |  | -				if ( ++i % nBeforeFlush == 0 ) {  | 
99 |  | -					s.flush();  | 
100 |  | -					s.clear();  | 
 | 79 | +		factoryScope.inTransaction( (session) -> {  | 
 | 80 | +			session.setCacheMode( CacheMode.IGNORE );  | 
 | 81 | +			int i = 0;  | 
 | 82 | +			try (ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )  | 
 | 83 | +					.scroll( ScrollMode.FORWARD_ONLY )) {  | 
 | 84 | +				while ( sr.next() ) {  | 
 | 85 | +					DataPoint dp = (DataPoint) sr.get();  | 
 | 86 | +					dp.setDescription( "done!" );  | 
 | 87 | +					if ( ++i % nBeforeFlush == 0 ) {  | 
 | 88 | +						session.flush();  | 
 | 89 | +						session.clear();  | 
 | 90 | +					}  | 
101 | 91 | 				}  | 
102 | 92 | 			}  | 
103 |  | -		}  | 
104 |  | -		t.commit();  | 
105 |  | -		s.close();  | 
 | 93 | +		} );  | 
 | 94 | + | 
 | 95 | +		factoryScope.inTransaction( (session) -> {  | 
 | 96 | +			session.setCacheMode( CacheMode.IGNORE );  | 
 | 97 | +			int i = 0;  | 
106 | 98 | 
 
  | 
107 |  | -		s = openSession();  | 
108 |  | -		s.setCacheMode( CacheMode.IGNORE );  | 
109 |  | -		t = s.beginTransaction();  | 
110 |  | -		i = 0;  | 
111 |  | -		try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )  | 
112 |  | -				.scroll( ScrollMode.FORWARD_ONLY )) {  | 
113 |  | -			while ( sr.next() ) {  | 
114 |  | -				DataPoint dp = (DataPoint) sr.get();  | 
115 |  | -				s.remove( dp );  | 
116 |  | -				if ( ++i % nBeforeFlush == 0 ) {  | 
117 |  | -					s.flush();  | 
118 |  | -					s.clear();  | 
 | 99 | +			try (ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )  | 
 | 100 | +					.scroll( ScrollMode.FORWARD_ONLY )) {  | 
 | 101 | +				while ( sr.next() ) {  | 
 | 102 | +					DataPoint dp = (DataPoint) sr.get();  | 
 | 103 | +					session.remove( dp );  | 
 | 104 | +					if ( ++i % nBeforeFlush == 0 ) {  | 
 | 105 | +						session.flush();  | 
 | 106 | +						session.clear();  | 
 | 107 | +					}  | 
119 | 108 | 				}  | 
120 | 109 | 			}  | 
121 |  | -		}  | 
122 |  | -		t.commit();  | 
123 |  | -		s.close();  | 
 | 110 | +		} );  | 
124 | 111 | 	}  | 
125 | 112 | }  | 
0 commit comments