How to use db in erlang correctly?
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.
精彩评论