C#中的Kafka交易范围

发布于 2025-02-09 18:44:03 字数 720 浏览 2 评论 0原文

试图在MongoDB中持续一些数据,并将几条消息发布到交易中的所有内容。我正在考虑使用dotnet transactionscope,但我看不到如何将其应用于Confluent Kafka软件包。


        var _database = client.GetDatabase(mongoUrl.DatabaseName);

        using(var transactionScope = new TransactionScope());
        {
          var drinkCollections = _database.GetCollection<Drinks>("drinks").AsTransactionCollection();
          var orders = _database.GetCollection<Orders>("orders").AsTransactionCollection();

          _kafkaProducer.ProduceAsync("commands", "orderID", new OrderCommand() { Waiter = "John", Price = 3.35m });

          transactionScope.Complete();
      }

显然,如果生产方法未能发布到Kafka,则应回滚或取消对MongoDB的持久性。

不确定如何实施交易。任何想法都将不胜感激。

Trying to persist some data in MongoDB and publish a couple of messages to Kafka everything inside of a transaction. I was thinking of using the dotnet TransactionScope but I do not see how to apply it to the Confluent Kafka packages.


        var _database = client.GetDatabase(mongoUrl.DatabaseName);

        using(var transactionScope = new TransactionScope());
        {
          var drinkCollections = _database.GetCollection<Drinks>("drinks").AsTransactionCollection();
          var orders = _database.GetCollection<Orders>("orders").AsTransactionCollection();

          _kafkaProducer.ProduceAsync("commands", "orderID", new OrderCommand() { Waiter = "John", Price = 3.35m });

          transactionScope.Complete();
      }

Obviously if the ProduceAsync method fails to publish to Kafka then it should rollback or cancel the persistence to MongoDb.

Not sure how to implement the transaction. Any ideas would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文