-
Notifications
You must be signed in to change notification settings - Fork 199
/
OptimisticLockingExtraTest.java
130 lines (101 loc) · 3.71 KB
/
OptimisticLockingExtraTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Hibernate OGM, Domain model persistence for NoSQL datastores
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.ogm.backendtck.optimisticlocking;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.ogm.cfg.OgmProperties;
import org.hibernate.ogm.datastore.spi.DatastoreProvider;
import org.hibernate.ogm.dialect.impl.ForwardingGridDialect;
import org.hibernate.ogm.dialect.spi.TupleContext;
import org.hibernate.ogm.model.key.spi.EntityKey;
import org.hibernate.ogm.model.spi.Tuple;
import org.hibernate.ogm.utils.GridDialectType;
import org.hibernate.ogm.utils.OgmTestCase;
import org.hibernate.ogm.utils.SkipByGridDialect;
import org.hibernate.ogm.utils.TestHelper;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.fest.assertions.Assertions.assertThat;
/**
* Test for detecting concurrent updates by dialects which support atomic find/update semantics or have their own
* optimistic locking scheme.
*
* @author Gunnar Morling
*/
@SkipByGridDialect(
value = { GridDialectType.CASSANDRA },
comment = "list - bag semantics unsupported (no primary key)"
)
public class OptimisticLockingExtraTest extends OgmTestCase {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void updateToEmbeddedCollectionCausesVersionToBeIncreased() throws Throwable {
Galaxy galaxy = persistGalaxy();
Session session = openSession();
Transaction transaction = session.beginTransaction();
Galaxy entity = (Galaxy) session.get( Galaxy.class, galaxy.getId() );
entity.getStars().add( new Star( "Algol" ) );
transaction.commit();
session.clear();
session = openSession();
transaction = session.beginTransaction();
entity = (Galaxy) session.get( Galaxy.class, galaxy.getId() );
assertThat( entity.getVersion() ).isEqualTo( 1 );
assertThat( entity.getStars() ).hasSize( 3 );
transaction.commit();
session.close();
}
private Galaxy persistGalaxy() {
Session session = openSession();
session.beginTransaction();
Galaxy milkyWay = new Galaxy( "galaxy-1", "Milky Way", new Star( "Sun" ), new Star( "Alpha Centauri" ) );
session.persist( milkyWay );
session.getTransaction().commit();
session.close();
return milkyWay;
}
@Override
protected void configure(Configuration cfg) {
cfg.getProperties().put( OgmProperties.GRID_DIALECT, TestDialect.class );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Galaxy.class };
}
@SuppressWarnings("serial")
public static class TestDialect extends ForwardingGridDialect<Serializable> {
public TestDialect(DatastoreProvider provider) {
super( TestHelper.getCurrentGridDialect( provider ) );
}
@Override
public boolean updateTupleWithOptimisticLock(EntityKey entityKey, Tuple oldVersion, Tuple tuple, TupleContext tupleContext) {
if ( Thread.currentThread().getName().equals( "ogm-test-thread-0" ) ) {
waitALittleBit();
}
return super.updateTupleWithOptimisticLock( entityKey, oldVersion, tuple, tupleContext );
}
@Override
public boolean removeTupleWithOptimisticLock(EntityKey entityKey, Tuple oldVersion, TupleContext tupleContext) {
if ( Thread.currentThread().getName().equals( "ogm-test-thread-0" ) ) {
waitALittleBit();
}
return super.removeTupleWithOptimisticLock( entityKey, oldVersion, tupleContext );
}
private void waitALittleBit() {
try {
Thread.sleep( 1000 );
}
catch (InterruptedException e) {
throw new RuntimeException( e );
}
}
}
}