Search code examples
mysqlspring-bootgoogle-cloud-platformgoogle-cloud-storagegoogle-cloud-sql

Connecting local spring boot app to google cloud SQL through google proxy


I am new to Java/spring boot and I am trying to use google's cloud SQL as a database for my project. I have downloaded the google proxy and authenticated using google cloud SDK. When I start the proxy and connect using SSMS it connects and works fine. The issue is when I try connect through the spring boot app I get errors. I am not sure if I am missing something in application properties or have forgot to import a dependency or something like that. Any help would be appreciated! I have included applications.properties, build.gradle and errors below. Thanks!

Application.properties

spring.jpa.hibernate.ddl-auto=create spring.datasource.url=jdbc:mysql://127.0.0.1:1433/{DBNAME} spring.datasource.username={DMUSERNAME} spring.datasource.password={DMPASSWORD}

build.gradle

   id 'org.springframework.boot' version '2.2.1.RELEASE'
   id 'io.spring.dependency-management' version '1.0.8.RELEASE'
   id 'java'
}

group = '{GROUPNAME}'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
   compileOnly {
       extendsFrom annotationProcessor
   }
}

repositories {
   mavenCentral()
   maven { url 'https://repo.spring.io/milestone' }
}

ext {
   set('springCloudVersion', "Hoxton.RC2")
}

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-web'
   compileOnly 'org.projectlombok:lombok'
   annotationProcessor 'org.projectlombok:lombok'
   testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
   testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
   implementation 'org.springframework.cloud:spring-cloud-gcp-starter'
   implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
   runtime 'mysql:mysql-connector-java'
   implementation 'org.springframework.boot:spring-boot-starter-jdbc'

}

dependencyManagement {
   imports {
       mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

test {
   useJUnitPlatform()
}

Errors

  Caused by: java.net.ConnectException: Connection refused: no further information
  Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service 
  [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 
  'hibernate.dialect' not set
   Process 'command 'C:/Program Files/Java/jdk-13.0.1/bin/java.exe'' finished with non-zero exit value 1

Solution

  • Which database are you using? Your question is tagged mysql.

    Port 1433 is for Microsoft SQL Server. SSMS is for Microsoft SQL Server

    Your connection string is for MySQL and not for SQL Server.

    Change this:

    spring.datasource.url=jdbc:mysql://127.0.0.1:1433/{DBNAME}
    

    To this:

    spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433/{DBNAME}
    

    The above connection strings use TCP Sockets (and not Unix Sockets) and require that the Cloud SQL Proxy was also set up using TCP Sockets. For example, when you started Cloud SQL Proxy in TCP Socket mode, it should look like this:

    ./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:1433
    

    Since you have successfully connected via SSMS, I assume that you have set up the correct roles for Cloud SQL access.