1
|
package eu.etaxonomy.cdm.test.function;
|
2
|
|
3
|
import static org.junit.Assert.assertEquals;
|
4
|
import static org.junit.Assert.assertFalse;
|
5
|
import static org.junit.Assert.assertNotSame;
|
6
|
import static org.junit.Assert.assertNull;
|
7
|
import static org.junit.Assert.assertSame;
|
8
|
import static org.junit.Assert.assertTrue;
|
9
|
|
10
|
import java.util.UUID;
|
11
|
|
12
|
import javax.sql.DataSource;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.hibernate.LockMode;
|
16
|
import org.hibernate.Session;
|
17
|
import org.hibernate.SessionFactory;
|
18
|
import org.junit.After;
|
19
|
import org.junit.Before;
|
20
|
import org.junit.Ignore;
|
21
|
import org.junit.Test;
|
22
|
import org.springframework.core.io.ClassPathResource;
|
23
|
import org.springframework.core.io.Resource;
|
24
|
import org.springframework.transaction.PlatformTransactionManager;
|
25
|
import org.springframework.transaction.TransactionDefinition;
|
26
|
import org.springframework.transaction.TransactionStatus;
|
27
|
import org.unitils.database.annotations.Transactional;
|
28
|
import org.unitils.database.util.TransactionMode;
|
29
|
import org.unitils.dbunit.annotation.DataSet;
|
30
|
import org.unitils.spring.annotation.SpringBeanByType;
|
31
|
|
32
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
33
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
34
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
35
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
36
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
37
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
40
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
41
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
42
|
|
43
|
/**
|
44
|
* TODO refactor the Transaction Handling into Conversation Manager
|
45
|
*
|
46
|
* @author n.hoffmann
|
47
|
*
|
48
|
*/
|
49
|
@Transactional(TransactionMode.DISABLED)
|
50
|
public class TestConcurrentSession extends CdmIntegrationTest{
|
51
|
|
52
|
private static final Logger logger = Logger.getLogger(TestConcurrentSession.class);
|
53
|
|
54
|
@SpringBeanByType
|
55
|
private SessionFactory sessionFactory;
|
56
|
|
57
|
@SpringBeanByType
|
58
|
private PlatformTransactionManager transactionManager;
|
59
|
|
60
|
@SpringBeanByType
|
61
|
private ITaxonService taxonService;
|
62
|
|
63
|
@SpringBeanByType
|
64
|
private IReferenceService referenceService;
|
65
|
|
66
|
@SpringBeanByType
|
67
|
private DataSource dataSource;
|
68
|
|
69
|
@SpringBeanByType
|
70
|
private ITaxonDao taxonDao;
|
71
|
|
72
|
Resource applicationContextResource = new ClassPathResource("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml");
|
73
|
|
74
|
|
75
|
|
76
|
private ConversationHolder conversationHolder1;
|
77
|
private ConversationHolder conversationHolder2;
|
78
|
private ConversationHolder conversationHolder3;
|
79
|
|
80
|
private UUID taxonUuid1 = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
|
81
|
private UUID taxonUuid2 = UUID.fromString("822d98dc-9ef7-44b7-a870-94573a3bcb46");
|
82
|
private UUID taxonUuid3 = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
|
83
|
|
84
|
private UUID referenceUuid1 = UUID.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2");
|
85
|
private UUID referenceUuid2 = UUID.fromString("ad4322b7-4b05-48af-be70-f113e46c545e");
|
86
|
|
87
|
/**
|
88
|
*
|
89
|
*/
|
90
|
private TransactionDefinition definition = null;
|
91
|
|
92
|
|
93
|
|
94
|
@Before
|
95
|
public void setup(){
|
96
|
conversationHolder1 = new ConversationHolder(dataSource, sessionFactory, transactionManager);
|
97
|
conversationHolder2 = new ConversationHolder(dataSource, sessionFactory, transactionManager);
|
98
|
conversationHolder3 = new ConversationHolder(dataSource, sessionFactory, transactionManager);
|
99
|
}
|
100
|
|
101
|
@After
|
102
|
public void tearDown(){
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Test the general possibility to open two sessions at the same time
|
107
|
*/
|
108
|
@Test
|
109
|
@DataSet("ConcurrentSessionTest.xml")
|
110
|
public void testTwoSessions(){
|
111
|
|
112
|
conversationHolder1.bind();
|
113
|
conversationHolder1.startTransaction();
|
114
|
int context1Count = taxonDao.count();
|
115
|
|
116
|
conversationHolder2.bind();
|
117
|
int context2Count = taxonDao.count();
|
118
|
|
119
|
assertNotSame("The contexts sessions should be distinct.", conversationHolder1.getSession(), conversationHolder2.getSession());
|
120
|
assertEquals("Both contexts should yield the same results(at least if " +
|
121
|
"there where no write operations in between)", context1Count, context2Count);
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Getting the same taxon from two different sessions. However, the resulting objects should not be the same.
|
126
|
*/
|
127
|
@Test
|
128
|
@DataSet("ConcurrentSessionTest.xml")
|
129
|
public void testTwoSessionsEqualTaxon(){
|
130
|
|
131
|
conversationHolder1.bind();
|
132
|
TaxonBase taxonBase1 = taxonDao.findByUuid(taxonUuid1);
|
133
|
|
134
|
conversationHolder2.bind();
|
135
|
TaxonBase taxonBase2 = taxonDao.findByUuid(taxonUuid1);
|
136
|
|
137
|
assertEquals("The objects should be equal.", taxonBase1, taxonBase2);
|
138
|
assertNotSame("The objects should not be the same.", taxonBase1, taxonBase2);
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
*
|
143
|
*/
|
144
|
@Test
|
145
|
@DataSet("ConcurrentSessionTest.xml")
|
146
|
public void testTwoSessionsEqualTaxonWithTaxonService(){
|
147
|
conversationHolder1.bind();
|
148
|
TaxonBase taxonBase1 = taxonService.getTaxonByUuid(taxonUuid1);
|
149
|
|
150
|
conversationHolder2.bind();
|
151
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid1);
|
152
|
|
153
|
assertEquals("The objects should be equal", taxonBase1, taxonBase2);
|
154
|
assertNotSame("The objects should not be the same", taxonBase1, taxonBase2);
|
155
|
}
|
156
|
|
157
|
|
158
|
/**
|
159
|
* The session should still be open after committing a transaction.
|
160
|
*/
|
161
|
@Test
|
162
|
@DataSet("ConcurrentSessionTest.xml")
|
163
|
public void testLongConversationWithMultipleTransactions(){
|
164
|
|
165
|
conversationHolder1.bind();
|
166
|
TransactionStatus txStatusOne = conversationHolder1.startTransaction();
|
167
|
TaxonBase taxonBase1 = taxonDao.findByUuid(taxonUuid1);
|
168
|
TaxonNameBase taxonName1 = taxonBase1.getName();
|
169
|
conversationHolder1.commit();
|
170
|
|
171
|
|
172
|
conversationHolder1.bind();
|
173
|
TransactionStatus txStatusTwo = conversationHolder1.startTransaction();
|
174
|
TaxonNameBase taxonName2 = taxonBase1.getName();
|
175
|
conversationHolder1.commit();
|
176
|
|
177
|
assertNotSame(txStatusOne, txStatusTwo);
|
178
|
assertSame("Two objects from different transactions should be the same, because we are still in " +
|
179
|
"same persistence context", taxonName1, taxonName2);
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Switching sessions. Should not throw any exceptions
|
184
|
*/
|
185
|
@Test
|
186
|
@DataSet("ConcurrentSessionTest.xml")
|
187
|
public void testSwitchSessions(){
|
188
|
|
189
|
conversationHolder1.bind();
|
190
|
TransactionStatus txStatusOne = transactionManager.getTransaction(definition);
|
191
|
TaxonBase taxonBase1 = taxonService.getTaxonByUuid(taxonUuid1);
|
192
|
|
193
|
conversationHolder2.bind();
|
194
|
TransactionStatus txStatusTwo = transactionManager.getTransaction(definition );
|
195
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid1);
|
196
|
|
197
|
conversationHolder1.bind();
|
198
|
TaxonBase taxonBase3 = taxonService.getTaxonByUuid(taxonUuid2);
|
199
|
|
200
|
|
201
|
}
|
202
|
|
203
|
@Test
|
204
|
@DataSet("ConcurrentSessionTest.xml")
|
205
|
public void testReaccessingTheSameLazyLoadedObjectInTwoDifferentTransactions(){
|
206
|
|
207
|
conversationHolder1.bind();
|
208
|
TransactionStatus tx1 = conversationHolder1.startTransaction();
|
209
|
TaxonBase t1 = taxonService.getTaxonByUuid(taxonUuid1);
|
210
|
|
211
|
TaxonNameBase n1 = t1.getName();
|
212
|
TransactionStatus tx2 = conversationHolder1.commit(true);
|
213
|
|
214
|
// I wonder if this breaks
|
215
|
TaxonNameBase n2 = t1.getName();
|
216
|
|
217
|
assertSame(n1, n2);
|
218
|
assertNotSame(tx1, tx2);
|
219
|
|
220
|
}
|
221
|
|
222
|
@Test
|
223
|
@Ignore
|
224
|
/*
|
225
|
* for some reason object identity is not guaranteed anymore when accessing the
|
226
|
* same object in the same session but in different transactions. Because of that
|
227
|
* this test fails, and IMHO it should not. I am not quite sure if this is a bug.
|
228
|
*/
|
229
|
@DataSet("ConcurrentSessionTest.xml")
|
230
|
public void testReaccessingTheSameObjectInTwoDifferentTransactions(){
|
231
|
|
232
|
conversationHolder1.bind();
|
233
|
TransactionStatus tx1 = conversationHolder1.startTransaction();
|
234
|
TaxonBase t1 = taxonService.getTaxonByUuid(taxonUuid1);
|
235
|
|
236
|
TransactionStatus tx2 = conversationHolder1.commit(true);
|
237
|
|
238
|
// I wonder if this breaks
|
239
|
TaxonBase t2 = taxonService.getTaxonByUuid(taxonUuid1);
|
240
|
|
241
|
assertSame(t1, t2);// TODO this fails, why????
|
242
|
assertNotSame(tx1, tx2);
|
243
|
|
244
|
}
|
245
|
|
246
|
@Test
|
247
|
@DataSet("ConcurrentSessionTest.xml")
|
248
|
public void testReaccessingTheSameObjectInSameTransaction(){
|
249
|
|
250
|
conversationHolder1.bind();
|
251
|
conversationHolder1.startTransaction();
|
252
|
TaxonBase t1 = taxonService.getTaxonByUuid(taxonUuid1);
|
253
|
|
254
|
// I wonder if this breaks
|
255
|
TaxonBase t2 = taxonService.getTaxonByUuid(taxonUuid1);
|
256
|
|
257
|
assertSame(t1, t2);
|
258
|
|
259
|
}
|
260
|
|
261
|
|
262
|
|
263
|
/**
|
264
|
* Load an object, manipulate it and persist it by committing the transaction.
|
265
|
* When reloading the same object we should still be in the same session
|
266
|
*/
|
267
|
@Test
|
268
|
@DataSet("ConcurrentSessionTest.xml")
|
269
|
public void testSavingAndReaccessingTheSameObject(){
|
270
|
|
271
|
TestConversationEnabled testConversationEnabled = new TestConversationEnabled();
|
272
|
|
273
|
conversationHolder1.registerForDataStoreChanges(testConversationEnabled);
|
274
|
|
275
|
conversationHolder1.bind();
|
276
|
TransactionStatus txStatusOne = conversationHolder1.startTransaction();
|
277
|
Session sessionFirstTransaction = conversationHolder1.getSession();
|
278
|
TaxonBase taxonBase = taxonService.getTaxonByUuid(taxonUuid1);
|
279
|
TaxonNameBase newTaxonName = BotanicalName.NewInstance(null);
|
280
|
|
281
|
conversationHolder1.bind();
|
282
|
newTaxonName.addTaxonBase(taxonBase);
|
283
|
|
284
|
conversationHolder1.bind();
|
285
|
taxonService.save(taxonBase);
|
286
|
conversationHolder1.commit();
|
287
|
|
288
|
|
289
|
conversationHolder1.bind();
|
290
|
TransactionStatus txStatusTwo = conversationHolder1.startTransaction();
|
291
|
Session sessionSecondTransaction = conversationHolder1.getSession();
|
292
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid1);
|
293
|
conversationHolder1.commit();
|
294
|
|
295
|
assertEquals("The taxa should be equal.", taxonBase, taxonBase2);
|
296
|
|
297
|
|
298
|
// assertSame("The taxa should be the same.", taxonBase, taxonBase2);
|
299
|
assertSame("The sessions should be the same.", sessionFirstTransaction, sessionSecondTransaction);
|
300
|
|
301
|
assertEquals("The name objects should be the same.", taxonBase.getName(), taxonBase2.getName());
|
302
|
|
303
|
|
304
|
// assertSame("The name objects should be the same.", taxonBase.getName(), taxonBase2.getName());
|
305
|
}
|
306
|
|
307
|
/**
|
308
|
* We load the same taxon in two different sessions. The reference of the first
|
309
|
* taxon gets manipulated and the taxon saved afterwards. When trying to persist the other
|
310
|
* taxon we would expect some form of exception.
|
311
|
*
|
312
|
* TODO it is not quite clear to me what really should happen here. Right now it seems like
|
313
|
* we do not have any locking at all. Needs further investigation.
|
314
|
*
|
315
|
* UPDATE we indeed have no locking. Last write wins!
|
316
|
*/
|
317
|
@Test
|
318
|
@Ignore
|
319
|
@DataSet("ConcurrentSessionTest.xml")
|
320
|
public void testWhatHappensWhenEncounteringStaleData(){
|
321
|
conversationHolder1.bind();
|
322
|
TransactionStatus txStatusOne = conversationHolder1.startTransaction();
|
323
|
TaxonBase taxonBase1 = taxonService.getTaxonByUuid(taxonUuid1);
|
324
|
|
325
|
conversationHolder2.bind();
|
326
|
TransactionStatus txStatusTwo = conversationHolder2.startTransaction();
|
327
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid1);
|
328
|
|
329
|
conversationHolder1.bind();
|
330
|
ReferenceBase reference1 = referenceService.getReferenceByUuid(referenceUuid1);
|
331
|
assertSame("This should be the sec", taxonBase1.getSec(), reference1);
|
332
|
|
333
|
ReferenceBase reference2 = referenceService.getReferenceByUuid(referenceUuid2);
|
334
|
taxonBase1.setSec(reference2);
|
335
|
taxonService.save(taxonBase1);
|
336
|
conversationHolder1.commit();
|
337
|
|
338
|
conversationHolder2.bind();
|
339
|
taxonBase2.setSec(null);
|
340
|
taxonService.save(taxonBase2);
|
341
|
conversationHolder2.commit();
|
342
|
|
343
|
conversationHolder3.bind();
|
344
|
TaxonBase taxonBase3 = taxonService.getTaxonByUuid(taxonUuid1);
|
345
|
assertNull(taxonBase3.getSec());
|
346
|
}
|
347
|
|
348
|
/**
|
349
|
* We want to know if the data really gets persisted.
|
350
|
*/
|
351
|
@Test
|
352
|
@DataSet("ConcurrentSessionTest.xml")
|
353
|
public void testProofOfDataPersistency(){
|
354
|
// first conversation
|
355
|
conversationHolder1.bind();
|
356
|
conversationHolder1.startTransaction();
|
357
|
// get a taxon
|
358
|
TaxonBase taxonBase = taxonService.getTaxonByUuid(taxonUuid1);
|
359
|
// get a reference
|
360
|
ReferenceBase reference = referenceService.getReferenceByUuid(referenceUuid2);
|
361
|
// make sure
|
362
|
assertNotSame("this reference should not be the taxons sec.", taxonBase.getSec(), reference);
|
363
|
// set the reference as the taxons new sec
|
364
|
taxonBase.setSec(reference);
|
365
|
// save and commit
|
366
|
taxonService.save(taxonBase);
|
367
|
conversationHolder1.commit();
|
368
|
|
369
|
|
370
|
// second conversation
|
371
|
conversationHolder2.bind();
|
372
|
conversationHolder2.startTransaction();
|
373
|
// load the same taxon in a different session
|
374
|
TaxonBase taxonBaseInSecondTransaction = taxonService.getTaxonByUuid(taxonUuid1);
|
375
|
// load the reference
|
376
|
ReferenceBase referenceInSecondTransaction = referenceService.getReferenceByUuid(referenceUuid2);
|
377
|
// we assume that
|
378
|
assertSame("The reference should be the sec now.", taxonBaseInSecondTransaction.getSec(), referenceInSecondTransaction);
|
379
|
assertNotSame("The reference should not be the same object as in first transaction.", reference, referenceInSecondTransaction);
|
380
|
}
|
381
|
|
382
|
/**
|
383
|
* The same test as before but uses the conversation managers transaction
|
384
|
*/
|
385
|
@Test
|
386
|
@DataSet("ConcurrentSessionTest.xml")
|
387
|
public void testProofOfDataPersistencyNewTransactionModel(){
|
388
|
// first conversation
|
389
|
conversationHolder1.bind();
|
390
|
conversationHolder1.startTransaction();
|
391
|
// get a taxon
|
392
|
TaxonBase taxonBase = taxonService.getTaxonByUuid(taxonUuid1);
|
393
|
// get a reference
|
394
|
ReferenceBase reference = referenceService.getReferenceByUuid(referenceUuid2);
|
395
|
// make sure
|
396
|
assertNotSame("this reference should not be the taxons sec.", taxonBase.getSec(), reference);
|
397
|
// set the reference as the taxons new sec
|
398
|
taxonBase.setSec(reference);
|
399
|
// save and commit
|
400
|
taxonService.save(taxonBase);
|
401
|
conversationHolder1.commit();
|
402
|
|
403
|
|
404
|
// second conversation
|
405
|
conversationHolder2.bind();
|
406
|
conversationHolder2.startTransaction();
|
407
|
// load the same taxon in a different session
|
408
|
TaxonBase taxonBaseInSecondTransaction = taxonService.getTaxonByUuid(taxonUuid1);
|
409
|
// load the reference
|
410
|
ReferenceBase referenceInSecondTransaction = referenceService.getReferenceByUuid(referenceUuid2);
|
411
|
// we assume that
|
412
|
assertSame("The reference should be the sec now.", taxonBaseInSecondTransaction.getSec(), referenceInSecondTransaction);
|
413
|
assertNotSame("The reference should not be the same object as in first transaction.", reference, referenceInSecondTransaction);
|
414
|
}
|
415
|
|
416
|
/**
|
417
|
* We manipulate an object in one session and see how these change get propagated to the other session.
|
418
|
*/
|
419
|
// @Ignore
|
420
|
@Test
|
421
|
@DataSet("ConcurrentSessionTest.xml")
|
422
|
public void testIfDataGetsPersistedWhenFiringCommit(){
|
423
|
// first conversation
|
424
|
conversationHolder1.bind();
|
425
|
conversationHolder1.startTransaction();
|
426
|
// get a taxon
|
427
|
TaxonBase taxonBase = taxonService.getTaxonByUuid(taxonUuid1);
|
428
|
// get a reference
|
429
|
ReferenceBase reference = referenceService.getReferenceByUuid(referenceUuid2);
|
430
|
// make sure
|
431
|
assertTrue(! taxonBase.getSec().equals(reference));
|
432
|
assertNotSame("this reference should not be the taxons sec.", taxonBase.getSec(), reference);
|
433
|
// set the reference as the taxons new sec
|
434
|
taxonBase.setSec(reference);
|
435
|
// save and commit
|
436
|
taxonService.save(taxonBase);
|
437
|
|
438
|
// second conversation
|
439
|
conversationHolder2.bind();
|
440
|
// load the same taxon in a different session, since we did not commit the first transaction,
|
441
|
// the reference change did not make its way to the database and the references should be distinct
|
442
|
TaxonBase taxonBaseInSecondTransaction = taxonService.getTaxonByUuid(taxonUuid1);
|
443
|
assertFalse(taxonBase.getSec().equals(taxonBaseInSecondTransaction.getSec()));
|
444
|
|
445
|
// commit the first transaction
|
446
|
conversationHolder1.bind();
|
447
|
conversationHolder1.commit();
|
448
|
|
449
|
// as the taxonBaseInSecondTransaction still has it's data from before the first transaction was committed
|
450
|
// we assume that the references are still not equal
|
451
|
assertFalse(taxonBase.getSec().equals(taxonBaseInSecondTransaction.getSec()));
|
452
|
|
453
|
// we call a refresh on the taxonBaseInSecondTransaction to synchronize its state with the database
|
454
|
conversationHolder2.bind();
|
455
|
taxonService.refresh(taxonBaseInSecondTransaction);
|
456
|
|
457
|
// the objects should now be equal
|
458
|
|
459
|
assertTrue(taxonBase.getSec().equals(taxonBaseInSecondTransaction.getSec()));
|
460
|
}
|
461
|
|
462
|
/**
|
463
|
* This should not throw exceptions
|
464
|
*/
|
465
|
@Test
|
466
|
@DataSet("ConcurrentSessionTest.xml")
|
467
|
public void testMultipleTransactionsInOneSession(){
|
468
|
conversationHolder1.bind();
|
469
|
conversationHolder1.startTransaction();
|
470
|
TaxonBase taxonBase1 = taxonService.getTaxonByUuid(taxonUuid1);
|
471
|
conversationHolder1.commit();
|
472
|
|
473
|
conversationHolder1.startTransaction();
|
474
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid2);
|
475
|
conversationHolder1.commit();
|
476
|
}
|
477
|
|
478
|
@Test
|
479
|
@DataSet("ConcurrentSessionTest.xml")
|
480
|
public void testMultipleTransactionsInMultipleSessions(){
|
481
|
conversationHolder1.bind();
|
482
|
conversationHolder1.startTransaction();
|
483
|
|
484
|
conversationHolder2.bind();
|
485
|
conversationHolder2.startTransaction();
|
486
|
|
487
|
conversationHolder3.bind();
|
488
|
conversationHolder3.startTransaction();
|
489
|
}
|
490
|
|
491
|
|
492
|
@Test
|
493
|
@DataSet("ConcurrentSessionTest.xml")
|
494
|
public void testInsert(){
|
495
|
TestConversationEnabled testConversationEnabled = new TestConversationEnabled();
|
496
|
|
497
|
conversationHolder1.registerForDataStoreChanges(testConversationEnabled);
|
498
|
|
499
|
conversationHolder1.bind();
|
500
|
conversationHolder1.startTransaction();
|
501
|
TaxonBase newTaxon = Taxon.NewInstance(null, null);
|
502
|
taxonService.save(newTaxon);
|
503
|
conversationHolder1.commit();
|
504
|
}
|
505
|
|
506
|
@Test
|
507
|
@DataSet("ConcurrentSessionTest.xml")
|
508
|
public void testUpdate(){
|
509
|
TestConversationEnabled testConversationEnabled = new TestConversationEnabled();
|
510
|
|
511
|
conversationHolder1.registerForDataStoreChanges(testConversationEnabled);
|
512
|
|
513
|
conversationHolder1.bind();
|
514
|
conversationHolder1.startTransaction();
|
515
|
TaxonBase taxonBase1 = taxonService.getTaxonByUuid(taxonUuid1);
|
516
|
ReferenceBase reference = referenceService.getReferenceByUuid(referenceUuid2);
|
517
|
taxonBase1.setSec(reference);
|
518
|
taxonService.save(taxonBase1);
|
519
|
conversationHolder1.commit();
|
520
|
}
|
521
|
|
522
|
@Test
|
523
|
@DataSet("ConcurrentSessionTest.xml")
|
524
|
public void testMultipleSessionSwitching(){
|
525
|
conversationHolder1.bind();
|
526
|
TaxonBase taxon1 = taxonService.getTaxonByUuid(taxonUuid1);
|
527
|
assertSame(conversationHolder1.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
528
|
|
529
|
conversationHolder2.bind();
|
530
|
TaxonBase taxon2 = taxonService.getTaxonByUuid(taxonUuid2);
|
531
|
assertSame(conversationHolder2.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
532
|
|
533
|
conversationHolder3.bind();
|
534
|
TaxonBase taxon3 = taxonService.getTaxonByUuid(taxonUuid1);
|
535
|
assertSame(conversationHolder3.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
536
|
|
537
|
conversationHolder2.bind();
|
538
|
TaxonNameBase name2 = taxon2.getName();
|
539
|
assertSame(conversationHolder2.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
540
|
|
541
|
conversationHolder3.bind();
|
542
|
TaxonNameBase name3 = taxon3.getName();
|
543
|
assertSame(conversationHolder3.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
544
|
|
545
|
// Lazy loading somehow works without binding the session first
|
546
|
TaxonNameBase name1 = taxon1.getName();
|
547
|
assertNotSame(name1, name3);
|
548
|
assertEquals(name1, name3);
|
549
|
assertNotSame(conversationHolder1.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
550
|
|
551
|
}
|
552
|
|
553
|
@Test
|
554
|
@DataSet("ConcurrentSessionTest.xml")
|
555
|
public void testMultipleSessionSwitchingInTransactions(){
|
556
|
conversationHolder1.bind();
|
557
|
conversationHolder1.startTransaction();
|
558
|
TaxonBase taxon1 = taxonService.getTaxonByUuid(taxonUuid1);
|
559
|
assertSame(conversationHolder1.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
560
|
conversationHolder1.commit();
|
561
|
|
562
|
conversationHolder2.bind();
|
563
|
conversationHolder2.startTransaction();
|
564
|
TaxonBase taxon2 = taxonService.getTaxonByUuid(taxonUuid2);
|
565
|
assertSame(conversationHolder2.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
566
|
conversationHolder2.commit();
|
567
|
|
568
|
conversationHolder3.bind();
|
569
|
conversationHolder3.startTransaction();
|
570
|
TaxonBase taxon3 = taxonService.getTaxonByUuid(taxonUuid1);
|
571
|
assertSame(conversationHolder3.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
572
|
conversationHolder3.commit();
|
573
|
|
574
|
conversationHolder2.bind();
|
575
|
conversationHolder2.startTransaction();
|
576
|
TaxonNameBase name2 = taxon2.getName();
|
577
|
assertSame(conversationHolder2.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
578
|
conversationHolder2.commit();
|
579
|
|
580
|
conversationHolder3.bind();
|
581
|
conversationHolder3.startTransaction();
|
582
|
TaxonNameBase name3 = taxon3.getName();
|
583
|
assertSame(conversationHolder3.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
584
|
|
585
|
// Lazy loading somehow works without binding the session first
|
586
|
TaxonNameBase name1 = taxon1.getName();
|
587
|
assertNotSame(name1, name3);
|
588
|
assertEquals(name1, name3);
|
589
|
assertNotSame(conversationHolder1.getSession(), conversationHolder1.getSessionFactory().getCurrentSession());
|
590
|
|
591
|
}
|
592
|
|
593
|
|
594
|
/**
|
595
|
* this is a locking playground
|
596
|
*/
|
597
|
@Test
|
598
|
@DataSet("ConcurrentSessionTest.xml")
|
599
|
public void testLocking(){
|
600
|
conversationHolder1.bind();
|
601
|
// first session, first transaction
|
602
|
conversationHolder1.startTransaction();
|
603
|
TaxonBase taxonBase = taxonService.getTaxonByUuid(taxonUuid1);
|
604
|
// leave the first transaction without committing it
|
605
|
|
606
|
// start a new session with a new transaction
|
607
|
conversationHolder2.bind();
|
608
|
conversationHolder2.startTransaction();
|
609
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid1);
|
610
|
taxonBase.setSec(null);
|
611
|
conversationHolder2.commit();
|
612
|
// transaction of the second session got committed
|
613
|
|
614
|
// return to the first session and commit its transaction
|
615
|
conversationHolder1.bind();
|
616
|
conversationHolder1.commit();
|
617
|
|
618
|
conversationHolder1.startTransaction();
|
619
|
conversationHolder1.lock(taxonBase, LockMode.READ);
|
620
|
|
621
|
}
|
622
|
|
623
|
|
624
|
}
|