uber-go/fx

How to deal with request scoped lifecycle for database transient connections?

leandrocurioso opened this issue · 0 comments

I'm using fx + mysql + gin and I need some help to figure out how to turn a database connection scoped.

Let's go through this points:

  • Registered a factory (New) to return a db connection;

  • My repository struct uses this connection inside its New constructor function;

  • The project is a rest api and I need to know how can I request a connection during a request and use the same connection during the entire request, then when the request is done I need to close it.

  • The main point is if I use 2 different repositories in the same request I get 2 diferent connections and this doesn't make sense since the cost of opening a connection is high! I should be able to open a new connection "OnRequestBegin" and release "OnRequestEnd". I worked with some injectors before for other languages but I didn't find anything related to it for FX.

Am I losing something? Does anyone know how to solve this?