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

Support deriving a DataSourceBuilder from a wrapped DataSource #31605

Closed
zeitlinger opened this issue Jul 7, 2022 · 2 comments
Closed

Support deriving a DataSourceBuilder from a wrapped DataSource #31605

zeitlinger opened this issue Jul 7, 2022 · 2 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@zeitlinger
Copy link

Wrapped data sources, such as OpenTelemetryDataSource are not detected correctly for flyway, because they are not unwrapped.

As a workaround, you can do this following.
It seems to work, but it would be better to generalize the idea of unwrapping. Maybe try to unwrap all data sources?
The workaround is based on this unwrap call.

public class MyOpenTelemetryDataSource extends OpenTelemetryDataSource
    implements EmbeddedDatabase {

  public ObservabilitySdkDataSource(DataSource delegate) {
    super(delegate);
  }

  @Override
  public void shutdown() {
    // this is not an embedded database, this is just a workaround be able to unwrap the database for flyway
  }
}

Without this workaround you get the following exception:

        at org.springframework.boot.jdbc.UnsupportedDataSourcePropertyException.throwIf(UnsupportedDataSourcePropertyException.java:36) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.jdbc.DataSourceBuilder$ReflectionDataSourceProperties.getMethod(DataSourceBuilder.java:560) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.jdbc.DataSourceBuilder$ReflectionDataSourceProperties.get(DataSourceBuilder.java:550) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.jdbc.DataSourceBuilder.build(DataSourceBuilder.java:178) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.getMigrationDataSource(FlywayAutoConfiguration.java:150) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.configureDataSource(FlywayAutoConfiguration.java:131) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.flyway(FlywayAutoConfiguration.java:118) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.20.jar:5.3.20]
        ... 114 common frames omitted

The exception is thrown when flyway tries to retrieve the url from the original datasource - which is wrapped by OpenTelemetryDataSource like in this setup:

spring:
  datasource:
    driverClassName: org.hsqldb.jdbc.JDBCDriver
    url: jdbc:hsqldb:mem:obsdemo
    username: sa
    password: sa
  flyway:
    baselineOnMigrate: true
    user: sa
    password: sa
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 7, 2022
@wilkinsona
Copy link
Member

Thanks for the suggestion.

In your configuration example above, the username and password for Flyway and the data source are the same. If this applies in your real application, can avoid the problem by removing spring.flyway.user and spring.flyway.password. The main datasource will then be used to migrate the database.

Another option is to define your own DataSource bean and annotate its @Bean method with @FlywayDataSource. This will allow you to provide a Flyway-specific data source which removes the need for us to try and derive one from the main data source.

We can consider broadening the cases in which derivation will unwrap the DataSource. I vaguely recall some problems in the past with unwrapping and certain JDBC drivers (Oracle perhaps) so we may not be able to do so.

@wilkinsona
Copy link
Member

I think #24154 is the Oracle UCP problem that I was remembering. I don't think it will prevent us from unwrapping more generally.

@wilkinsona wilkinsona changed the title support wrapped data sources Support deriving a DataSourceBuilder from a wrapped DataSource Jul 7, 2022
@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 8, 2022
@philwebb philwebb added this to the 3.0.x milestone Jul 8, 2022
@wilkinsona wilkinsona added type: enhancement A general enhancement and removed type: bug A general bug labels Jul 8, 2022
@wilkinsona wilkinsona self-assigned this Jul 8, 2022
@wilkinsona wilkinsona modified the milestones: 3.0.x, 3.0.0-M4 Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants