fix: fix context propagation in UseDB method #1278
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What did this pull request do?
First and foremost, Thank you for creating this great project.
WHAT
The issue where the context was always set to
context.Background()
in the UseDB method has been fixed. Previously, when performing database operations, the original context (including cancellation and timeout) was ignored, and a newcontext.Background()
was always used. After the fix, the method now usesdb.Statement.Context
to propagate the existing context. If no context is set, it defaults tocontext.Background()
.In the following example, even though the context (ctx) was passed from the higher layer, the original context was not properly propagated in the previous implementation:
WHY
By always setting the context to
context.Background()
, the original context's cancellation and timeout settings were not applied to the database queries, leading to potential issues with request timeouts or cancellations not being respected. This fix ensures that the cancellation and timeout of the context are properly propagated, allowing database operations to be influenced by the context. This will ensure that when a context is passed from a higher layer, like in the example above, it will correctly reflect cancellations or timeouts.User Case Description