Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singleton per request object #11

Closed
omarkohl opened this issue Dec 22, 2015 · 2 comments
Closed

Singleton per request object #11

omarkohl opened this issue Dec 22, 2015 · 2 comments

Comments

@omarkohl
Copy link

One issue I ran into with your dbsession service example (that uses a service factory) is the following:

When looking for the service different sessions are returned depending on the context. This is by design. Citing the documentation:

The factory will be used at most once per request/context/name combination.

Having different DB session in one request is quite ugly and I can think of no use case for it. How did I end up with different contexts? By using the request object outside a view function invoked directly with pyramid.threadlocal.get_current_request . In that case the context is None.

This example by Jon Rosebaugh - What the Zope Transaction Manager Means To Me (and you) would work but I wanted to use pyramid_services (for consistency, because I'm already using it in other parts of the application).

I'm aware that this might not be the place for this since it classifies more as a question than an issue, but you could at least fix the example to be more clear that

request.find_service(name='db')

will return different sessions depending on the request context.

My workaround is to always pass context=None:

request.find_service(name='db', context=None)

Is there a simpler solution (e.g. a singleton per request)? register_service() would register a singleton object for the whole application, but I do want a different DB session for every request.

@mmerickel
Copy link
Owner

I see what you're saying. Ideally we would use the original context interface as the cache key, not the context supplied.

@mmerickel
Copy link
Owner

We can certainly make that change. I'm not sure it's entirely obvious how to go about it right now, but if you want to investigate please feel free to open a PR with a fix or even just add a test case to the test suite.

mmerickel added a commit that referenced this issue Feb 3, 2016
mmerickel added a commit that referenced this issue Feb 3, 2016
avoid making new services per specific-context when the service is registered
for a more general interface

Fixes #11
mmerickel added a commit that referenced this issue Feb 3, 2016
mmerickel added a commit that referenced this issue Feb 3, 2016
avoid making new services per specific-context when the service is registered
for a more general interface

Fixes #11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants