Reputation: 4742
I wish to connect to Google Cloud SQL using JDBC SocketFactory in a Spring 4 application.
The dependencies in pom.xml are:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.0.14</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>
The applicationContext.xml contains:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource" />
<property name="maximumPoolSize" value="10" />
<property name="idleTimeout" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="url">jdbc:postgresql://google/mydb?cloudSqlInstance=projectId:region:instance&socketFactory=com.google.cloud.sql.postgres.SocketFactory</prop>
<prop key="user">postgres</prop>
<prop key="password">password</prop>
</props>
</property>
</bean>
But when I run the application, I get the following exception:
Caused by: java.lang.IllegalArgumentException: cloudSqlInstance property not set. Please specify this property in the JDBC URL or the connection Properties with value in form "project:region:instance" at com.google.common.base.Preconditions.checkArgument(Preconditions.java:135)
What could be going wrong here?
Upvotes: 1
Views: 1940
Reputation: 11
I believe this would be caused by the use of PGSimpleDatasource. The Postgres datasources filters all connection url properties. The only allowed ones are those found in the following enum:
https://jdbc.postgresql.org/documentation/publicapi/org/postgresql/PGProperty.html
You can see the filtering here:
Since cloudSqlInstance is not on this list it will not be available to the socket factory.
The solution would propabaly be to use another datasource implementation.
Upvotes: 1