Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

starter-sql-mysql doesn't override spring.datasource.url #2537

Closed
GabrielBB opened this issue Sep 30, 2020 · 6 comments · Fixed by #2558
Closed

starter-sql-mysql doesn't override spring.datasource.url #2537

GabrielBB opened this issue Sep 30, 2020 · 6 comments · Fixed by #2558

Comments

@GabrielBB
Copy link

GabrielBB commented Sep 30, 2020

It connects to the Google cloud database but then liquibase tries to boot up and it picks the h2 database. I need h2 only for local and integration tests.

application.properties

spring.datasource.url=jdbc:h2:mem:none;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.cloud.gcp.sql.enabled=false

application-prod.properties

spring.cloud.gcp.sql.enabled=true
spring.cloud.gcp.sql.database-name=xxxx
spring.cloud.gcp.sql.instance-connection-name=xxxx:europe-west3:xxxx

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.3.4.RELEASE"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    kotlin("jvm") version "1.4.0"
    kotlin("plugin.spring") version "1.4.0"
    kotlin("plugin.jpa") version "1.4.0"
}

group = "com.github.gabrielbb"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-mail")
    implementation("org.springframework.cloud:spring-cloud-gcp-starter-storage:1.2.5.RELEASE")

    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("io.github.microutils:kotlin-logging:1.12.0")

    runtimeOnly("org.liquibase:liquibase-core")
    runtimeOnly("com.h2database:h2")
    runtimeOnly("org.springframework.cloud:spring-cloud-gcp-starter-sql-mysql:1.2.5.RELEASE")

    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
        exclude(module = "mockito-core")
    }
    testImplementation("io.mockk:mockk:1.10.0")
    testImplementation("com.ninja-squad:springmockk:2.0.3")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}
[ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Default MYSQL JdbcUrl provider. Connecting to jdbc:mysql://google/xxxx?socketFactory=com.google.cloud.sql.mysql.SocketFactory&cloudSqlInstance=xxxx:europe-west3:xxxx with driver com.mysql.jdbc.Driver
2020-09-30 04:17:05.959 VETLoading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-09-30 04:17:06.055 VET2020-09-30 08:17:06.055 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-09-30 04:17:06.061 VET2020-09-30 08:17:06.061 WARN 1 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2020-09-30 04:17:06.141 VET2020-09-30 08:17:06.141 WARN 1 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jdbc:h2:mem:none;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE
2020-09-30 04:17:06.145 VET2020-09-30 08:17:06.145 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
@meltsufin
Copy link
Contributor

How are you enabling the prod profile?

@GabrielBB
Copy link
Author

How are you enabling the prod profile?

Using SPRING_PROFILES_ACTIVE env var

        gcloud run deploy "$SERVICE_NAME" \
            --image="gcr.io/$PROJECT_ID/$SERVICE_NAME:$GITHUB_SHA" \
            --update-env-vars SPRING_PROFILES_ACTIVE=prod

@meltsufin
Copy link
Contributor

Can you run with debug=true and provide the auto-configuration report?
Note that the default profile would still be active, and you're relying on GcpCloudSqlAutoConfiguration to ignore spring.datasource.url, which it should, but I'm not sure how this interacts with Liquidbase.

@GabrielBB
Copy link
Author

GabrielBB commented Oct 5, 2020

@meltsufin I ended up just adding this manually in application-prod.properties : jdbc:mysql://google/xxxx?socketFactory=com.google.cloud.sql.mysql.SocketFactory&cloudSqlInstance=xxxx:europe-west3:xxxx. However I can no longer help figuring out this problem because I also ended up migrating to AWS. I hope you can figure it out, if you don't close this issue now

@meltsufin
Copy link
Contributor

@meltsufin Thanks for the update.
I've been able to reproduce this issue by simply adding the spring.datasource.url to our MySQL sample: https://github.com/spring-cloud/spring-cloud-gcp/blob/master/spring-cloud-gcp-samples/spring-cloud-gcp-sql-mysql-sample/src/main/resources/application.properties

For some reason our DataSourceProperties bean overriding using @Primary is not working anymore in GcpCloudSqlAutoConfiguration.

@meltsufin meltsufin added P2 and removed P3 labels Oct 5, 2020
@meltsufin meltsufin changed the title starter-sql-mysql doesn't override spring.datasource.url for LiquibaseConfiguration starter-sql-mysql doesn't override spring.datasource.url Oct 5, 2020
@meltsufin
Copy link
Contributor

Fixed in #2558

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging a pull request may close this issue.

2 participants