GCP数据存储com.google.cloud.Datastore.DatastoreException:事务内部的查询



一个简单的查找实体(简单实体,没有子体关系(进行更改并将实体保存在同一事务中。

但导致以下错误。

21:46:47.883 [Test worker] ERROR c.f.r.e.CustomExceptionControllerAdvice - Application exception occurred
com.google.cloud.datastore.DatastoreException: queries inside transactions must have ancestors
at com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.translate(HttpDatastoreRpc.java:128)
at com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.translate(HttpDatastoreRpc.java:113)
at com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.runQuery(HttpDatastoreRpc.java:181)
at com.google.cloud.datastore.DatastoreImpl$1.call(DatastoreImpl.java:180)
at com.google.cloud.datastore.DatastoreImpl$1.call(DatastoreImpl.java:177)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
at com.google.cloud.datastore.DatastoreImpl.runQuery(DatastoreImpl.java:176)
at com.google.cloud.datastore.QueryResultsImpl.sendRequest(QueryResultsImpl.java:73)
at com.google.cloud.datastore.QueryResultsImpl.<init>(QueryResultsImpl.java:57)
at com.google.cloud.datastore.DatastoreImpl.run(DatastoreImpl.java:170)
at com.google.cloud.datastore.TransactionImpl.run(TransactionImpl.java:96)
at org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate.queryKeysOrEntities(DatastoreTemplate.java:266)
at org.springframework.cloud.gcp.data.datastore.repository.query.GqlDatastoreQuery.execute(GqlDatastoreQuery.java:145)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:618)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy185.findByHash(Unknown Source)

在对两个gcp项目进行了充分的测试和比较后,注意到,当应用程序连接到数据存储模式下具有Cloud Firestore数据库实例的gcp项目时,相同的代码运行时没有任何错误(上述异常(。

当应用程序连接到本机模式数据库实例中Firestore的gcp项目时,导致上述错误。

https://cloud.google.com/datastore/docs/firestore-or-datastore

数据存储事务仅在来自同一实体组的实体上运行,这些实体是共同祖先的后代。为了保持这一限制,事务中的所有查询都必须包括一个祖先过滤器,该过滤器指定事务中其他操作所在实体组中的祖先。

来自Firestore在数据存储模式下的官方GCP文档:

如果您想在事务中使用查询,您的数据必须以可以指定的方式组织到实体组中祖先将匹配正确数据的筛选器。

看看这篇关于StackOverflow的文章,它描述了一种可能的解决方法。

最新更新