Skip to content

Commit dfbb05f

Browse files
authored
chore: updates to Cloud SQL samples (GoogleCloudPlatform#4352)
* add connection URL to comments of SQL Server Sample * add instructions for forcing ipType when connecting
1 parent 3b64697 commit dfbb05f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

cloud-sql/mysql/servlet/src/main/java/com/example/cloudsql/ConnectionPoolContextListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ private DataSource createConnectionPool() {
6666
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
6767
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
6868

69+
// The ipTypes argument can be used to specify a comma delimited list of preferred IP types
70+
// for connecting to a Cloud SQL instance. The argument ipTypes=PRIVATE will force the
71+
// SocketFactory to connect with an instance's associated private IP.
72+
config.addDataSourceProperty("ipTypes", "PUBLIC,PRIVATE");
73+
6974
// ... Specify additional connection properties here.
7075
// [START_EXCLUDE]
7176

cloud-sql/postgres/servlet/src/main/java/com/example/cloudsql/ConnectionPoolContextListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ private DataSource createConnectionPool() {
6666
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory");
6767
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
6868

69-
69+
// The ipTypes argument can be used to specify a comma delimited list of preferred IP types
70+
// for connecting to a Cloud SQL instance. The argument ipTypes=PRIVATE will force the
71+
// SocketFactory to connect with an instance's associated private IP.
72+
config.addDataSourceProperty("ipTypes", "PUBLIC,PRIVATE");
7073

7174
// ... Specify additional connection properties here.
7275
// [START_EXCLUDE]

cloud-sql/sqlserver/servlet/src/main/java/com/example/cloudsql/ConnectionPoolContextListener.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@ private DataSource createConnectionPool() {
4545
// The configuration object specifies behaviors for the connection pool.
4646
HikariConfig config = new HikariConfig();
4747

48+
// The following is equivalent to setting the config options below:
49+
// jdbc:sqlserver://;user=<DB_USER>;password=<DB_PASS>;databaseName=<DB_NAME>;
50+
// socketFactoryClass=com.google.cloud.sql.sqlserver.SocketFactory;
51+
// socketFactoryConstructorArg=<CLOUD_SQL_CONNECTION_NAME>
52+
53+
// See the link below for more info on building a JDBC URL for the Cloud SQL JDBC Socket Factory
54+
// https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#creating-the-jdbc-url
55+
4856
// Configure which instance and what database user to connect with.
4957
config
50-
.setDataSourceClassName(String.format("com.microsoft.sqlserver.jdbc.SQLServerDataSource"));
58+
.setDataSourceClassName("com.microsoft.sqlserver.jdbc.SQLServerDataSource");
5159
config.setUsername(DB_USER); // e.g. "root", "sqlserver"
5260
config.setPassword(DB_PASS); // e.g. "my-password"
5361
config.addDataSourceProperty("databaseName", DB_NAME);

0 commit comments

Comments
 (0)