开发者

How to use db in erlang correctly?

开发者 https://www.devze.com 2023-04-05 05:37 出处:网络
I write some example on erlang and mongodb. As driver for mongodb I use emongo. Problem is if I make connection in one module I can not reuse this conn开发者_运维百科ection in different modules. In C/

I write some example on erlang and mongodb. As driver for mongodb I use emongo. Problem is if I make connection in one module I can not reuse this conn开发者_运维百科ection in different modules. In C/C++ or other objective languages I can make singleton and use it. How can I reuse open connection in erlang? Thanks.


You should be able to re-use the pool ID from any part of your application.

In module A:

emongo:insert(test, SomeCollection, Document).

In module B:

emongo:insert(test, AnotherCollection, AnotherDocument).

As long as you keep using the same id (in this case, test) either by hard coding it into the different modules or by sending it in a message, you should be able to use the library from any part of the application.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号