@@ -34,13 +34,14 @@ public class ConnectionPoolContextListener implements ServletContextListener {
3434
3535 // Saving credentials in environment variables is convenient, but not secure - consider a more
3636 // secure solution such as https://cloud.google.com/kms/ to help keep secrets safe.
37- private static final String CLOUD_SQL_INSTANCE_NAME = System .getenv ("CLOUD_SQL_INSTANCE_NAME" );
37+ private static final String CLOUD_SQL_CONNECTION_NAME = System .getenv (
38+ "CLOUD_SQL_CONNECTION_NAME" );
3839 private static final String DB_USER = System .getenv ("DB_USER" );
3940 private static final String DB_PASS = System .getenv ("DB_PASS" );
4041 private static final String DB_NAME = System .getenv ("DB_NAME" );
4142
4243 private DataSource createConnectionPool () {
43- // [START cloud_sql_mysql_connection_pool ]
44+ // [START cloud_sql_mysql_servlet_create ]
4445 // The configuration object specifies behaviors for the connection pool.
4546 HikariConfig config = new HikariConfig ();
4647
@@ -52,50 +53,49 @@ private DataSource createConnectionPool() {
5253 // For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections.
5354 // See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details.
5455 config .addDataSourceProperty ("socketFactory" , "com.google.cloud.sql.mysql.SocketFactory" );
55- config .addDataSourceProperty ("cloudSqlInstance" , CLOUD_SQL_INSTANCE_NAME );
56+ config .addDataSourceProperty ("cloudSqlInstance" , CLOUD_SQL_CONNECTION_NAME );
5657 config .addDataSourceProperty ("useSSL" , "false" );
5758
5859 // ... Specify additional connection properties here.
59-
6060 // [START_EXCLUDE]
6161
62- // [START cloud_sql_mysql_limit_connections ]
62+ // [START cloud_sql_mysql_servlet_limit ]
6363 // maximumPoolSize limits the total number of concurrent connections this pool will keep. Ideal
6464 // values for this setting are highly variable on app design, infrastructure, and database.
6565 config .setMaximumPoolSize (5 );
6666 // minimumIdle is the minimum number of idle connections Hikari maintains in the pool.
6767 // Additional connections will be established to meet this value unless the pool is full.
6868 config .setMinimumIdle (5 );
69- // [END cloud_sql_mysql_limit_connections ]
69+ // [END cloud_sql_mysql_servlet_limit ]
7070
71- // [START cloud_sql_mysql_connection_timeout ]
71+ // [START cloud_sql_mysql_servlet_timeout ]
7272 // setConnectionTimeout is the maximum number of milliseconds to wait for a connection checkout.
7373 // Any attempt to retrieve a connection from this pool that exceeds the set limit will throw an
7474 // SQLException.
7575 config .setConnectionTimeout (10000 ); // 10 seconds
7676 // idleTimeout is the maximum amount of time a connection can sit in the pool. Connections that
7777 // sit idle for this many milliseconds are retried if minimumIdle is exceeded.
7878 config .setIdleTimeout (600000 ); // 10 minutes
79- // [END cloud_sql_mysql_connection_timeout ]
79+ // [END cloud_sql_mysql_servlet_timeout ]
8080
81- // [START cloud_sql_mysql_connection_backoff ]
81+ // [START cloud_sql_mysql_servlet_backoff ]
8282 // Hikari automatically delays between failed connection attempts, eventually reaching a
8383 // maximum delay of `connectionTimeout / 2` between attempts.
84- // [END cloud_sql_mysql_connection_backoff ]
84+ // [END cloud_sql_mysql_servlet_backoff ]
8585
86- // [START cloud_sql_mysql_connection_lifetime ]
86+ // [START cloud_sql_mysql_servlet_lifetime ]
8787 // maxLifetime is the maximum possible lifetime of a connection in the pool. Connections that
8888 // live longer than this many milliseconds will be closed and reestablished between uses. This
8989 // value should be several minutes shorter than the database's timeout value to avoid unexpected
9090 // terminations.
9191 config .setMaxLifetime (1800000 ); // 30 minutes
92- // [END cloud_sql_mysql_connection_lifetime ]
92+ // [END cloud_sql_mysql_servlet_lifetime ]
9393
9494 // [END_EXCLUDE]
9595
9696 // Initialize the connection pool using the configuration object.
9797 DataSource pool = new HikariDataSource (config );
98- // [END cloud_sql_mysql_connection_pool ]
98+ // [END cloud_sql_mysql_servlet_create ]
9999 return pool ;
100100 }
101101
0 commit comments