1919import java .util .ArrayList ;
2020import java .util .List ;
2121
22+ import org .junit .Assert ;
23+ import org .junit .Before ;
24+ import org .junit .Test ;
25+
2226import com .arangodb .ArangoConfigure ;
2327import com .arangodb .ArangoDriver ;
2428import com .arangodb .ArangoException ;
2529import com .arangodb .entity .DocumentEntity ;
2630import com .arangodb .entity .TransactionEntity ;
31+ import com .arangodb .example .document .BaseExample ;
2732
2833/**
29- * Using ArangoDb transaction functions.
34+ * Using a server-side transaction function.
35+ *
36+ * see https://docs.arangodb.com/Transactions/TransactionInvocation.html
3037 *
3138 * @author a-brandt
3239 */
33- public class TransactionExample {
40+ public class TransactionExample extends BaseExample {
41+
42+ private static final String DATABASE_NAME = "TransactionExample" ;
3443
3544 private static final String COLLECTION_NAME = "transactionCollection" ;
3645
@@ -40,53 +49,64 @@ public class TransactionExample {
4049 // increment counter 10 times
4150 private static final int NUMBER_UPDATES = 10 ;
4251
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 ;
4653
47- ArangoConfigure configure = new ArangoConfigure ();
48- configure .init ();
54+ public static ArangoConfigure configuration ;
4955
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 );
6459
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+ }
6864
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 );
7682
77- documentEntity2 = driver .getDocument (documentEntity2 .getDocumentHandle (), myCounter .class );
83+ documentEntity1 = arangoDriver .getDocument (documentEntity1 .getDocumentHandle (), myCounter .class );
7884
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 );
8190
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 ();
8696 }
97+ joinThreads (transactionThreadslist );
8798
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 ());
88104 }
89105
106+ /**
107+ * Example without transaction function
108+ *
109+ */
90110 public static class NoTransactionThread extends Thread {
91111
92112 private String documentHandle ;
@@ -96,10 +116,7 @@ public NoTransactionThread(String documentHandle) {
96116 }
97117
98118 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 );
103120
104121 try {
105122 ;
@@ -116,12 +133,14 @@ public void run() {
116133 }
117134 } catch (ArangoException e ) {
118135 e .printStackTrace ();
119- } finally {
120- configure .shutdown ();
121136 }
122137 }
123138 }
124139
140+ /**
141+ * Example with transaction function
142+ *
143+ */
125144 public static class TransactionThread extends Thread {
126145
127146 private String documentHandle ;
@@ -132,10 +151,7 @@ public TransactionThread(String documentHandle) {
132151 }
133152
134153 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 );
139155
140156 TransactionEntity transaction = buildTransaction (driver );
141157 try {
@@ -148,8 +164,6 @@ public void run() {
148164 }
149165 } catch (ArangoException e ) {
150166 e .printStackTrace ();
151- } finally {
152- configure .shutdown ();
153167 }
154168 }
155169 }
@@ -186,11 +200,18 @@ private static void sleepRandom() {
186200
187201 }
188202
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+ */
189210 private static TransactionEntity buildTransaction (ArangoDriver driver ) {
190211
191212 // create action function
192213 String action = "function (id) {"
193- // use internal database functions
214+ // use internal database functions
194215 + " var db = require('internal').db;"
195216 // get the document
196217 + "a = db._document(id); "
0 commit comments