Skip to content

Unwrap DataSource hidden behind InfrastructureProxy in SqlScriptsTestExecutionListener #26422

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

Closed
stolsvik opened this issue Jan 21, 2021 · 3 comments
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@stolsvik
Copy link

stolsvik commented Jan 21, 2021

in method SqlScriptsTestExecutionListener.executeSqlScripts(...), around line 213, the following code resides:

DataSource dataSourceFromTxMgr = getDataSourceFromTransactionManager(txMgr);
// Ensure user configured an appropriate DataSource/TransactionManager pair.
if (dataSource != null && dataSourceFromTxMgr != null && !dataSource.equals(dataSourceFromTxMgr)) {
    throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " +
          "the configured DataSource [%s] (named '%s') is not the one associated with " +
          "transaction manager [%s] (named '%s').", testContext, dataSource.getClass().getName(),
           dsName, txMgr.getClass().getName(), tmName));
}

This seems to contradict the logic used in DataSourceUtils which handles such equality checks taking into account that a DataSource may be an implementation of InfrastrutureProxy.

We have a setup where the DataSource in the Spring context is directly the database DataSource (well, it is pooled), while the DataSource given to the DataSourceTransactionManager is a wrapped version. However, the wrapping class implements InfrastructureProxy, which ensures that all Spring JDBC stuff - which utilizes the DataSourceUtils class to fetch (and release) Connections - actually handles this perfectly: Even though e.g. the JdbcTemplate has gotten the "plain" DataSource, while, again, the TxMgr has the wrapped DataSource, when in a Spring managed Transaction, the JdbcTemplate ends up with Connections from the wrapped DataSource (in the DataSourceTransactionManager) due to the logic wrt. InfrastructureProxy in DataSourceUtils.

However, this is not the case in SqlScriptsTestExecutionListener, due to the much plainer .equals(..) check being performed there. Basically, had the check not been there, it would have worked. Also, had the check taken into account the InfrastructureProxy logic, it would also have worked.

(And, due to the equality being checked is dataSourceFromSpringContext.equals(dataSourceFromTxMgr) - and not the other way around - I cannot hack this to work either. The only way around that would be to also wrap the DataSource in the Spring context too (to trick the .equals(..)) - in which case I could just as well put the same DataSource in Spring context as the one given to the DataSourceTransactionManager, which of course would fix this. However, the problem is that these two are handled in different code bases, and it is not always the situation that we want to do that wrapping of the DataSource given to the DataSourceTransactionManager.).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 21, 2021
@sbrannen sbrannen added the in: test Issues in the test module label Jan 22, 2021
@sbrannen sbrannen changed the title Test framework: SqlScriptsTestExecutionListener does not honor InfrastructureProxy SqlScriptsTestExecutionListener does not honor InfrastructureProxy for DataSource Jan 22, 2021
@sbrannen
Copy link
Member

sbrannen commented Jan 22, 2021

Thanks for raising the issue.

Are you on Spring Framework 5.3.x?

Also, out of curiosity...

Do you have custom code that generates the proxy implementing DataSource and InfrastructureProxy?

Or is the proxy being created by something in one of the Spring portfolio projects or a third-party library?

@sbrannen sbrannen added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 22, 2021
@sbrannen sbrannen self-assigned this Jan 22, 2021
@sbrannen sbrannen added this to the 5.3.4 milestone Jan 22, 2021
@sbrannen sbrannen changed the title SqlScriptsTestExecutionListener does not honor InfrastructureProxy for DataSource Unwrap DataSource hidden behind InfrastructureProxy in SqlScriptsTestExecutionListener Jan 22, 2021
@sbrannen
Copy link
Member

Support for unwrapping proxied data sources has been added in 4a7a225.

Feel free to try this out in the upcoming 5.3.4 snapshots and let us know if you run into any issues.

@stolsvik
Copy link
Author

@sbrannen It is in external library code, not in any Spring portfolio projects. I realized how this worked - and was duly impressed by the logic! - and utilized it. Then, in one of our projects, we got hit by this issue.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants