-
Notifications
You must be signed in to change notification settings - Fork 197
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
@Transactional
annotation misbehavior
#2943
@Transactional
annotation misbehavior
#2943
Comments
@sdelamo Hi Sergio, hope you're doing well :) Sorry for bothering you, but I would like to know if you could give me a hand with these bugs. I can offer to help fixing them if you guys give me some pointers on where should I take a look at. I tried debugging and I found a couple of things I put in the README.md of the example application, but I'm not sure how Micronaut Data is weaving things together. Thanks again!! |
Looks like you need:
Otherwise, the TX manager is not created |
Hi dstepanov thanks for your answer :) Regarding your suggestion, I'm mocking the repositories in that test. Why is the TX manager still required in this case? Also, the Please let me know if you need more info, I'll be more than happy to expand on my explanation. |
The interceptor is applied around your mock and triggers a new TX.
It works in the same way, your test was incorrect. |
Thanks for explaining that. It's weird for me to still need a DB to run a test with mocked repositories, but adding those properties made the test to pass. Regarding the other misbehavior (at least for me) I just pushed a change. Can you give it a try please and share your thoughts with me? Apologies for bothering with this, but it kind of seems like a bug to me. |
I tried and it works. What's wrong? |
Expected Behavior
When
@Transactional
is applied at a class level, all non-private methods should be automatically wrapped in a transaction and have a connection available.When
@Transactional
is applied at a method level (non-private), it should be wrapped in a transaction and have a connection available.When using
MockBean
,TransactionalInterceptor
shouldn't be applied (as it expects aDataSource
to be created and the idea of mocking a repository is not having to have one).It doesn't matter if it is:
@jakarta.transaction.Transactional
@io.micronaut.transaction.annotation.Transactional
The bug manifests itself with both annotations.
Actual Behaviour
When
@Transactional
is applied at a class level, all non-private methods are not automatically wrapped in a transaction and/or don't have a connection available. The following exception is thrown:io.micronaut.data.connection.exceptions.NoConnectionException: Expected an existing connection, but none was found.
When
@Transactional
is applied at a method level, tests involving those methods fail (see this Micronaut test bug I created and duplicated here in Micronaut-core repo).It doesn't matter if it is
@jakarta.transaction.Transactional
or@io.micronaut.transaction.annotation.Transactional
.Bug test cases
@Transactional
2@Transactional
2@Transactional
2Steps To Reproduce
For your convenience, you can use the provided app example. But here are the steps anyway:
@Repository
and@Transactional
(at class level).JdbcOperations
object and run a query against H2, which should have been populated beforehand (using Liquibase, for instance). Query can be a simpleSELECT * FROM mytable
.@EventListener
or any other place where you inject the repository and call that method).Interestingly, another similar error happens during test execution (depending on what you annotate with
@Transactional
). Please take a look at:A workaround I tried (hoping to pass tests and not receiving exceptions at runtime) but without any luck is to annotate both the class and non-private methods with
@Transactional
.Thanks.
Environment Information
Example Application
https://github.com/Nahuel92/Micronaut-test-bug
I added my findings to the README.md file.
Version
4.4.2
Footnotes
Tests mock repositories with
@MockBean(RepositoryClass.class)
. ↩It doesn't matter whether it's
@jakarta.transaction.Transactional
or
@io.micronaut.transaction.annotation.Transactional
. ↩ ↩2 ↩3The text was updated successfully, but these errors were encountered: