跨多个Web请求的MongoDB事务 [英] MongoDB transactions spanning multiple web requests
问题描述
我正在尝试使用Go通过HTTP实施 MongoDB 交易API服务客户端将请求发送到以下位置:/db/transaction/begin
获取交易ID为了将所有后续与交易相关的CRUD操作API请求与该交易ID绑定在一起最后,客户端要么向/db/transaction/{transaction_id}/commit
发送请求或/db/transaction/{transaction_id}/rollback
(有点类似于Firestore交易)
我能够使用 ArangoDB 实现这种行为,因为它是Go Driver允许我设置每个CRUD操作的交易ID(解决方案
更简单的解决方案是在提交端点运行时将应用中的操作存储(缓冲)并全部提交给MongoDB.
从技术上讲,您可以在应用程序中手动管理事务生命周期并实现跨Web请求的事务,但这是1)相当尴尬和2)您将失去驱动程序在其中为您所做的各种瞬时错误处理withTransaction API.
要这样做:
- 熟悉会话规范
- 熟悉交易规范
- 阅读方便tx api规范,以便让客户知道为什么免费免费获得什么,如果您的客户问您为什么要暴露驱动程序没有提供的错误
- 在驱动程序中查看实现这些规范的代码
- 为应用程序中的需求实施足够的会话和事务管理
可能更容易分叉&修补驱动程序,但这显然还有其他问题,原因是您的代码库与官方驱动程序代码库不同.
I'm trying to implement a MongoDB transactions API service over HTTP using Go
where the client sends a request to: /db/transaction/begin
to acquire a Transaction ID
in order to bind all subsequent transaction-related CRUD operations API requests with that Transaction ID
and finally the client either sends a request to /db/transaction/{transaction_id}/commit
or /db/transaction/{transaction_id}/rollback
(something somewhat similar to Firestore Transaction)
I was able to implement this kind of behavior with ArangoDB since it's Go Driver allows me to set the Transaction ID of each CRUD operation (https://godoc.org/github.com/arangodb/go-driver#WithTransactionID)
but unfortunately i couldn't achieve this with the official MongoDB Go Driver
am i missing something? Is there a workaround or any custom implementation which could help me?
Thanks in advance
The simpler solution is to store (buffer) the operations in your application and submit them all together to MongoDB when the commit endpoint runs.
Technically you can manually manage transaction lifecycle in your application and implement transactions spanning web requests but this is 1) going to be quite awkward and 2) you are going to lose the various transient error handling that the driver does for you in the withTransaction API.
To do this:
- Familiarize yourself with sessions specification
- Familiarize yourself with transactions specification
- Read the convenient tx api spec so that you know what you won't be getting for free, if your customer asks why you are exposing errors that drivers don't
- Review the code in your driver implementing these specifications
- Implement enough of session and transaction management for your requirements in your application
Might be easier to fork & patch the driver but this obviously has other issues resulting from your codebase diverging from the official driver codebase.
这篇关于跨多个Web请求的MongoDB事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!