GoogleCloudPlatform / java-docs-samples

Java and Kotlin Code samples used on cloud.google.com
https://cloud.google.com/java
Apache License 2.0
1.78k stars 2.84k forks source link

[Bug] JDBC URL connection using db_username instead of db_name for accessing DB #8650

Open gusthavosouza opened 1 year ago

gusthavosouza commented 1 year ago

Hey, I am setting up an application on Google Cloud tech stack:

GCP SQL Postgress 15 Spring JPA 3.0.1 Google App Engine

followed the examples on: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/cloud-sql/postgres/servlet/src/main/java/com/example/cloudsql/ConnectorConnectionPoolFactory.java

My configs:

spring:
    cloud:
        appId: my-app-id
    datasource:
        hikari:
            data-source-properties:
                socketFactory: "com.google.cloud.sql.postgres.SocketFactory"
                cloudSqlInstance: ${INSTANCE_CONNECTION_NAME}
                sslmode: disable
        url: jdbc:postgresql://${DB_NAME}/
        password: ${DB_PASSWORD}
        username: ${DB_USERNAME}
    jpa:
        hibernate:
            ddl-auto: update
        properties:
            hibernate:
                dialect: org.hibernate.dialect.PostgreSQLDialect
    gcp:
        sql:
            database-name: ${DB_NAME}

My app.yaml


runtime: java17
instance_class: F1
env_variables:
  SPRING_PROFILES_ACTIVE: "prod"
  INSTANCE_CONNECTION_NAME: postgress_connection_name
  DB_NAME: db_name_anything
  DB_USERNAME: db_user_anything
  DB_PASSWORD: "_my_pass"
handlers:
  - url: /.*
    script: this field is required, but ignored
automatic_scaling:
  min_idle_instances: automatic
  max_idle_instances: 1
  min_pending_latency: automatic
  max_pending_latency: 0.030s
  max_instances: 1

Error:


o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 3D000
2023-09-17 12:43:26 default[v1]  2023-09-17T12:43:26.201Z ERROR 12 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : FATAL: database "db_name_anything" does not exist
2023-09-17 12:43:26 default[v1]  2023-09-17T12:43:26.208Z ERROR 12 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution [FATAL: database "db_name_anything" does not exist] [n/a]

I have solved the problem with creating the user as the name of the db, however, this seems like a bug to me.

Thank you!

minherz commented 10 months ago

The code sample in question is explained in the following documentation:

You can also look how to set up connection to Postgre.

Can you please follow the instructions and validate that if you cannot connect with a DB user that has a custom ID not matching the database name? If you follow these instructions and is able to reproduce the behavior, please share the detailed step-by-step reproduction scenario so we can troubleshoot the problem.

Thank you