Connection concurrency issues
dannyshisler opened this issue · 0 comments
I've found a strange side-effect from trying to use multiple concurrent db connections.
I wrote a base class that instantiates a new connection each time I want to make a db call. On user sign-in or sign-up, I persist to pouchdb my logged-in User
and an AuthToken
using User
and Auth
classes that derive from the base class. Both put
calls happen at roughly the same time - when logging the put
calls I can see that the async function calls overlap each other.
The problem is that when I reopen the app and try to read the Auth
Token from pouchdb, I get the last logged in User
returned from the get call even though I pass the id of the AuthToken
.
To be clear I do a db.get("LastAuthToken")
and get an object back with the id: {_id: "LastLoggedInUser"}
which I wouldn't have thought would be possible?
I've resolved the issue by using a singleton pattern that stores the db connection in a stand-alone variable so that there's only ever a single instance of the db connection. This seems to have resolved the problem (although I haven't done extensive testing yet).
A couple of questions:
-
Is this expected behaviour or a known issue? I haven't seen any reference to problems with multiple concurrent connections anywhere.
-
Is it safe to leave this db connection open? Is it likely to cause any issues or problems?
Thanks