43
43
import org .springframework .context .expression .BeanFactoryAccessor ;
44
44
import org .springframework .context .expression .BeanFactoryResolver ;
45
45
import org .springframework .dao .DataAccessException ;
46
+ import org .springframework .dao<
8000
/span>.InvalidDataAccessResourceUsageException ;
46
47
import org .springframework .dao .support .DataAccessUtils ;
47
48
import org .springframework .dao .support .PersistenceExceptionTranslator ;
48
49
import org .springframework .data .mapping .PersistentPropertyAccessor ;
@@ -127,31 +128,33 @@ public ArangoDatabase db() throws DataAccessException {
127
128
});
128
129
}
129
130
130
- private ArangoCollection _collection (final String name ) {
131
- return _collection (name , null , null );
131
+ private ArangoCollection _collection (final String name , boolean transactional ) {
132
+ return _collection (name , null , null , transactional );
132
133
}
133
134
134
- private ArangoCollection _collection (final Class <?> entityClass ) {
135
- return _collection (entityClass , null );
135
+ private ArangoCollection _collection (final Class <?> entityClass , boolean transactional ) {
136
+ return _collection (entityClass , null , transactional );
136
137
}
137
138
138
- private ArangoCollection _collection (final Class <?> entityClass , final Object id ) {
139
+ private ArangoCollection _collection (final Class <?> entityClass , final Obje
8000
ct id , boolean transactional ) {
139
140
final ArangoPersistentEntity <?> persistentEntity = converter .getMappingContext ()
140
141
.getRequiredPersistentEntity (entityClass );
141
142
final String name = determineCollectionFromId (id ).orElse (persistentEntity .getCollection ());
142
- return _collection (name , persistentEntity , persistentEntity .getCollectionOptions ());
143
+ return _collection (name , persistentEntity , persistentEntity .getCollectionOptions (), transactional );
143
144
}
144
145
145
146
private ArangoCollection _collection (final String name , final ArangoPersistentEntity <?> persistentEntity ,
146
- final CollectionCreateOptions options ) {
147
+ final CollectionCreateOptions options , boolean transactional ) {
147
148
148
149
final ArangoDatabase db = db ();
149
150
final Class <?> entityClass = persistentEntity != null ? persistentEntity .getType () : null ;
150
151
final CollectionCacheValue value = collectionCache .computeIfAbsent (new CollectionCacheKey (db .name (), name ),
151
152
key -> {
152
153
final ArangoCollection collection = db .collection (name );
153
154
if (!collection .exists ()) {
154
-
155
+ if (transactional ) {
156
+ throw new InvalidDataAccessResourceUsageException ("Missing collection cannot be created during transaction" );
157
+ }
155
158
collection .create (options );
156
159
}
157
160
return new CollectionCacheValue (collection );
@@ -160,8 +163,10 @@ private ArangoCollection _collection(final String name, final ArangoPersistentEn
160
163
final ArangoCollection collection = value .getCollection ();
161
164
if (persistentEntity != null && !entities .contains (entityClass )) {
162
165
value .addEntityClass (entityClass );
166
+ if (!transactional ) {
163
167
ensureCollectionIndexes (collection (collection ), persistentEntity );
164
168
}
169
+ }
165
170
return collection ;
166
171
}
167
172
@@ -287,18 +292,18 @@ public ArangoDBVersion getVersion() throws DataAccessException {
287
292
public <T > ArangoCursor <T > query (final String query , final Map <String , Object > bindVars ,
288
293
final AqlQueryOptions options , final Class <T > entityClass ) throws DataAccessException {
289
294
try {
290
- ArangoCursor <T > cursor = db ().query (query , entityClass , bindVars == null ? null : prepareBindVars (bindVars ), options );
295
+ ArangoCursor <T > cursor = db ().query (query , entityClass , bindVars == null ? null : prepareBindVars (bindVars , false ), options );
291
296
return new ArangoExtCursor <>(cursor , entityClass , eventPublisher );
292
297
} catch (final ArangoDBException e ) {
293
298
throw translateException (e );
294
299
}
295
300
}
296
301
297
- private Map <String , Object > prepareBindVars (final Map <String , Object > bindVars ) {
302
+ private Map <String , Object > prepareBindVars (final Map <String , Object > bindVars , boolean transactional ) {
298
303
final Map <String , Object > prepared = new HashMap <>(bindVars .size ());
299
304
for (final Entry <String , Object > entry : bindVars .entrySet ()) {
300
305
if (entry .getKey ().startsWith ("@" ) && entry .getValue () instanceof Class <?> clazz ) {
301
- prepared .put (entry .getKey (), _collection (clazz ).name ());
306
+ prepared .put (entry .getKey (), _collection (clazz , transactional ).name ());
302
307
} else {
303
308
prepared .put (entry .getKey (), entry .getValue ());
304
309
}
@@ -317,7 +322,7 @@ public <T> MultiDocumentEntity<DocumentDeleteEntity<T>> deleteAll(
317
322
318
323
MultiDocumentEntity <DocumentDeleteEntity <T >> result ;
319
324
try {
320
- result = _collection (entityClass ).deleteDocuments (toList (values ), options , entityClass );
325
+ result = _collection (entityClass , options . getStreamTransactionId () != null ).deleteDocuments (toList (values ), options , entityClass );
321
326
} catch (final ArangoDBException e ) {
322
327
throw translateException (e );
323
328
}
@@ -349,7 +354,7 @@ public <T> DocumentDeleteEntity<T> delete(final Object id, final DocumentDeleteO
349
354
350
355
DocumentDeleteEntity <T > result ;
351
356
try {
352
- result = _collection (entityClass , id ).deleteDocument (determineDocumentKeyFromId (id ), options , entityClass );
357
+ result = _collection (entityClass , id , options . getStreamTransactionId () != null ).deleteDocument (determineDocumentKeyFromId (id ), options , entityClass );
353
358
} catch (final ArangoDBException e ) {
354
359
throw translateException (e );
355
360
}
@@ -369,7 +374,7 @@ public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateAll(
369
374
370
375
MultiDocumentEntity <DocumentUpdateEntity <T >> result ;
371
376
try {
372
- result = _collection (entityClass ).updateDocuments (toList (values ), options , entityClass );
377
+ result = _collection (entityClass , options . getStreamTransactionId () != null ).updateDocuments (toList (values ), options , entityClass );
373
378
} catch (final ArangoDBException e ) {
374
379
throw translateException (e );
375
380
}
@@ -387,7 +392,7 @@ public <T> DocumentUpdateEntity<T> update(final Object id, final T value, final
387
392
388
393
DocumentUpdateEntity <T > result ;
389
394
try {
390
- result = _collection (value .getClass (), id ).updateDocument (determineDocumentKeyFromId (id ), value , options );
395
+ result = _collection (value .getClass (), id , options . getStreamTransactionId () != null ).updateDocument (determineDocumentKeyFromId (id ), value , options );
391
396
} catch (final ArangoDBException e ) {
392
397
throw translateException (e );
393
398
}
@@ -408,7 +413,7 @@ public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> replaceAll(
408
413
409
414
MultiDocumentEntity <DocumentUpdateEntity <T >> result ;
410
415
try {
411
- result = _collection (entityClass ).replaceDocuments (toList (values ), options , entityClass );
416
+ result = _collection (entityClass , options . getStreamTransactionId () != null ).replaceDocuments (toList (values ), options , entityClass );
412
417
} catch (final ArangoDBException e ) {
413
418
throw translateException (e );
414
419
}
@@ -425,7 +430,7 @@ public <T> DocumentUpdateEntity<T> replace(final Object id, final T value, final
425
430
426
431
DocumentUpdateEntity <T > result ;
427
432
try {
428
- result = _collection (value .getClass (), id ).replaceDocument (determineDocumentKeyFromId (id ), value , options );
433
+ result = _collection (value .getClass (), id , false ).replaceDocument (determineDocumentKeyFromId (id ), value , options );
429
434
} catch (final ArangoDBException e ) {
430
435
throw translateException (e );
431
436
}
@@ -439,7 +444,7 @@ public <T> DocumentUpdateEntity<T> replace(final Object id, final T value, final
439
444
public <T > Optional <T > find (final Object id , final Class <T > entityClass , final DocumentReadOptions options )
440
445
throws DataAccessException {
441
446
try {
442
- T res = _collection (entityClass , id ).getDocument (determineDocumentKeyFromId (id ), entityClass , options );
447
+ T res = _collection (entityClass , id , options . getStreamTransactionId () != null ).getDocument (determineDocumentKeyFromId (id ), entityClass , options );
443
448
if (res != null ) {
444
449
potentiallyEmitEvent (new AfterLoadEvent <>(res ));
445
450
}
@@ -462,7 +467,7 @@ public <T> Iterable<T> findAll(final Iterable<?> ids, final Class<T> entityClass
462
467
try {
463
468
final Collection <String > keys = new ArrayList <>();
464
469
ids .forEach (id -> keys .add (determineDocumentKeyFromId (id )));
465
- Collection <T > docs = _collection (entityClass ).getDocuments (keys , entityClass ).getDocuments ();
470
+ Collection <T > docs = _collection (entityClass , options . getStreamTransactionId () != null ).getDocuments (keys , entityClass ).getDocuments ();
466
471
for (T doc : docs ) {
467
472
if (doc != null ) {
468
473
potentiallyEmitEvent (new AfterLoadEvent <>(doc ));
@@ -482,7 +487,7 @@ public <T> MultiDocumentEntity<DocumentCreateEntity<T>> insertAll(
482
487
483
488
MultiDocumentEntity <DocumentCreateEntity <T >> result ;
484
489
try {
485
- result = _collection (entityClass ).insertDocuments (toList (values ), options , entityClass );
490
+ result = _collection (entityClass , options . getStreamTransactionId () != null ).insertDocuments (toList (values ), options , entityClass );
486
491
} catch (final ArangoDBException e ) {
487
492
throw translateException (e );
488
493
}
@@ -498,7 +503,7 @@ public <T> DocumentCreateEntity<T> insert(final T value, final DocumentCreateOpt
498
503
499
504
DocumentCreateEntity <T > result ;
500
505
try {
501
- result = _collection (value .getClass ()).insertDocument (value , options );
506
+ result = _collection (value .getClass (), options . getStreamTransactionId () != null ).insertDocument (value , options );
502
507
} catch (final ArangoDBException e ) {
503
508
throw translateException (e );
504
509
}
@@ -511,7 +516,7 @@ public <T> DocumentCreateEntity<T> insert(final T value, final DocumentCreateOpt
511
516
@ Override
512
517
public <T > T repsert (final T value , AqlQueryOptions options ) throws DataAccessException {
513
518
@ SuppressWarnings ("unchecked" ) final Class <T > clazz = (Class <T >) value .getClass ();
514
- final String collectionName = _collection (clazz ).name ();
519
+ final String collectionName = _collection (clazz , options . getStreamTransactionId () != null ).name ();
515
520
516
521
potentiallyEmitEvent (new BeforeSaveEvent <>(value ));
517
522
@@ -543,7 +548,7 @@ public <T> Iterable<T> repsertAll(final Iterable<T> values, final Class<? super
543
548
return Collections .emptyList ();
544
549
}
545
550
546
- final String collectionName = _collection (entityClass ).name ();
551
+ final String collectionName = _collection (entityClass , options . getStreamTransactionId () != null ).name ();
547
552
potentiallyEmitBeforeSaveEvent (values );
548
553
549
554
Map <String , Object > bindVars = new HashMap <>();
@@ -644,7 +649,7 @@ private void updateDBFields(final Object value, final DocumentEntity documentEnt
644
649
@ Override
645
650
public boolean exists (final Object id , final Class <?> entityClass , DocumentExistsOptions options ) throws DataAccessException {
646
651
try {
647
- return _collection (entityClass ).documentExists (determineDocumentKeyFromId (id ), options );
652
+ return _collection (entityClass , options . getStreamTransactionId () != null ).documentExists (determineDocumentKeyFromId (id ), options );
648
653
} catch (final ArangoDBException e ) {
649
654
throw translateException (e );
650
655
}
@@ -665,13 +670,13 @@ public void dropDatabase() throws DataAccessException {
665
670
666
671
@ Override
667
672
public CollectionOperations collection (final Class <?> entityClass ) throws DataAccessException {
668
- return collection (_collection (entityClass ));
673
+ return collection (_collection (entityClass , false ));
669
674
}
670
675
671
676
@ Override
672
677
public CollectionOperations collection (final String name , final CollectionCreateOptions options )
673
678
throws DataAccessException {
674
- return collection (_collection (name , null , options ));
679
+ return collection (_collection (name , null , options , false ));
675
680
}
676
681
677
682
private CollectionOperations collection (final ArangoCollection collection ) {
0 commit comments