Spring JPA applicationContext.xml for Google Cloud SQL Postgres

101 views
Skip to first unread message

Aniket Bhadane

unread,
Jul 16, 2019, 9:44:15 AM7/16/19
to Google Cloud SQL discuss
I want to connect to Google Cloud SQL Postgres using my Spring 4 application running on GKE using JPA. Previously, my applicationContext.xml which connected to database on a container in the same pod as application, contained:


<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
 
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>


<bean id="entityManagerFactory"
 
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
 
<property name="dataSource" ref="dataSource" />
 
<property name="packagesToScan" value="com.avaya.myproject.entity" />
 
<property name="jpaVendorAdapter" ref="hbAdapterBean_pgsql"/>
 
<property name="jpaProperties">
 
<props>
 
<prop key="hibernate.show_sql">false</prop>
 
<prop key="hibernate.format_sql">true</prop>
 
<prop key="hibernate.hbm2ddl.auto">update</prop>
 
<prop key="hibernate.id.new_generator_mappings">true</prop>
 
</props>
 
</property>
</bean>


<bean id="hbAdapterBean_pgsql" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
 
<property name="showSql" value="false"></property>  
 
<property name="generateDdl" value="true"></property>  
 
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"></property>  
</bean>  


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
 
<property name="driverClassName" value="org.postgresql.Driver"></property>  
 
<property name="url" value="jdbc:postgresql://${DB_HOST_}:${DB_PORT_}/${DB_NAME_}"></property>  
 
<property name="username" value="${DB_USER_}"></property>  
 
<property name="password" value="${DB_PASSWORD_}"></property>  
</bean>
 
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 
<constructor-arg ref="dataSource"/>
</bean>


I'm struggling to figure out what changes are required in the xml for connecting with Google Cloud SQL Postgres.

Harmit Rishi (Cloud Platform Support)

unread,
Jul 17, 2019, 2:58:50 PM7/17/19
to google-cloud...@googlegroups.com
Hello, 

Thank you for using Google Groups!

Based on my understanding of Cloud SQL connection requirements, you would have to use a Cloud SQL proxy for connecting your instance with GKE. You may find further details of this here. However, I would like to clarify part of your post, would you be able to elaborate what you meant when you said previously? Does that mean it was working before? 

Looking forward to hearing from you.


Tenzing Wangdi

unread,
Jul 17, 2019, 4:47:54 PM7/17/19
to Google Cloud SQL discuss
You can connect via Cloud Proxy or through private IP. For private IP, you need to enable / check if VPC native is enabled for your GKE. 
Reply all
Reply to author
Forward
0 new messages