4343import org .springframework .context .expression .BeanFactoryAccessor ;
4444import org .springframework .context .expression .BeanFactoryResolver ;
4545import org .springframework .dao .DataAccessException ;
46+ import org .springframework .dao .InvalidDataAccessResourceUsageException ;
4647import org .springframework .dao .support .DataAccessUtils ;
4748import org .springframework .dao .support .PersistenceExceptionTranslator ;
4849import org .springframework .data .mapping .PersistentPropertyAccessor ;
@@ -127,31 +128,33 @@ public ArangoDatabase db() throws DataAccessException {
127128 });
128129 }
129130
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 );
132133 }
133134
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 );
136137 }
137138
138- private ArangoCollection _collection (final Class <?> entityClass , final Object id ) {
139+ private ArangoCollection _collection (final Class <?> entityClass , final Object id , boolean transactional ) {
139140 final ArangoPersistentEntity <?> persistentEntity = converter .getMappingContext ()
140141 .getRequiredPersistentEntity (entityClass );
141142 final String name = determineCollectionFromId (id ).orElse (persistentEntity .getCollection ());
142- return _collection (name , persistentEntity , persistentEntity .getCollectionOptions ());
143+ return _collection (name , persistentEntity , persistentEntity .getCollectionOptions (), transactional );
143144 }
144145
145146 private ArangoCollection _collection (final String name , final ArangoPersistentEntity <?> persistentEntity ,
146- final CollectionCreateOptions options ) {
147+ final CollectionCreateOptions options , boolean transactional ) {
147148
148149 final ArangoDatabase db = db ();
149150 final Class <?> entityClass = persistentEntity != null ? persistentEntity .getType () : null ;
150151 final CollectionCacheValue value = collectionCache .computeIfAbsent (new CollectionCacheKey (db .name (), name ),
151152 key -> {
152153 final ArangoCollection collection = db .collection (name );
153154 if (!collection .exists ()) {
154-
155+ if (transactional ) {
156+ throw new InvalidDataAccessResourceUsageException ("Missing collection cannot be created during transaction" );
157+ }
155158 collection .create (options );
156159 }
157160 return new CollectionCacheValue (collection );
@@ -160,8 +163,10 @@ private ArangoCollection _collection(final String name, final ArangoPersistentEn
160163 final ArangoCollection collection = value .getCollection ();
161164 if (persistentEntity != null && !entities .contains (entityClass )) {
162165 value .addEntityClass (entityClass );
166+ if (!transactional ) {
163167 ensureCollectionIndexes (collection (collection ), persistentEntity );
164168 }
169+ }
165170 return collection ;
166171 }
167172
@@ -287,18 +292,18 @@ public ArangoDBVersion getVersion() throws DataAccessException {
287292 public <T > ArangoCursor <T > query (final String query , final Map <String , Object > bindVars ,
288293 final AqlQueryOptions options , final Class <T > entityClass ) throws DataAccessException {
289294 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 );
291296 return new ArangoExtCursor <>(cursor , entityClass , eventPublisher );
292297 } catch (final ArangoDBException e ) {
293298 throw translateException (e );
294299 }
295300 }
296301
297- private Map <String , Object > prepareBindVars (final Map <String , Object > bindVars ) {
302+ private Map <String , Object > prepareBindVars (final Map <String , Object > bindVars , boolean transactional ) {
298303 final Map <String , Object > prepared = new HashMap <>(bindVars .size ());
299304 for (final Entry <String , Object > entry : bindVars .entrySet ()) {
300305 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 ());
302307 } else {
303308 prepared .put (entry .getKey (), entry .getValue ());
304309 }
@@ -317,7 +322,7 @@ public <T> MultiDocumentEntity<DocumentDeleteEntity<T>> deleteAll(
317322
318323 MultiDocumentEntity <DocumentDeleteEntity <T >> result ;
319324 try {
320- result = _collection (entityClass ).deleteDocuments (toList (values ), options , entityClass );
325+ result = _collection (entityClass , options . getStreamTransactionId () != null ).deleteDocuments (toList (values ), options , entityClass );
321326 } catch (final ArangoDBException e ) {
322327 throw translateException (e );
323328 }
@@ -349,7 +354,7 @@ public <T> DocumentDeleteEntity<T> delete(final Object id, final DocumentDeleteO
349354
350355 DocumentDeleteEntity <T > result ;
351356 try {
352- result = _collection (entityClass , id ).deleteDocument (determineDocumentKeyFromId (id ), options , entityClass );
357+ result = _collection (entityClass , id , options . getStreamTransactionId () != null ).deleteDocument (determineDocumentKeyFromId (id ), options , entityClass );
353358 } catch (final ArangoDBException e ) {
354359 throw translateException (e );
355360 }
@@ -369,7 +374,7 @@ public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateAll(
369374
370375 MultiDocumentEntity <DocumentUpdateEntity <T >> result ;
371376 try {
372- result = _collection (entityClass ).updateDocuments (toList (values ), options , entityClass );
377+ result = _collection (entityClass , options . getStreamTransactionId () != null ).updateDocuments (toList (values ), options , entityClass );
373378 } catch (final ArangoDBException e ) {
374379 throw translateException (e );
375380 }
@@ -387,7 +392,7 @@ public <T> DocumentUpdateEntity<T> update(final Object id, final T value, final
387392
388393 DocumentUpdateEntity <T > result ;
389394 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 );
391396 } catch (final ArangoDBException e ) {
392397 throw translateException (e );
393398 }
@@ -408,7 +413,7 @@ public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> replaceAll(
408413
409414 MultiDocumentEntity <DocumentUpdateEntity <T >> result ;
410415 try {
411- result = _collection (entityClass ).replaceDocuments (toList (values ), options , entityClass );
416+ result = _collection (entityClass , options . getStreamTransactionId () != null ).replaceDocuments (toList (values ), options , entityClass );
412417 } catch (final ArangoDBException e ) {
413418 throw translateException (e );
414419 }
@@ -425,7 +430,7 @@ public <T> DocumentUpdateEntity<T> replace(final Object id, final T value, final
425430
426431 DocumentUpdateEntity <T > result ;
427432 try {
428- result = _collection (value .getClass (), id ).replaceDocument (determineDocumentKeyFromId (id ), value , options );
433+ result = _collection (value .getClass (), id , false ).replaceDocument (determineDocumentKeyFromId (id ), value , options );
429434 } catch (final ArangoDBException e ) {
430435 throw translateException (e );
431436 }
@@ -439,7 +444,7 @@ public <T> DocumentUpdateEntity<T> replace(final Object id, final T value, final
439444 public <T > Optional <T > find (final Object id , final Class <T > entityClass , final DocumentReadOptions options )
440445 throws DataAccessException {
441446 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 );
443448 if (res != null ) {
444449 potentiallyEmitEvent (new AfterLoadEvent <>(res ));
445450 }
@@ -462,7 +467,7 @@ public <T> Iterable<T> findAll(final Iterable<?> ids, final Class<T> entityClass
462467 try {
463468 final Collection <String > keys = new ArrayList <>();
464469 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 ();
466471 for (T doc : docs ) {
467472 if (doc != null ) {
468473 potentiallyEmitEvent (new AfterLoadEvent <>(doc ));
@@ -482,7 +487,7 @@ public <T> MultiDocumentEntity<DocumentCreateEntity<T>> insertAll(
482487
483488 MultiDocumentEntity <DocumentCreateEntity <T >> result ;
484489 try {
485- result = _collection (entityClass ).insertDocuments (toList (values ), options , entityClass );
490+ result = _collection (entityClass , options . getStreamTransactionId () != null ).insertDocuments (toList (values ), options , entityClass );
486491 } catch (final ArangoDBException e ) {
487492 throw translateException (e );
488493 }
@@ -498,7 +503,7 @@ public <T> DocumentCreateEntity<T> insert(final T value, final DocumentCreateOpt
498503
499504 DocumentCreateEntity <T > result ;
500505 try {
501- result = _collection (value .getClass ()).insertDocument (value , options );
506+ result = _collection (value .getClass (), options . getStreamTransactionId () != null ).insertDocument (value , options );
502507 } catch (final ArangoDBException e ) {
503508 throw translateException (e );
504509 }
@@ -511,7 +516,7 @@ public <T> DocumentCreateEntity<T> insert(final T value, final DocumentCreateOpt
511516 @ Override
512517 public <T > T repsert (final T value , AqlQueryOptions options ) throws DataAccessException {
513518 @ 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 ();
515520
516521 potentiallyEmitEvent (new BeforeSaveEvent <>(value ));
517522
@@ -543,7 +548,7 @@ public <T> Iterable<T> repsertAll(final Iterable<T> values, final Class<? super
543548 return Collections .emptyList ();
544549 }
545550
546- final String collectionName = _collection (entityClass ).name ();
551+ final String collectionName = _collection (entityClass , options . getStreamTransactionId () != null ).name ();
547552 potentiallyEmitBeforeSaveEvent (values );
548553
549554 Map <String , Object > bindVars = new HashMap <>();
@@ -644,7 +649,7 @@ private void updateDBFields(final Object value, final DocumentEntity documentEnt
644649 @ Override
645650 public boolean exists (final Object id , final Class <?> entityClass , DocumentExistsOptions options ) throws DataAccessException {
646651 try {
647- return _collection (entityClass ).documentExists (determineDocumentKeyFromId (id ), options );
652+ return _collection (entityClass , options . getStreamTransactionId () != null ).documentExists (determineDocumentKeyFromId (id ), options );
648653 } catch (final ArangoDBException e ) {
649654 throw translateException (e );
650655 }
@@ -665,13 +670,13 @@ public void dropDatabase() throws DataAccessException {
665670
666671 @ Override
667672 public CollectionOperations collection (final Class <?> entityClass ) throws DataAccessException {
668- return collection (_collection (entityClass ));
673+ return collection (_collection (entityClass , false ));
669674 }
670675
671676 @ Override
672677 public CollectionOperations collection (final String name , final CollectionCreateOptions options )
673678 throws DataAccessException {
674- return collection (_collection (name , null , options ));
679+ return collection (_collection (name , null , options , false ));
675680 }
676681
677682 private CollectionOperations collection (final ArangoCollection collection ) {
0 commit comments