1 |
7a1d87e3
|
a.babadshanjan
|
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 |
89fc9bb6
|
n.hoffmann
|
import static org.junit.Assert.assertNull;
|
7 |
7a1d87e3
|
a.babadshanjan
|
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 |
fc83c486
|
n.hoffmann
|
import org.springframework.core.io.ClassPathResource;
|
23 |
|
|
import org.springframework.core.io.Resource;
|
24 |
7a1d87e3
|
a.babadshanjan
|
import org.springframework.transaction.PlatformTransactionManager;
|
25 |
|
|
import org.springframework.transaction.TransactionDefinition;
|
26 |
|
|
import org.springframework.transaction.TransactionStatus;
|
27 |
fc83c486
|
n.hoffmann
|
import org.unitils.database.annotations.Transactional;
|
28 |
|
|
import org.unitils.database.util.TransactionMode;
|
29 |
7a1d87e3
|
a.babadshanjan
|
import org.unitils.dbunit.annotation.DataSet;
|
30 |
|
|
import org.unitils.spring.annotation.SpringBeanByType;
|
31 |
|
|
|
32 |
6ae61fd7
|
n.hoffmann
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
33 |
7a1d87e3
|
a.babadshanjan
|
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 |
6ae61fd7
|
n.hoffmann
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39 |
7a1d87e3
|
a.babadshanjan
|
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 |
fc83c486
|
n.hoffmann
|
@Transactional(TransactionMode.DISABLED)
|
50 |
6ae61fd7
|
n.hoffmann
|
public class TestConcurrentSession extends CdmIntegrationTest{
|
51 |
7a1d87e3
|
a.babadshanjan
|
|
52 |
6ae61fd7
|
n.hoffmann
|
private static final Logger logger = Logger.getLogger(TestConcurrentSession.class);
|
53 |
7a1d87e3
|
a.babadshanjan
|
|
54 |
fc83c486
|
n.hoffmann
|
@SpringBeanByType
|
55 |
7a1d87e3
|
a.babadshanjan
|
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 |
fc83c486
|
n.hoffmann
|
Resource applicationContextResource = new ClassPathResource("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml");
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
7a1d87e3
|
a.babadshanjan
|
private ConversationHolder conversationHolder1;
|
77 |
|
|
private ConversationHolder conversationHolder2;
|
78 |
fc83c486
|
n.hoffmann
|
private ConversationHolder conversationHolder3;
|
79 |
7a1d87e3
|
a.babadshanjan
|
|
80 |
|
|
private UUID taxonUuid1 = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
|
81 |
|
|
private UUID taxonUuid2 = UUID.fromString("822d98dc-9ef7-44b7-a870-94573a3bcb46");
|
82 |
fc83c486
|
n.hoffmann
|
private UUID taxonUuid3 = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
|
83 |
7a1d87e3
|
a.babadshanjan
|
|
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 |
fc83c486
|
n.hoffmann
|
|
112 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
113 |
fc83c486
|
n.hoffmann
|
conversationHolder1.startTransaction();
|
114 |
7a1d87e3
|
a.babadshanjan
|
int context1Count = taxonDao.count();
|
115 |
|
|
|
116 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
117 |
7a1d87e3
|
a.babadshanjan
|
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 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
132 |
7a1d87e3
|
a.babadshanjan
|
TaxonBase taxonBase1 = taxonDao.findByUuid(taxonUuid1);
|
133 |
|
|
|
134 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
135 |
7a1d87e3
|
a.babadshanjan
|
TaxonBase taxonBase2 = taxonDao.findByUuid(taxonUuid1);
|
136 |
|
|
|
137 |
fc83c486
|
n.hoffmann
|
assertEquals("The objects should be equal.", taxonBase1, taxonBase2);
|
138 |
|
|
assertNotSame("The objects should not be the same.", taxonBase1, taxonBase2);
|
139 |
7a1d87e3
|
a.babadshanjan
|
}
|
140 |
|
|
|
141 |
|
|
/**
|
142 |
|
|
*
|
143 |
|
|
*/
|
144 |
|
|
@Test
|
145 |
|
|
@DataSet("ConcurrentSessionTest.xml")
|
146 |
|
|
public void testTwoSessionsEqualTaxonWithTaxonService(){
|
147 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
148 |
7a1d87e3
|
a.babadshanjan
|
TaxonBase taxonBase1 = taxonService.getTaxonByUuid(taxonUuid1);
|
149 |
|
|
|
150 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
151 |
7a1d87e3
|
a.babadshanjan
|
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 |
fc83c486
|
n.hoffmann
|
conversationHolder1.bind();
|
166 |
|
|
TransactionStatus txStatusOne = conversationHolder1.startTransaction();
|
167 |
7a1d87e3
|
a.babadshanjan
|
TaxonBase taxonBase1 = taxonDao.findByUuid(taxonUuid1);
|
168 |
|
|
TaxonNameBase taxonName1 = taxonBase1.getName();
|
169 |
fc83c486
|
n.hoffmann
|
conversationHolder1.commit();
|
170 |
|
|
|
171 |
7a1d87e3
|
a.babadshanjan
|
|
172 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
173 |
fc83c486
|
n.hoffmann
|
TransactionStatus txStatusTwo = conversationHolder1.startTransaction();
|
174 |
7a1d87e3
|
a.babadshanjan
|
TaxonNameBase taxonName2 = taxonBase1.getName();
|
175 |
fc83c486
|
n.hoffmann
|
conversationHolder1.commit();
|
176 |
7a1d87e3
|
a.babadshanjan
|
|
177 |
fc83c486
|
n.hoffmann
|
assertNotSame(txStatusOne, txStatusTwo);
|
178 |
7a1d87e3
|
a.babadshanjan
|
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 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
190 |
7a1d87e3
|
a.babadshanjan
|
TransactionStatus txStatusOne = transactionManager.getTransaction(definition);
|
191 |
|
|
TaxonBase taxonBase1 = taxonService.getTaxonByUuid(taxonUuid1);
|
192 |
|
|
|
193 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
194 |
7a1d87e3
|
a.babadshanjan
|
TransactionStatus txStatusTwo = transactionManager.getTransaction(definition );
|
195 |
|
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid1);
|
196 |
|
|
|
197 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
198 |
7a1d87e3
|
a.babadshanjan
|
TaxonBase taxonBase3 = taxonService.getTaxonByUuid(taxonUuid2);
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
}
|
202 |
|
|
|
203 |
fc83c486
|
n.hoffmann
|
@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 |
7a1d87e3
|
a.babadshanjan
|
/**
|
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 |
fc83c486
|
n.hoffmann
|
TestConversationEnabled testConversationEnabled = new TestConversationEnabled();
|
272 |
|
|
|
273 |
|
|
conversationHolder1.registerForDataStoreChanges(testConversationEnabled);
|
274 |
|
|
|
275 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
276 |
fc83c486
|
n.hoffmann
|
TransactionStatus txStatusOne = conversationHolder1.startTransaction();
|
277 |
7a1d87e3
|
a.babadshanjan
|
Session sessionFirstTransaction = conversationHolder1.getSession();
|
278 |
|
|
TaxonBase taxonBase = taxonService.getTaxonByUuid(taxonUuid1);
|
279 |
|
|
TaxonNameBase newTaxonName = BotanicalName.NewInstance(null);
|
280 |
|
|
|
281 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
282 |
7a1d87e3
|
a.babadshanjan
|
newTaxonName.addTaxonBase(taxonBase);
|
283 |
|
|
|
284 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
285 |
7a1d87e3
|
a.babadshanjan
|
taxonService.save(taxonBase);
|
286 |
fc83c486
|
n.hoffmann
|
conversationHolder1.commit();
|
287 |
7a1d87e3
|
a.babadshanjan
|
|
288 |
|
|
|
289 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
290 |
fc83c486
|
n.hoffmann
|
TransactionStatus txStatusTwo = conversationHolder1.startTransaction();
|
291 |
7a1d87e3
|
a.babadshanjan
|
Session sessionSecondTransaction = conversationHolder1.getSession();
|
292 |
|
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid1);
|
293 |
fc83c486
|
n.hoffmann
|
conversationHolder1.commit();
|
294 |
7a1d87e3
|
a.babadshanjan
|
|
295 |
fc83c486
|
n.hoffmann
|
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 |
7a1d87e3
|
a.babadshanjan
|
}
|
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 |
fc83c486
|
n.hoffmann
|
@Ignore
|
319 |
7a1d87e3
|
a.babadshanjan
|
@DataSet("ConcurrentSessionTest.xml")
|
320 |
|
|
public void testWhatHappensWhenEncounteringStaleData(){
|
321 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
322 |
fc83c486
|
n.hoffmann
|
TransactionStatus txStatusOne = conversationHolder1.startTransaction();
|
323 |
7a1d87e3
|
a.babadshanjan
|
TaxonBase taxonBase1 = taxonService.getTaxonByUuid(taxonUuid1);
|
324 |
|
|
|
325 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
326 |
fc83c486
|
n.hoffmann
|
TransactionStatus txStatusTwo = conversationHolder2.startTransaction();
|
327 |
7a1d87e3
|
a.babadshanjan
|
TaxonBase taxonBase2 = taxonService.getTaxonByUuid(taxonUuid1);
|
328 |
|
|
|
329 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
330 |
7a1d87e3
|
a.babadshanjan
|
ReferenceBase reference1 = referenceService.getReferenceByUuid(referenceUuid1);
|
331 |
fc83c486
|
n.hoffmann
|
assertSame("This should be the sec", taxonBase1.getSec(), reference1);
|
332 |
7a1d87e3
|
a.babadshanjan
|
|
333 |
|
|
ReferenceBase reference2 = referenceService.getReferenceByUuid(referenceUuid2);
|
334 |
|
|
taxonBase1.setSec(reference2);
|
335 |
|
|
taxonService.save(taxonBase1);
|
336 |
fc83c486
|
n.hoffmann
|
conversationHolder1.commit();
|
337 |
7a1d87e3
|
a.babadshanjan
|
|
338 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
339 |
7a1d87e3
|
a.babadshanjan
|
taxonBase2.setSec(null);
|
340 |
|
|
taxonService.save(taxonBase2);
|
341 |
fc83c486
|
n.hoffmann
|
conversationHolder2.commit();
|
342 |
7a1d87e3
|
a.babadshanjan
|
|
343 |
17ae07e8
|
n.hoffmann
|
conversationHolder3.bind();
|
344 |
7a1d87e3
|
a.babadshanjan
|
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 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
356 |
35726797
|
n.hoffmann
|
conversationHolder1.startTransaction();
|
357 |
7a1d87e3
|
a.babadshanjan
|
// 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 |
35726797
|
n.hoffmann
|
conversationHolder1.commit();
|
368 |
7a1d87e3
|
a.babadshanjan
|
|
369 |
|
|
|
370 |
|
|
// second conversation
|
371 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
372 |
35726797
|
n.hoffmann
|
conversationHolder2.startTransaction();
|
373 |
7a1d87e3
|
a.babadshanjan
|
// 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 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
390 |
7a1d87e3
|
a.babadshanjan
|
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 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
406 |
7a1d87e3
|
a.babadshanjan
|
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 |
fc83c486
|
n.hoffmann
|
// @Ignore
|
420 |
7a1d87e3
|
a.babadshanjan
|
@Test
|
421 |
|
|
@DataSet("ConcurrentSessionTest.xml")
|
422 |
|
|
public void testIfDataGetsPersistedWhenFiringCommit(){
|
423 |
|
|
// first conversation
|
424 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
425 |
7a1d87e3
|
a.babadshanjan
|
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 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
440 |
7a1d87e3
|
a.babadshanjan
|
// 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 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
447 |
7a1d87e3
|
a.babadshanjan
|
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 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
455 |
7a1d87e3
|
a.babadshanjan
|
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 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
469 |
7a1d87e3
|
a.babadshanjan
|
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 |
35726797
|
n.hoffmann
|
@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 |
6ae61fd7
|
n.hoffmann
|
@Test
|
493 |
|
|
@DataSet("ConcurrentSessionTest.xml")
|
494 |
|
|
public void testInsert(){
|
495 |
17ae07e8
|
n.hoffmann
|
TestConversationEnabled testConversationEnabled = new TestConversationEnabled();
|
496 |
|
|
|
497 |
fc83c486
|
n.hoffmann
|
conversationHolder1.registerForDataStoreChanges(testConversationEnabled);
|
498 |
17ae07e8
|
n.hoffmann
|
|
499 |
6ae61fd7
|
n.hoffmann
|
conversationHolder1.bind();
|
500 |
|
|
conversationHolder1.startTransaction();
|
501 |
|
|
TaxonBase newTaxon = Taxon.NewInstance(null, null);
|
502 |
|
|
taxonService.save(newTaxon);
|
503 |
|
|
conversationHolder1.commit();
|
504 |
|
|
}
|
505 |
|
|
|
506 |
fc83c486
|
n.hoffmann
|
@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 |
17ae07e8
|
n.hoffmann
|
|
594 |
7a1d87e3
|
a.babadshanjan
|
/**
|
595 |
|
|
* this is a locking playground
|
596 |
|
|
*/
|
597 |
|
|
@Test
|
598 |
|
|
@DataSet("ConcurrentSessionTest.xml")
|
599 |
|
|
public void testLocking(){
|
600 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
601 |
7a1d87e3
|
a.babadshanjan
|
// 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 |
17ae07e8
|
n.hoffmann
|
conversationHolder2.bind();
|
608 |
7a1d87e3
|
a.babadshanjan
|
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 |
17ae07e8
|
n.hoffmann
|
conversationHolder1.bind();
|
616 |
7a1d87e3
|
a.babadshanjan
|
conversationHolder1.commit();
|
617 |
|
|
|
618 |
|
|
conversationHolder1.startTransaction();
|
619 |
|
|
conversationHolder1.lock(taxonBase, LockMode.READ);
|
620 |
|
|
|
621 |
|
|
}
|
622 |
6ae61fd7
|
n.hoffmann
|
|
623 |
|
|
|
624 |
7a1d87e3
|
a.babadshanjan
|
}
|