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

Transaction improvements + use new propagation context API #2189

Merged
merged 37 commits into from
Jun 6, 2023
Merged

Conversation

dstepanov
Copy link
Contributor

@dstepanov dstepanov commented May 15, 2023

This PR:

  • Propagate the transaction context using new Propagation API
  • Removes previous TX manager classes copied from Spring
  • Splits TX manager into TX manager and Connection manager
  • Introduces new @Connection annotation that works in a similar way as @Transaction, allowing to have one open connection/session for multiple repository operations (The annotation is conflicting with JDBC Connection, maybe it should be something else like @WithConnection WDYT?)
  • Disables opening TX for every repository operation
  • Hibernate configuration improvements
  • Spring JDBC / TX improvements

Resolves #1875

@dstepanov dstepanov marked this pull request as draft May 15, 2023 18:15
@dstepanov dstepanov changed the title Transaction improvements + use new propagation context API WIP Transaction improvements + use new propagation context API May 15, 2023
@dstepanov dstepanov changed the title WIP Transaction improvements + use new propagation context API WIP Transaction improvements + use new propagation context API [skip ci] May 15, 2023
graemerocher pushed a commit to micronaut-projects/micronaut-core that referenced this pull request May 17, 2023
Initial version of the propagation API.

Main points:
- The propagated context is always immutable and consist of different propagated elements similar to Kotlin Coroutines context
- Propagated element can implement `ThreadPropagatedContextElement` to setup/restore thread locals
- The propagated state is never captured but the context needs to be extended and pushed down the downstream
- The capturing API is not needed anymore, this should eliminate a lot of overhead from the reactive code
- The context is automatically propagated to the Kotlin coroutines
- In the Reactor context should be propagated as whole with utility method to extract/extend it (The Reactor context needs to be modified manually)
- The implementation is using try-resources to propagate the contest without extra overhead, this might change in the future to support scoped local, but at this moment, I think this is the best solution.

I want to merge this first PR to have the orther work mergable.

Next PRs will remove the existing instrumentation propagation, add docs etc.

There are already examples how it should be used:
- Tracing micronaut-projects/micronaut-tracing#281
- Micronaut Data micronaut-projects/micronaut-data#2189 (This is a big PR because of the changes in the TX management and removal of forked Spring TX code)
@dstepanov dstepanov force-pushed the prop branch 2 times, most recently from 3044125 to 3021d01 Compare May 30, 2023 16:59
@dstepanov
Copy link
Contributor Author

@radovanradic Would you have any idea why r2dbc-example-kotlin is not picking up the mariadb r2dbc driver?

@radovanradic
Copy link
Contributor

@radovanradic Would you have any idea why r2dbc-example-kotlin is not picking up the mariadb r2dbc driver?

Investigated it here #2245, not sure if this fix makes sense

@dstepanov
Copy link
Contributor Author

I need to exclude Sonar from the TCK projects as it includes warning for the incorrect usecases

@dstepanov dstepanov marked this pull request as ready for review June 5, 2023 21:44
@dstepanov dstepanov requested review from graemerocher and radovanradic and removed request for graemerocher June 5, 2023 21:44
// prepareTransactionalConnection(connection, definition);

if (!onComplete.isEmpty()) {
Collections.reverse(onComplete);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid having to reverse this collection

@@ -6,7 +6,7 @@

import static org.junit.jupiter.api.Assertions.*;

@MicronautTest
@MicronautTest(transactional = false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this disabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

Comment on lines -13 to 14
private final SynchronousTransactionManager<Connection> transactionManager;
private final TransactionOperations<Session> transactionManager;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add more documentation to breaks about this change and how programmatic transactions have changed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous way is still supported (SynchronousTransactionManager<Session>) but I would like to eliminate SynchronousTransactionManager because of the detached TX is not the best for propagation

@sonarcloud
Copy link

sonarcloud bot commented Jun 6, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug E 7 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 97 Code Smells

55.5% 55.5% Coverage
5.7% 5.7% Duplication

@dstepanov dstepanov merged commit 0a1695a into master Jun 6, 2023
@dstepanov dstepanov deleted the prop branch June 6, 2023 19:17
timyates added a commit to micronaut-projects/micronaut-crac that referenced this pull request Jun 19, 2023
timyates added a commit to micronaut-projects/micronaut-crac that referenced this pull request Jun 19, 2023
…v4.0.0-m9 (#134)

* fix(deps): update dependency io.micronaut.data:micronaut-data-bom to v4.0.0-m9

* Fix for Data M9. Classes were moved in micronaut-projects/micronaut-data#2189

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Tim Yates <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

Transactional processing changes in Micronaut 4
3 participants