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

Make DbContext pooling accessible to non-DI applications #24137

Closed
roji opened this issue Feb 12, 2021 · 3 comments · Fixed by #24712
Closed

Make DbContext pooling accessible to non-DI applications #24137

roji opened this issue Feb 12, 2021 · 3 comments · Fixed by #24712
Assignees
Labels
area-dbcontext area-perf closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@roji
Copy link
Member

roji commented Feb 12, 2021

The setup of DbContext pooling is currently tied to DI. While it's possible to set it up without DI, that currently requires using internal types.

@zliebersbach
Copy link

@roji I notice you mention it is possible to set up pooling without DI, would you be able to cite any examples of how to implement this? It would be an amazing help!

@roji
Copy link
Member Author

roji commented Apr 14, 2021

Here's a way to do it:

var optionsBuilder = new DbContextOptionsBuilder<FortunesContext>();
// Configure context options
var pool = new DbContextPool<FortunesContext>(optionsBuilder.options);

// Now that you have a pool, you can use it as follows:

var poolable = _contextPool.Rent();
poolable.SetLease(new DbContextLease(_contextPool, standalone: true));
using var dbContext = (FortunesContext)poolable;

However, note again the above uses internal APIs, and so may break in future releases.

@roji roji self-assigned this Apr 20, 2021
@roji roji modified the milestones: Backlog, 6.0.0 Apr 20, 2021
@roji
Copy link
Member Author

roji commented Apr 20, 2021

As discussed with @ajcvickers offline, bypassing DI in the TE Fortunes benchmark yields around 2.8% RPS. So I'll be doing this for 6.0.0.

roji added a commit that referenced this issue Apr 21, 2021
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 21, 2021
roji added a commit that referenced this issue Apr 21, 2021
roji added a commit that referenced this issue Apr 21, 2021
roji added a commit that referenced this issue Apr 22, 2021
Closes #24137

(cherry picked from commit acfec75)
roji added a commit that referenced this issue Apr 22, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-preview5 Apr 26, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-preview5, 6.0.0 Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dbcontext area-perf closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants