在Azure Functions中重用MongoDB连接 [英] Reuse MongoDB connection in Azure Functions

查看:44
本文介绍了在Azure Functions中重用MongoDB连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了避免在连接数据库时出现性能问题,Azure Function文档建议我们重新使用数据库连接.如何在Java中重用MongoDB连接,以便将该连接重用于多个函数调用?

In order to avoid performance issues when connecting to databases, Azure Function documentation suggests that we reuse database connections. How to reuse a MongoDB connection in Java so that the connection is reused for multiple function invocations?

推荐答案

最佳选择在函数方法之外(例如, public static void Run()或其他方法),将连接创建为静态可以重用,而不必在每个请求中都创建新的连接.函数实例可以保留并重复使用,连接也可以

Best option is outside of the function method (e.g. public static void Run() or whatever), creating the connection as a static that can be reused without having to create a new connection each request. Function instances are kept around and re-used, and so would the connection

下面是伪代码(我不知道实际的MongoDb SDK,但是模式应该很好用)

Below is pseudo-code (I don’t know actual MongoDb SDK, but pattern should work great)

package com.hollan.com

// ...

private static MongoDbConnection _mongoConnection = new MongoDbConnection("http://myMongo:1234");

public void MyFunction(...) {
  _mongoConnection.Insert(...);
}

这篇关于在Azure Functions中重用MongoDB连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆