Open cfecherolle opened 2 weeks ago
Hey @cfecherolle, thanks for the comprehensive report. First, I must say that such problems are very difficult to solve even when one has access to a debugger and can see exactly what's happening in the code, let alone over the internet like this 🙂
I also can't confirm with 100% certainty whether this problem could be caused by the Zonky library or not. If you're using refresh mode, you need to be aware that between individual tests, the previous database might be discarded and a new one created. This means that the data source instance gets replaced, which results in transactions from the previous test not being propagated to the next test. However, I don't think this is your case.
In the problem description, you mention using ChainedTransactionManager
with two data sources. What data sources are these? Are they both zonky embedded databases? And if so, are they two distinct data sources, or are you referencing the same data source instance in both DatasourceTransactionManagers
?
I'm asking because the DatasourceTransactionManager
implementation holds active transactions in a thread local map that uses the data source instance as a key. So if you have two DatasourceTransactionManagers
referencing the same data source instance, they actually share this map of active transactions. Therefore, when the first DatasourceTransactionManager
calls the bindResource
method, this resource gets registered for both transaction managers, which could cause the error described above.
In such a case, the solution would be to create two separate databases, see https://github.com/zonkyio/embedded-database-spring-test#creating-multiple-databases-within-a-single-test-class, one for each DatasourceTransactionManager
. But this is just my speculation. It's possible that the problem is caused by something different.
If you discover any new findings, let me know, I'm always happy to help.
Hey @tomix26, thank you for taking the time to write such a thorough answer!
I think the ChainedTransactionManager
was a bit weird to begin with, I originally set it up in my tests to replace Atomikos because it was easier at that time, and I did not need actual JTA handling in my tests.
Now that I have updated a lot of things related to Spring Boot 3 (including Atomikos) it was actually really easy to just switch to the same beans I have for production code and stop using a dummy ChainedTransactionManager
in my tests.
As I stopped using it, the issue went away.
I guess it was totally unrelated to this library, after all. Thanks again for your kind answer and support!
Hi,
I recently migrated my Spring Boot 2 application to Spring Boot 3. With this migration came a lot of dependencies updates, as expected, and there is now a specific use case where my integration tests using the embedded PostgreSQL database won't run.
I get this exception:
I have narrowed the issue to a weird situation: I'm using
@Sql
annotations (two, to be exact) to execute SQL scripts on my database before running the tests. These annotations trigger a call toexecuteSqlScripts
inSqlScriptsTestExecutionListener
and seem to work as intended.bindResource
inTransactionSynchronizationManager
is made in order to start the transaction.BlockingDatabaseWrapper
by adding it to theresources
map. So far, so good.ChainedTransactionManager
with two datasources, a secondBlockingDatabaseWrapper
resource is then bound usingbindResource
and added to the map too.doCleanupAfterCompletion
ofDatasourceTransactionManager
, the first resource is unbound (removed from the map).@Sql
script is then executed, and the same steps are repeated. The resources map is left empty, as expected again.BlockingDatabaseWrapper
resource corresponding to the datasource I'm using with Spring Batch (bindResource
is called indoGetConnection
ofDataSourceUtils
)bindResource
is called again indoBegin
methodDatasourceTransactionManager
and tries to bind the resource that was bound just before, resulting in the error that I copy-pasted above.I'm not 100% sure this has something to do with the
embedded-database-spring-test
, but the Spring Batch jobs start fine when I launch then in "production" code with an actual database configuration so, I'm wondering whether it's related or not. Feel free to tell me if it seems like it's unrelated though.Thanks in advance!