Line 0
Link Here
|
|
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 1998, 2010 Oracle. All rights reserved. |
3 |
* This program and the accompanying materials are made available under the |
4 |
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
5 |
* which accompanies this distribution. |
6 |
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
7 |
* and the Eclipse Distribution License is available at |
8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
9 |
* |
10 |
* Contributors: |
11 |
* Oracle - initial API and implementation from Oracle TopLink |
12 |
******************************************************************************/ |
13 |
package org.eclipse.persistence.testing.tests.jpa.advanced; |
14 |
|
15 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
16 |
import org.eclipse.persistence.testing.models.jpa.advanced.*; |
17 |
import org.eclipse.persistence.testing.models.jpa.advanced.entities.*; |
18 |
|
19 |
import java.util.ArrayList; |
20 |
import java.util.Collection; |
21 |
import java.util.HashMap; |
22 |
import java.util.Map; |
23 |
import junit.framework.Test; |
24 |
import junit.framework.TestSuite; |
25 |
|
26 |
import javax.persistence.EntityManager; |
27 |
import javax.persistence.LockModeType; |
28 |
import javax.persistence.PessimisticLockScope; |
29 |
import javax.persistence.PersistenceException; |
30 |
import javax.persistence.Query; |
31 |
|
32 |
import org.eclipse.persistence.descriptors.ClassDescriptor; |
33 |
import org.eclipse.persistence.config.QueryHints; |
34 |
import org.eclipse.persistence.sessions.server.ServerSession; |
35 |
|
36 |
/** |
37 |
* <p> |
38 |
* <b>Purpose</b>: Test Pessimistic Locking Extended Scope functionality. |
39 |
* <p> |
40 |
* <b>Description</b>: Test the relationship will be locked or unlocked under different situations |
41 |
* <p> |
42 |
*/ |
43 |
public class PessimisticLockingExtendedScopeTestSuite extends JUnitTestCase { |
44 |
|
45 |
public PessimisticLockingExtendedScopeTestSuite() { |
46 |
super(); |
47 |
} |
48 |
|
49 |
public PessimisticLockingExtendedScopeTestSuite(String name) { |
50 |
super(name); |
51 |
} |
52 |
|
53 |
public static Test suite() { |
54 |
TestSuite suite = new TestSuite("PessimisticLocking ExtendedScope TestSuite"); |
55 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testSetup")); |
56 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES1")); |
57 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES2")); |
58 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES3")); |
59 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES4")); |
60 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES5")); |
61 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES6")); |
62 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES7")); |
63 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES8")); |
64 |
suite.addTest(new PessimisticLockingExtendedScopeTestSuite("testPESSMISTIC_ES9")); |
65 |
return suite; |
66 |
} |
67 |
|
68 |
public void testSetup() { |
69 |
ServerSession session = JUnitTestCase.getServerSession(); |
70 |
new AdvancedTableCreator().replaceTables(session); |
71 |
//make the entity EquipmentCode read-write for the following tests |
72 |
ClassDescriptor descriptor = session.getDescriptor(EquipmentCode.class); |
73 |
boolean shouldBeReadOnly = descriptor.shouldBeReadOnly(); |
74 |
descriptor.setShouldBeReadOnly(false); |
75 |
clearCache(); |
76 |
} |
77 |
|
78 |
//Entity relationships for which the locked entity contains the foreign key will be locked with bidirectional one-to-one mapping without mappedBy (Scenario 1.1) |
79 |
public void testPESSMISTIC_ES1() throws Exception{ |
80 |
// Cannot create parallel entity managers in the server. |
81 |
if (! isOnServer() && isSelectForUpateSupported()) { |
82 |
EntityManager em = createEntityManager(); |
83 |
EntyA a = null; |
84 |
EntyC c = null; |
85 |
try{ |
86 |
beginTransaction(em); |
87 |
a = new EntyA(); |
88 |
c = new EntyC(); |
89 |
em.persist(c); |
90 |
a.setName("test"); |
91 |
a.setEntyC(c); |
92 |
em.persist(a); |
93 |
commitTransaction(em); |
94 |
}catch (RuntimeException ex){ |
95 |
throw ex; |
96 |
}finally{ |
97 |
if (isTransactionActive(em)){ |
98 |
rollbackTransaction(em); |
99 |
} |
100 |
closeEntityManager(em); |
101 |
} |
102 |
|
103 |
Exception lockTimeOutException = null; |
104 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
105 |
Map<String, Object> properties = new HashMap(); |
106 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.EXTENDED); |
107 |
EntityManager em1= createEntityManager(); |
108 |
|
109 |
try{ |
110 |
beginTransaction(em1); |
111 |
a = em1.find(EntyA.class, a.getId()); |
112 |
em1.lock(a, lockMode, properties); |
113 |
EntityManager em2 = createEntityManager(); |
114 |
try{ |
115 |
beginTransaction(em2); |
116 |
a = em2.find(EntyA.class, a.getId()); |
117 |
a.setEntyC(null); |
118 |
commitTransaction(em2); |
119 |
}catch(javax.persistence.RollbackException ex){ |
120 |
ex.printStackTrace(); |
121 |
if (ex.getMessage().indexOf("org.eclipse.persistence.exceptions.DatabaseException") == -1){ |
122 |
ex.printStackTrace(); |
123 |
fail("it's not the right exception"); |
124 |
} |
125 |
}finally{ |
126 |
if (isTransactionActive(em2)){ |
127 |
rollbackTransaction(em2); |
128 |
} |
129 |
closeEntityManager(em2); |
130 |
} |
131 |
}catch (Exception ex){ |
132 |
throw ex; |
133 |
}finally{ |
134 |
if (isTransactionActive(em1)){ |
135 |
rollbackTransaction(em1); |
136 |
} |
137 |
closeEntityManager(em1); |
138 |
} |
139 |
} |
140 |
} |
141 |
|
142 |
//Entity relationships for which the locked entity contains the foreign key will be locked with unidirectional one-to-one mapping(Scenario 1.2) |
143 |
public void testPESSMISTIC_ES2() throws Exception{ |
144 |
// Cannot create parallel entity managers in the server. |
145 |
if (! isOnServer() && isSelectForUpateSupported()) { |
146 |
EntityManager em = createEntityManager(); |
147 |
EntyA a = null; |
148 |
EntyB b = null; |
149 |
try{ |
150 |
beginTransaction(em); |
151 |
a = new EntyA(); |
152 |
b = new EntyB(); |
153 |
a.setEntyB(b); |
154 |
em.persist(a); |
155 |
commitTransaction(em); |
156 |
}catch (RuntimeException ex){ |
157 |
throw ex; |
158 |
}finally{ |
159 |
if (isTransactionActive(em)){ |
160 |
rollbackTransaction(em); |
161 |
} |
162 |
closeEntityManager(em); |
163 |
} |
164 |
|
165 |
Exception lockTimeOutException = null; |
166 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
167 |
Map<String, Object> properties = new HashMap(); |
168 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.EXTENDED); |
169 |
EntityManager em1= createEntityManager(); |
170 |
|
171 |
try{ |
172 |
beginTransaction(em1); |
173 |
a = em1.find(EntyA.class, a.getId()); |
174 |
em1.lock(a, lockMode, properties); |
175 |
EntityManager em2 = createEntityManager(); |
176 |
try{ |
177 |
beginTransaction(em2); |
178 |
a = em2.find(EntyA.class, a.getId()); |
179 |
a.setEntyB(null); |
180 |
commitTransaction(em2); |
181 |
}catch(javax.persistence.RollbackException ex){ |
182 |
if (ex.getMessage().indexOf("org.eclipse.persistence.exceptions.DatabaseException") == -1){ |
183 |
ex.printStackTrace(); |
184 |
fail("it's not the right exception"); |
185 |
} |
186 |
}finally{ |
187 |
if (isTransactionActive(em2)){ |
188 |
rollbackTransaction(em2); |
189 |
} |
190 |
closeEntityManager(em2); |
191 |
} |
192 |
}catch (Exception ex){ |
193 |
throw ex; |
194 |
}finally{ |
195 |
if (isTransactionActive(em1)){ |
196 |
rollbackTransaction(em1); |
197 |
} |
198 |
closeEntityManager(em1); |
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
//Entity relationships for which the locked entity contains the foreign key will be locked with unidirectional many-to-one mapping(Scenario 1.3) |
204 |
public void testPESSMISTIC_ES3() throws Exception{ |
205 |
// Cannot create parallel entity managers in the server. |
206 |
if (! isOnServer() && isSelectForUpateSupported()) { |
207 |
EntityManager em = createEntityManager(); |
208 |
Equipment eq = null; |
209 |
EquipmentCode eqCode = null; |
210 |
try{ |
211 |
beginTransaction(em); |
212 |
eq = new Equipment(); |
213 |
eqCode = new EquipmentCode(); |
214 |
eqCode.setCode("A"); |
215 |
em.persist(eqCode); |
216 |
eq.setEquipmentCode(eqCode); |
217 |
em.persist(eq); |
218 |
commitTransaction(em); |
219 |
}catch (RuntimeException ex){ |
220 |
throw ex; |
221 |
}finally{ |
222 |
if (isTransactionActive(em)){ |
223 |
rollbackTransaction(em); |
224 |
} |
225 |
closeEntityManager(em); |
226 |
} |
227 |
|
228 |
Exception lockTimeOutException = null; |
229 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
230 |
Map<String, Object> properties = new HashMap(); |
231 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.EXTENDED); |
232 |
EntityManager em1= createEntityManager(); |
233 |
|
234 |
try{ |
235 |
beginTransaction(em1); |
236 |
eq = em1.find(Equipment.class, eq.getId()); |
237 |
em1.lock(eq, lockMode, properties); |
238 |
EntityManager em2 = createEntityManager(); |
239 |
try{ |
240 |
beginTransaction(em2); |
241 |
eq = em2.find(Equipment.class, eq.getId()); |
242 |
eq.setEquipmentCode(null); |
243 |
commitTransaction(em2); |
244 |
}catch(javax.persistence.RollbackException ex){ |
245 |
if (ex.getMessage().indexOf("org.eclipse.persistence.exceptions.DatabaseException") == -1){ |
246 |
ex.printStackTrace(); |
247 |
fail("it's not the right exception"); |
248 |
} |
249 |
}finally{ |
250 |
if (isTransactionActive(em2)){ |
251 |
rollbackTransaction(em2); |
252 |
} |
253 |
closeEntityManager(em2); |
254 |
} |
255 |
}catch (Exception ex){ |
256 |
throw ex; |
257 |
}finally{ |
258 |
if (isTransactionActive(em1)){ |
259 |
rollbackTransaction(em1); |
260 |
} |
261 |
closeEntityManager(em1); |
262 |
} |
263 |
} |
264 |
} |
265 |
|
266 |
//Entity relationships for which the locked entity contains the foreign key will be locked with bidirectional many-to-one mapping(Scenario 1.4) |
267 |
public void testPESSMISTIC_ES4() throws Exception{ |
268 |
// Cannot create parallel entity managers in the server. |
269 |
if (! isOnServer() && isSelectForUpateSupported()) { |
270 |
EntityManager em = createEntityManager(); |
271 |
Employee emp = null; |
272 |
Address ads = null; |
273 |
try{ |
274 |
beginTransaction(em); |
275 |
emp = new Employee(); |
276 |
ads = new Address("SomeStreet", "somecity", "province", "country", "postalcode"); |
277 |
emp.setAddress(ads); |
278 |
em.persist(emp); |
279 |
commitTransaction(em); |
280 |
}catch (RuntimeException ex){ |
281 |
throw ex; |
282 |
}finally{ |
283 |
if (isTransactionActive(em)){ |
284 |
rollbackTransaction(em); |
285 |
} |
286 |
closeEntityManager(em); |
287 |
} |
288 |
|
289 |
Exception lockTimeOutException = null; |
290 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
291 |
Map<String, Object> properties = new HashMap(); |
292 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.EXTENDED); |
293 |
EntityManager em1= createEntityManager(); |
294 |
|
295 |
try{ |
296 |
beginTransaction(em1); |
297 |
emp = em1.find(Employee.class, emp.getId()); |
298 |
em1.lock(emp, lockMode, properties); |
299 |
EntityManager em2 = createEntityManager(); |
300 |
try{ |
301 |
beginTransaction(em2); |
302 |
emp = em2.find(Employee.class, emp.getId()); |
303 |
Address address = null; |
304 |
emp.setAddress(address); |
305 |
commitTransaction(em2); |
306 |
}catch(javax.persistence.RollbackException ex){ |
307 |
if (ex.getMessage().indexOf("org.eclipse.persistence.exceptions.DatabaseException") == -1){ |
308 |
ex.printStackTrace(); |
309 |
fail("it's not the right exception"); |
310 |
} |
311 |
}finally{ |
312 |
if (isTransactionActive(em2)){ |
313 |
rollbackTransaction(em2); |
314 |
} |
315 |
closeEntityManager(em2); |
316 |
} |
317 |
}catch (Exception ex){ |
318 |
throw ex; |
319 |
}finally{ |
320 |
if (isTransactionActive(em1)){ |
321 |
rollbackTransaction(em1); |
322 |
} |
323 |
closeEntityManager(em1); |
324 |
} |
325 |
} |
326 |
} |
327 |
|
328 |
//Relationships owned by the entity that are contained in join tables will be locked with Unidirectional OneToMany mapping (Scenario 2.2) |
329 |
public void testPESSMISTIC_ES5() throws Exception{ |
330 |
// Cannot create parallel entity managers in the server. |
331 |
if (! isOnServer() && isSelectForUpateSupported()) { |
332 |
EntityManager em = createEntityManager(); |
333 |
EntyA entyA = null; |
334 |
EntyD entyD = null; |
335 |
try{ |
336 |
beginTransaction(em); |
337 |
entyA = new EntyA(); |
338 |
em.persist(entyA); |
339 |
entyA.getEntyDs().add(new EntyD()); |
340 |
commitTransaction(em); |
341 |
}catch (RuntimeException ex){ |
342 |
throw ex; |
343 |
}finally{ |
344 |
if (isTransactionActive(em)){ |
345 |
rollbackTransaction(em); |
346 |
} |
347 |
closeEntityManager(em); |
348 |
} |
349 |
|
350 |
Exception lockTimeOutException = null; |
351 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
352 |
Map<String, Object> properties = new HashMap(); |
353 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.EXTENDED); |
354 |
EntityManager em1= createEntityManager(); |
355 |
try{ |
356 |
beginTransaction(em1); |
357 |
entyA = em1.find(EntyA.class, entyA.getId()); |
358 |
em1.lock(entyA, lockMode, properties); |
359 |
EntityManager em2 = createEntityManager(); |
360 |
try{ |
361 |
beginTransaction(em2); |
362 |
entyA = em2.find(EntyA.class, entyA.getId()); |
363 |
entyA.setEntyDs(null); |
364 |
commitTransaction(em2); |
365 |
}catch(javax.persistence.RollbackException ex){ |
366 |
if (ex.getMessage().indexOf("org.eclipse.persistence.exceptions.DatabaseException") == -1){ |
367 |
ex.printStackTrace(); |
368 |
fail("it's not the right exception"); |
369 |
} |
370 |
}finally{ |
371 |
if (isTransactionActive(em2)){ |
372 |
rollbackTransaction(em2); |
373 |
} |
374 |
closeEntityManager(em2); |
375 |
} |
376 |
}catch (Exception ex){ |
377 |
throw ex; |
378 |
}finally{ |
379 |
if (isTransactionActive(em1)){ |
380 |
rollbackTransaction(em1); |
381 |
} |
382 |
closeEntityManager(em1); |
383 |
} |
384 |
} |
385 |
} |
386 |
|
387 |
//Relationships owned by the entity that are contained in join tables will be locked with Unidirectional ManyToMany mapping (Scenario 2.3) |
388 |
public void testPESSMISTIC_ES6() throws Exception{ |
389 |
// Cannot create parallel entity managers in the server. |
390 |
if (! isOnServer() && isSelectForUpateSupported()) { |
391 |
EntityManager em = createEntityManager(); |
392 |
EntyA entyA = null; |
393 |
EntyE entyE1, entyE2 = null; |
394 |
try{ |
395 |
beginTransaction(em); |
396 |
Collection entyEs = new ArrayList(); |
397 |
entyA = new EntyA(); |
398 |
entyE1 = new EntyE(); |
399 |
entyE2 = new EntyE(); |
400 |
entyEs.add(entyE1); |
401 |
entyEs.add(entyE2); |
402 |
entyA.setEntyEs(entyEs); |
403 |
em.persist(entyA); |
404 |
commitTransaction(em); |
405 |
}catch (RuntimeException ex){ |
406 |
throw ex; |
407 |
}finally{ |
408 |
if (isTransactionActive(em)){ |
409 |
rollbackTransaction(em); |
410 |
} |
411 |
closeEntityManager(em); |
412 |
} |
413 |
|
414 |
Exception lockTimeOutException = null; |
415 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
416 |
Map<String, Object> properties = new HashMap(); |
417 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.EXTENDED); |
418 |
EntityManager em1= createEntityManager(); |
419 |
try{ |
420 |
beginTransaction(em1); |
421 |
entyA = em1.find(EntyA.class, entyA.getId()); |
422 |
em1.lock(entyA, lockMode, properties); |
423 |
EntityManager em2 = createEntityManager(); |
424 |
try{ |
425 |
beginTransaction(em2); |
426 |
entyA = em2.find(EntyA.class, entyA.getId()); |
427 |
entyA.setEntyEs(null); |
428 |
commitTransaction(em2); |
429 |
}catch(javax.persistence.RollbackException ex){ |
430 |
if (ex.getMessage().indexOf("org.eclipse.persistence.exceptions.DatabaseException") == -1){ |
431 |
ex.printStackTrace(); |
432 |
fail("it's not the right exception"); |
433 |
} |
434 |
}finally{ |
435 |
if (isTransactionActive(em2)){ |
436 |
rollbackTransaction(em2); |
437 |
} |
438 |
closeEntityManager(em2); |
439 |
} |
440 |
}catch (Exception ex){ |
441 |
throw ex; |
442 |
}finally{ |
443 |
if (isTransactionActive(em1)){ |
444 |
rollbackTransaction(em1); |
445 |
} |
446 |
closeEntityManager(em1); |
447 |
} |
448 |
} |
449 |
} |
450 |
|
451 |
//Bidirectional OneToOne Relationship with target entity has foreign key, entity does not contain the foreign key will not be locked (Scenario 3.1) |
452 |
public void testPESSMISTIC_ES7() throws Exception{ |
453 |
// Cannot create parallel entity managers in the server. |
454 |
if (! isOnServer() && isSelectForUpateSupported()) { |
455 |
EntityManager em = createEntityManager(); |
456 |
EntyA a = null; |
457 |
EntyC c = null; |
458 |
try{ |
459 |
beginTransaction(em); |
460 |
a = new EntyA(); |
461 |
c = new EntyC(); |
462 |
em.persist(c); |
463 |
a.setEntyC(c); |
464 |
em.persist(a); |
465 |
commitTransaction(em); |
466 |
}catch (RuntimeException ex){ |
467 |
throw ex; |
468 |
}finally{ |
469 |
if (isTransactionActive(em)){ |
470 |
rollbackTransaction(em); |
471 |
} |
472 |
closeEntityManager(em); |
473 |
} |
474 |
|
475 |
Exception lockTimeOutException = null; |
476 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
477 |
Map<String, Object> properties = new HashMap(); |
478 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.NORMAL); |
479 |
EntityManager em1= createEntityManager(); |
480 |
|
481 |
try{ |
482 |
beginTransaction(em1); |
483 |
c = em1.find(EntyC.class, c.getId()); |
484 |
em1.lock(c, lockMode, properties); |
485 |
EntityManager em2 = createEntityManager(); |
486 |
try{ |
487 |
beginTransaction(em2); |
488 |
c = em2.find(EntyC.class, c.getId()); |
489 |
c.setEntyA(null); |
490 |
commitTransaction(em2); |
491 |
}catch(javax.persistence.RollbackException ex){ |
492 |
fail("it should not throw the exception!!!"); |
493 |
}finally{ |
494 |
if (isTransactionActive(em2)){ |
495 |
rollbackTransaction(em2); |
496 |
} |
497 |
closeEntityManager(em2); |
498 |
} |
499 |
}catch (Exception ex){ |
500 |
throw ex; |
501 |
}finally{ |
502 |
if (isTransactionActive(em1)){ |
503 |
rollbackTransaction(em1); |
504 |
} |
505 |
closeEntityManager(em1); |
506 |
} |
507 |
} |
508 |
} |
509 |
|
510 |
//Unidirectional OneToMany Relationship, in which entity does not contain the foreign key will not be locked (Scenario 3.2) |
511 |
public void testPESSMISTIC_ES8() throws Exception{ |
512 |
// Cannot create parallel entity managers in the server. |
513 |
if (! isOnServer() && isSelectForUpateSupported()) { |
514 |
EntityManager em = createEntityManager(); |
515 |
Employee emp = null; |
516 |
try{ |
517 |
beginTransaction(em); |
518 |
emp = new Employee(); |
519 |
emp.getDealers().add(new Dealer("Honda", "Kanata")); |
520 |
em.persist(emp); |
521 |
commitTransaction(em); |
522 |
}catch (RuntimeException ex){ |
523 |
throw ex; |
524 |
}finally{ |
525 |
if (isTransactionActive(em)){ |
526 |
rollbackTransaction(em); |
527 |
} |
528 |
closeEntityManager(em); |
529 |
} |
530 |
|
531 |
Exception lockTimeOutException = null; |
532 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
533 |
Map<String, Object> properties = new HashMap(); |
534 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.NORMAL); |
535 |
EntityManager em1= createEntityManager(); |
536 |
|
537 |
try{ |
538 |
beginTransaction(em1); |
539 |
emp = em1.find(Employee.class, emp.getId()); |
540 |
em1.lock(emp, lockMode, properties); |
541 |
EntityManager em2 = createEntityManager(); |
542 |
try{ |
543 |
beginTransaction(em2); |
544 |
emp = em1.find(Employee.class, emp.getId()); |
545 |
emp.setDealers(null); |
546 |
commitTransaction(em2); |
547 |
}catch(javax.persistence.RollbackException ex){ |
548 |
fail("it should not throw the exception!!!"); |
549 |
}finally{ |
550 |
if (isTransactionActive(em2)){ |
551 |
rollbackTransaction(em2); |
552 |
} |
553 |
closeEntityManager(em2); |
554 |
} |
555 |
}catch (Exception ex){ |
556 |
fail("it should not throw the exception!!!"); |
557 |
throw ex; |
558 |
}finally{ |
559 |
if (isTransactionActive(em1)){ |
560 |
rollbackTransaction(em1); |
561 |
} |
562 |
closeEntityManager(em1); |
563 |
} |
564 |
} |
565 |
} |
566 |
|
567 |
//Bidirectional ManyToMany Relationship, in which entity does not contain the foreign key will not be locked by default (Scenario 3.3) |
568 |
public void testPESSMISTIC_ES9() throws Exception{ |
569 |
// Cannot create parallel entity managers in the server. |
570 |
if (! isOnServer() && isSelectForUpateSupported()) { |
571 |
EntityManager em = createEntityManager(); |
572 |
Employee emp = null; |
573 |
try{ |
574 |
beginTransaction(em); |
575 |
emp = new Employee(); |
576 |
SmallProject smallProject = new SmallProject(); |
577 |
smallProject.setName("New High School Set Up"); |
578 |
emp.addProject(smallProject); |
579 |
LargeProject largeProject = new LargeProject(); |
580 |
largeProject.setName("Downtown Light Rail"); |
581 |
largeProject.setBudget(5000); |
582 |
emp.addProject(largeProject); |
583 |
em.persist(emp); |
584 |
commitTransaction(em); |
585 |
}catch (RuntimeException ex){ |
586 |
throw ex; |
587 |
}finally{ |
588 |
if (isTransactionActive(em)){ |
589 |
rollbackTransaction(em); |
590 |
} |
591 |
closeEntityManager(em); |
592 |
} |
593 |
|
594 |
Exception lockTimeOutException = null; |
595 |
LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE; |
596 |
Map<String, Object> properties = new HashMap(); |
597 |
properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.NORMAL); |
598 |
EntityManager em1= createEntityManager(); |
599 |
|
600 |
try{ |
601 |
beginTransaction(em1); |
602 |
emp = em1.find(Employee.class, emp.getId()); |
603 |
em1.lock(emp, lockMode, properties); |
604 |
EntityManager em2 = createEntityManager(); |
605 |
try{ |
606 |
beginTransaction(em2); |
607 |
emp = em1.find(Employee.class, emp.getId()); |
608 |
emp.setProjects(null); |
609 |
commitTransaction(em2); |
610 |
}catch(javax.persistence.RollbackException ex){ |
611 |
fail("it should not throw the exception!!!"); |
612 |
}finally{ |
613 |
if (isTransactionActive(em2)){ |
614 |
rollbackTransaction(em2); |
615 |
} |
616 |
closeEntityManager(em2); |
617 |
} |
618 |
}catch (Exception ex){ |
619 |
fail("it should not throw the exception!!!"); |
620 |
throw ex; |
621 |
}finally{ |
622 |
if (isTransactionActive(em1)){ |
623 |
rollbackTransaction(em1); |
624 |
} |
625 |
closeEntityManager(em1); |
626 |
} |
627 |
} |
628 |
} |
629 |
} |