19
19
import java .util .ArrayList ;
20
20
import java .util .List ;
21
21
22
+ import org .junit .Assert ;
23
+ import org .junit .Before ;
24
+ import org .junit .Test ;
25
+
22
26
import com .arangodb .ArangoConfigure ;
23
27
import com .arangodb .ArangoDriver ;
24
28
import com .arangodb .ArangoException ;
25
29
import com .arangodb .entity .DocumentEntity ;
26
30
import com .arangodb .entity .TransactionEntity ;
31
+ import com .arangodb .example .document .BaseExample ;
27
32
28
33
/**
29
- * Using ArangoDb transaction functions.
34
+ * Using a server-side transaction function.
35
+ *
36
+ * see https://docs.arangodb.com/Transactions/TransactionInvocation.html
30
37
*
31
38
* @author a-brandt
32
39
*/
33
- public class TransactionExample {
40
+ public class TransactionExample extends BaseExample {
41
+
42
+ private static final String DATABASE_NAME = "TransactionExample" ;
34
43
35
44
private static final String COLLECTION_NAME = "transactionCollection" ;
36
45
@@ -40,53 +49,64 @@ public class TransactionExample {
40
49
// increment counter 10 times
41
50
private static final int NUMBER_UPDATES = 10 ;
42
51
43
- public static void main (String [] args ) {
44
- List <NoTransactionThread > noTransactionThreadslist = new ArrayList <NoTransactionThread >();
45
- List <TransactionThread > transactionThreadslist = new ArrayList <TransactionThread >();
52
+ public ArangoDriver arangoDriver ;
46
53
47
- ArangoConfigure configure = new ArangoConfigure ();
48
- configure .init ();
54
+ public static ArangoConfigure configuration ;
49
55
50
- ArangoDriver driver = new ArangoDriver (configure );
51
- try {
52
- myCounter entity = new myCounter ();
53
- entity .setCount (0L );
54
- DocumentEntity <myCounter > documentEntity1 = driver .createDocument (COLLECTION_NAME , entity , true , null );
55
- DocumentEntity <myCounter > documentEntity2 = driver .createDocument (COLLECTION_NAME , entity , true , null );
56
-
57
- // start threads without transaction
58
- for (int i = 0 ; i < NUMBER_THREADS ; i ++) {
59
- NoTransactionThread s = new NoTransactionThread (documentEntity1 .getDocumentHandle ());
60
- noTransactionThreadslist .add (s );
61
- s .start ();
62
- }
63
- joinThreads (noTransactionThreadslist );
56
+ @ Before
57
+ public void _before () {
58
+ removeTestDatabase (DATABASE_NAME );
64
59
65
- // random values
66
- documentEntity1 = driver .getDocument (documentEntity1 .getDocumentHandle (), myCounter .class );
67
- System .out .println ("no transaction count = " + documentEntity1 .getEntity ().getCount ());
60
+ configuration = getConfiguration ();
61
+ arangoDriver = getArangoDriver (configuration );
62
+ createDatabase (arangoDriver , DATABASE_NAME );
63
+ }
68
64
69
- // start threads with ArangoDB transaction
70
- for (int i = 0 ; i < NUMBER_THREADS ; i ++) {
71
- TransactionThread s = new TransactionThread (documentEntity2 .getDocumentHandle ());
72
- transactionThreadslist .add (s );
73
- s .start ();
74
- }
75
- joinThreads (transactionThreadslist );
65
+ @ Test
66
+ public void transactionExample () throws ArangoException {
67
+ List <NoTransactionThread > noTransactionThreadslist = new ArrayList <NoTransactionThread >();
68
+ List <TransactionThread > transactionThreadslist = new ArrayList <TransactionThread >();
69
+
70
+ myCounter entity = new myCounter ();
71
+ entity .setCount (0L );
72
+ DocumentEntity <myCounter > documentEntity1 = arangoDriver .createDocument (COLLECTION_NAME , entity , true , null );
73
+ DocumentEntity <myCounter > documentEntity2 = arangoDriver .createDocument (COLLECTION_NAME , entity , true , null );
74
+
75
+ // start threads without transaction
76
+ for (int i = 0 ; i < NUMBER_THREADS ; i ++) {
77
+ NoTransactionThread s = new NoTransactionThread (documentEntity1 .getDocumentHandle ());
78
+ noTransactionThreadslist .add (s );
79
+ s .start ();
80
+ }
81
+ joinThreads (noTransactionThreadslist );
76
82
77
- documentEntity2 = driver .getDocument (documentEntity2 .getDocumentHandle (), myCounter .class );
83
+ documentEntity1 = arangoDriver .getDocument (documentEntity1 .getDocumentHandle (), myCounter .class );
78
84
79
- // result should be NUMBER_THREADS * NUMBER_UPDATES = 100
80
- System .out .println ("transaction count = " + documentEntity2 .getEntity ().getCount ());
85
+ // result should be NUMBER_THREADS * NUMBER_UPDATES = 100 but has random
86
+ // values
87
+ System .out .println ("no transaction result: count = " + documentEntity1 .getEntity ().getCount () + " != "
88
+ + NUMBER_THREADS * NUMBER_UPDATES );
89
+ Assert .assertTrue (documentEntity1 .getEntity ().getCount () != NUMBER_THREADS * NUMBER_UPDATES );
81
90
82
- } catch (ArangoException e ) {
83
- e .printStackTrace ();
84
- } finally {
85
- configure .shutdown ();
91
+ // start threads with ArangoDB transaction
92
+ for (int i = 0 ; i < NUMBER_THREADS ; i ++) {
93
+ TransactionThread s = new TransactionThread (documentEntity2 .getDocumentHandle ());
94
+ transactionThreadslist .add (s );
95
+ s .start ();
86
96
}
97
+ joinThreads (transactionThreadslist );
87
98
99
+ documentEntity2 = arangoDriver .getDocument (documentEntity2 .getDocumentHandle (), myCounter .class );
100
+
101
+ // result should be NUMBER_THREADS * NUMBER_UPDATES = 100
102
+ System .out .println ("with transaction result: count = " + documentEntity2 .getEntity ().getCount ());
103
+ Assert .assertEquals (NUMBER_THREADS * NUMBER_UPDATES , documentEntity2 .getEntity ().getCount ().intValue ());
88
104
}
89
105
106
+ /**
107
+ * Example without transaction function
108
+ *
109
+ */
90
110
public static class NoTransactionThread extends Thread {
91
111
92
112
private String documentHandle ;
@@ -96,10 +116,7 @@ public NoTransactionThread(String documentHandle) {
96
116
}
97
117
98
118
public void run () {
99
- ArangoConfigure configure = new ArangoConfigure ();
100
- configure .init ();
101
-
102
- ArangoDriver driver = new ArangoDriver (configure );
119
+ ArangoDriver driver = new ArangoDriver (configuration , DATABASE_NAME );
103
120
104
121
try {
105
122
;
@@ -116,12 +133,14 @@ public void run() {
116
133
}
117
134
} catch (ArangoException e ) {
118
135
e .printStackTrace ();
119
- } finally {
120
- configure .shutdown ();
121
136
}
122
137
}
123
138
}
124
139
140
+ /**
141
+ * Example with transaction function
142
+ *
143
+ */
125
144
public static class TransactionThread extends Thread {
126
145
127
146
private String documentHandle ;
@@ -132,10 +151,7 @@ public TransactionThread(String documentHandle) {
132
151
}
133
152
134
153
public void run () {
135
- ArangoConfigure configure = new ArangoConfigure ();
136
- configure .init ();
137
-
138
- ArangoDriver driver = new ArangoDriver (configure );
154
+ ArangoDriver driver = new ArangoDriver (configuration , DATABASE_NAME );
139
155
140
156
TransactionEntity transaction = buildTransaction (driver );
141
157
try {
@@ -148,8 +164,6 @@ public void run() {
148
164
}
149
165
} catch (ArangoException e ) {
150
166
e .printStackTrace ();
151
- } finally {
152
- configure .shutdown ();
153
167
}
154
168
}
155
169
}
@@ -186,11 +200,18 @@ private static void sleepRandom() {
186
200
187
201
}
188
202
203
+ /**
204
+ * Build the server side function to update a value transactional.
205
+ *
206
+ * @param driver
207
+ * the ArangoDB driver
208
+ * @return a transaction entity
209
+ */
189
210
private static TransactionEntity buildTransaction (ArangoDriver driver ) {
190
211
191
212
// create action function
192
213
String action = "function (id) {"
193
- // use internal database functions
214
+ // use internal database functions
194
215
+ " var db = require('internal').db;"
195
216
// get the document
196
217
+ "a = db._document(id); "
0 commit comments