|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.idgen.enhanced; |
6 | 6 |
|
7 | | -import java.util.Arrays; |
8 | | -import java.util.List; |
9 | | -import java.util.concurrent.Callable; |
10 | | -import java.util.concurrent.CopyOnWriteArrayList; |
11 | | -import java.util.concurrent.CountDownLatch; |
12 | | -import java.util.concurrent.ExecutionException; |
13 | | -import java.util.concurrent.ExecutorService; |
14 | | -import java.util.concurrent.Executors; |
15 | | -import java.util.concurrent.TimeUnit; |
16 | 7 | import jakarta.persistence.Entity; |
17 | 8 | import jakarta.persistence.GeneratedValue; |
18 | 9 | import jakarta.persistence.Id; |
19 | | - |
20 | | -import org.hibernate.Session; |
21 | 10 | import org.hibernate.annotations.GenericGenerator; |
22 | 11 | import org.hibernate.annotations.Parameter; |
23 | | -import org.hibernate.boot.registry.StandardServiceRegistry; |
| 12 | +import org.hibernate.boot.MetadataSources; |
24 | 13 | import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 14 | +import org.hibernate.boot.spi.MetadataImplementor; |
25 | 15 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
26 | | -import org.hibernate.exception.ConstraintViolationException; |
27 | | - |
| 16 | +import org.hibernate.engine.spi.SessionImplementor; |
| 17 | +import org.hibernate.service.ServiceRegistry; |
28 | 18 | import org.hibernate.testing.orm.junit.JiraKey; |
29 | | -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
30 | | -import org.junit.Test; |
| 19 | +import org.hibernate.testing.orm.transaction.TransactionUtil; |
| 20 | +import org.hibernate.tool.schema.Action; |
| 21 | +import org.junit.jupiter.api.Test; |
31 | 22 |
|
32 | | -import static org.junit.Assert.fail; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_AUTO; |
33 | 26 |
|
34 | 27 | /** |
35 | | - * Demonstrates HHH-3628 issue with rolling over buckets in HiLoOptimizer. There are |
36 | | - * two variants of the test which do pretty much the same thing - one in sessions in |
37 | | - * parallel threads and one simply performing actions in sequence in two sessions. |
38 | | - * Possibly the threaded version is somewhat redundant given that the simpler test |
39 | | - * also exhibits the problem. |
40 | | - * |
41 | 28 | * @author Richard Barnes 4 May 2016 |
42 | 29 | */ |
43 | 30 | @JiraKey(value = "HHH-3628") |
44 | | -public class HiloOptimizerConcurrencyTest extends BaseNonConfigCoreFunctionalTestCase { |
45 | | - |
46 | | - private boolean createSchema = true; |
47 | | - |
48 | | - private ExecutorService executor = Executors.newFixedThreadPool( 2 ); |
49 | | - |
50 | | - @Override |
51 | | - protected Class[] getAnnotatedClasses() { |
52 | | - return new Class[] { |
53 | | - HibPerson.class |
54 | | - }; |
55 | | - } |
| 31 | +public class HiloOptimizerConcurrencyTest { |
56 | 32 |
|
57 | 33 | @Test |
58 | | - public void testTwoSessionsParallelGeneration() { |
59 | | - createSchema = true; |
60 | | - |
61 | | - StandardServiceRegistry serviceRegistry = serviceRegistry(); |
62 | | - SessionFactoryImplementor sessionFactory = sessionFactory(); |
63 | | - |
64 | | - try { |
65 | | - final Session session1 = openSession(); |
| 34 | + public void testTwoSessionsSerialGeneration() { |
| 35 | + inSessionFactory( true, (sf1) -> { |
| 36 | + var session1 = sf1.openSession(); |
66 | 37 |
|
67 | | - try { |
68 | | - session1.beginTransaction(); |
69 | | - HibPerson p = new HibPerson(); |
| 38 | + TransactionUtil.inTransaction( session1, (SessionImplementor s) -> { |
| 39 | + var p = new HibPerson(); |
70 | 40 | session1.persist( p ); |
71 | | - } |
72 | | - finally { |
73 | | - session1.getTransaction().commit(); |
74 | | - } |
75 | | - |
76 | | - createSchema = false; |
77 | | - buildResources(); |
78 | | - |
79 | | - final Session session2 = openSession(); |
80 | | - try { |
81 | | - session2.beginTransaction(); |
82 | | - HibPerson p = new HibPerson(); |
83 | | - session2.persist( p ); |
84 | | - } |
85 | | - finally { |
86 | | - session2.getTransaction().commit(); |
87 | | - } |
| 41 | + } ); |
88 | 42 |
|
89 | | - final List<Throwable> errs = new CopyOnWriteArrayList<>(); |
| 43 | + inSessionFactory( false, (sf2) -> { |
| 44 | + var session2 = sf2.openSession(); |
90 | 45 |
|
91 | | - CountDownLatch firstLatch = new CountDownLatch( 1 ); |
92 | | - CountDownLatch secondLatch = new CountDownLatch( 1 ); |
| 46 | + TransactionUtil.inTransaction( session2, (SessionImplementor s) -> { |
| 47 | + var p = new HibPerson(); |
| 48 | + session2.persist( p ); |
| 49 | + } ); |
93 | 50 |
|
94 | | - Callable<Void> callable1 = () -> { |
95 | | - try { |
96 | | - for ( int i = 2; i < 6; i++ ) { |
97 | | - try { |
98 | | - session1.beginTransaction(); |
99 | | - HibPerson p = new HibPerson(); |
100 | | - session1.persist( p ); |
101 | | - } |
102 | | - finally { |
103 | | - session1.getTransaction().commit(); |
104 | | - } |
105 | | - } |
106 | | - firstLatch.countDown(); |
107 | | - secondLatch.await(); |
108 | | - try { |
109 | | - session1.beginTransaction(); |
110 | | - HibPerson p = new HibPerson(); |
| 51 | + for ( int i = 2; i < 6; i++ ) { |
| 52 | + TransactionUtil.inTransaction( session1, (SessionImplementor s) -> { |
| 53 | + var p = new HibPerson(); |
111 | 54 | session1.persist( p ); |
112 | | - } |
113 | | - finally { |
114 | | - session1.getTransaction().commit(); |
115 | | - } |
116 | | - } |
117 | | - catch (Throwable t) { |
118 | | - errs.add( t ); |
119 | | - } |
120 | | - |
121 | | - return null; |
122 | | - }; |
123 | | - |
124 | | - Callable<Void> callable2 = () -> { |
125 | | - try { |
126 | | - firstLatch.await(); |
127 | | - secondLatch.countDown(); |
128 | | - try { |
129 | | - session2.beginTransaction(); |
130 | | - HibPerson p = new HibPerson(); |
131 | | - session2.persist( p ); |
132 | | - } |
133 | | - finally { |
134 | | - session2.getTransaction().commit(); |
135 | | - } |
136 | | - } |
137 | | - catch (Throwable t) { |
138 | | - errs.add( t ); |
| 55 | + } ); |
139 | 56 | } |
140 | 57 |
|
141 | | - return null; |
142 | | - }; |
143 | | - |
144 | | - executor.invokeAll( Arrays.asList( |
145 | | - callable1, callable2 |
146 | | - ) , 30, TimeUnit.SECONDS).forEach( c -> { |
147 | | - try { |
148 | | - c.get(); |
149 | | - } |
150 | | - catch (InterruptedException|ExecutionException e) { |
151 | | - Thread.interrupted(); |
152 | | - fail(e.getMessage()); |
153 | | - } |
154 | | - }); |
155 | | - |
156 | | - for(Throwable ex : errs) { |
157 | | - fail(ex.getMessage()); |
158 | | - } |
159 | | - } |
160 | | - catch (InterruptedException e) { |
161 | | - fail(e.getMessage()); |
162 | | - } |
163 | | - finally { |
164 | | - releaseResources( serviceRegistry, sessionFactory ); |
165 | | - } |
166 | | - } |
| 58 | + TransactionUtil.inTransaction( session2, (SessionImplementor s) -> { |
| 59 | + var p = new HibPerson(); |
| 60 | + session2.persist( p ); |
| 61 | + } ); |
167 | 62 |
|
168 | | - @Test |
169 | | - public void testTwoSessionsSerialGeneration() { |
170 | | - createSchema = true; |
171 | | - rebuildSessionFactory(); |
172 | | - StandardServiceRegistry serviceRegistry = serviceRegistry(); |
173 | | - SessionFactoryImplementor sessionFactory = sessionFactory(); |
174 | | - |
175 | | - try { |
176 | | - final Session session1 = openSession(); |
177 | | - |
178 | | - try { |
179 | | - session1.beginTransaction(); |
180 | | - HibPerson p = new HibPerson(); |
181 | | - session1.persist( p ); |
182 | | - } |
183 | | - finally { |
184 | | - session1.getTransaction().commit(); |
185 | | - } |
186 | | - |
187 | | - createSchema = false; |
188 | | - buildResources(); |
189 | | - |
190 | | - final Session session2 = openSession(); |
191 | | - session2.beginTransaction(); |
192 | | - try { |
193 | | - HibPerson p = new HibPerson(); |
194 | | - session2.persist( p ); |
195 | | - } |
196 | | - finally { |
197 | | - session2.getTransaction().commit(); |
198 | | - } |
199 | | - |
200 | | - for ( int i = 2; i < 6; i++ ) { |
201 | | - session1.beginTransaction(); |
202 | | - try { |
203 | | - HibPerson p = new HibPerson(); |
| 63 | + TransactionUtil.inTransaction( session1, (SessionImplementor s) -> { |
| 64 | + var p = new HibPerson(); |
204 | 65 | session1.persist( p ); |
205 | | - } |
206 | | - finally { |
207 | | - session1.getTransaction().commit(); |
208 | | - } |
209 | | - } |
210 | | - session2.beginTransaction(); |
211 | | - try { |
212 | | - HibPerson p = new HibPerson(); |
213 | | - session2.persist( p ); |
214 | | - } |
215 | | - finally { |
216 | | - session2.getTransaction().commit(); |
217 | | - } |
218 | | - session1.beginTransaction(); |
219 | | - try { |
220 | | - HibPerson p = new HibPerson(); |
221 | | - session1.persist( p ); |
222 | | - } |
223 | | - finally { |
224 | | - session1.getTransaction().commit(); |
225 | | - } |
226 | | - } |
227 | | - catch (ConstraintViolationException cve) { |
228 | | - fail( "ConstraintViolationException: " + cve.getMessage() ); |
229 | | - } |
230 | | - finally { |
231 | | - releaseResources( serviceRegistry, sessionFactory ); |
232 | | - } |
| 66 | + } ); |
| 67 | + } ); |
| 68 | + } ); |
233 | 69 | } |
234 | 70 |
|
235 | | - private void releaseResources(StandardServiceRegistry serviceRegistry, SessionFactoryImplementor sessionFactory) { |
236 | | - if ( sessionFactory != null ) { |
237 | | - try { |
238 | | - sessionFactory.close(); |
239 | | - } |
240 | | - catch (Exception e) { |
241 | | - fail( "Unable to release SessionFactory : " + e.getMessage() ); |
242 | | - } |
243 | | - } |
244 | | - |
245 | | - if ( serviceRegistry != null ) { |
246 | | - try { |
247 | | - StandardServiceRegistryBuilder.destroy( serviceRegistry ); |
248 | | - } |
249 | | - catch (Exception e) { |
250 | | - fail( "Unable to release StandardServiceRegistry : " + e.getMessage() ); |
| 71 | + private void inSessionFactory(boolean createSchema, Consumer<SessionFactoryImplementor> action) { |
| 72 | + try (var serviceRegistry = createServiceRegistry( createSchema )) { |
| 73 | + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) |
| 74 | + .addAnnotatedClass( HibPerson.class ) |
| 75 | + .buildMetadata(); |
| 76 | + try (var sessionFactory = metadata.buildSessionFactory()) { |
| 77 | + action.accept( sessionFactory ); |
251 | 78 | } |
252 | 79 | } |
253 | 80 | } |
254 | 81 |
|
255 | | - @Override |
256 | | - protected boolean createSchema() { |
257 | | - return createSchema; |
| 82 | + private ServiceRegistry createServiceRegistry(boolean createSchema) { |
| 83 | + final StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); |
| 84 | + if ( createSchema ) { |
| 85 | + ssrb.applySetting( HBM2DDL_AUTO, Action.CREATE_DROP ); |
| 86 | + } |
| 87 | + return ssrb.build(); |
258 | 88 | } |
259 | 89 |
|
260 | 90 | @Entity(name = "HibPerson") |
|
0 commit comments