Skip to content

Commit b594c48

Browse files
authored
fix(cloud-sql): Updated sample to include full JDBC url in comments. (GoogleCloudPlatform#3562)
* added full jdbc url in cloud sql sample comments * updated variable names and moved URL * moved full JDBC url to before config options
1 parent fead834 commit b594c48

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ private DataSource createConnectionPool() {
5050
// The configuration object specifies behaviors for the connection pool.
5151
HikariConfig config = new HikariConfig();
5252

53+
// The following URL is equivalent to setting the config options below:
54+
// jdbc:postgresql:///<DB_NAME>?cloudSqlInstance=<CLOUD_SQL_CONNECTION_NAME>&
55+
// socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<DB_USER>&password=<DB_PASS>
56+
// See the link below for more info on building a JDBC URL for the Cloud SQL JDBC Socket Factory
57+
// https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#creating-the-jdbc-url
58+
5359
// Configure which instance and what database user to connect with.
5460
config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
5561
config.setUsername(DB_USER); // e.g. "root", "postgres"
@@ -110,7 +116,7 @@ private void createTable(DataSource pool) throws SQLException {
110116
"CREATE TABLE IF NOT EXISTS votes ( "
111117
+ "vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, candidate CHAR(6) NOT NULL,"
112118
+ " PRIMARY KEY (vote_id) );";
113-
try (PreparedStatement createTableStatement = conn.prepareStatement(stmt); ) {
119+
try (PreparedStatement createTableStatement = conn.prepareStatement(stmt);) {
114120
createTableStatement.execute();
115121
}
116122
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ private DataSource createConnectionPool() {
5050
// The configuration object specifies behaviors for the connection pool.
5151
HikariConfig config = new HikariConfig();
5252

53+
// The following URL is equivalent to setting the config options below:
54+
// jdbc:postgresql:///<DB_NAME>?cloudSqlInstance=<CLOUD_SQL_CONNECTION_NAME>&
55+
// socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<DB_USER>&password=<DB_PASS>
56+
// See the link below for more info on building a JDBC URL for the Cloud SQL JDBC Socket Factory
57+
// https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#creating-the-jdbc-url
58+
5359
// Configure which instance and what database user to connect with.
5460
config.setJdbcUrl(String.format("jdbc:postgresql:///%s", DB_NAME));
5561
config.setUsername(DB_USER); // e.g. "root", "postgres"
@@ -60,6 +66,8 @@ private DataSource createConnectionPool() {
6066
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory");
6167
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
6268

69+
70+
6371
// ... Specify additional connection properties here.
6472
// [START_EXCLUDE]
6573

@@ -110,7 +118,7 @@ private void createTable(DataSource pool) throws SQLException {
110118
"CREATE TABLE IF NOT EXISTS votes ( "
111119
+ "vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, candidate CHAR(6) NOT NULL,"
112120
+ " PRIMARY KEY (vote_id) );";
113-
try (PreparedStatement createTableStatement = conn.prepareStatement(stmt); ) {
121+
try (PreparedStatement createTableStatement = conn.prepareStatement(stmt);) {
114122
createTableStatement.execute();
115123
}
116124
}

0 commit comments

Comments
 (0)