@@ -34,13 +34,14 @@ public class ConnectionPoolContextListener implements ServletContextListener {
34
34
35
35
// Saving credentials in environment variables is convenient, but not secure - consider a more
36
36
// 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" );
38
39
private static final String DB_USER = System .getenv ("DB_USER" );
39
40
private static final String DB_PASS = System .getenv ("DB_PASS" );
40
41
private static final String DB_NAME = System .getenv ("DB_NAME" );
41
42
42
43
private DataSource createConnectionPool () {
43
- // [START cloud_sql_mysql_connection_pool ]
44
+ // [START cloud_sql_mysql_servlet_create ]
44
45
// The configuration object specifies behaviors for the connection pool.
45
46
HikariConfig config = new HikariConfig ();
46
47
@@ -52,50 +53,49 @@ private DataSource createConnectionPool() {
52
53
// For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections.
53
54
// See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details.
54
55
config .addDataSourceProperty ("socketFactory" , "com.google.cloud.sql.mysql.SocketFactory" );
8000
tr>55
- config .addDataSourceProperty ("cloudSqlInstance" , CLOUD_SQL_INSTANCE_NAME );
56
+ config .addDataSourceProperty ("cloudSqlInstance" , CLOUD_SQL_CONNECTION_NAME );
56
57
config .addDataSourceProperty ("useSSL" , "false" );
57
58
58
59
// ... Specify additional connection properties here.
59
-
60
60
// [START_EXCLUDE]
61
61
62
- // [START cloud_sql_mysql_limit_connections ]
62
+ // [START cloud_sql_mysql_servlet_limit ]
63
63
// maximumPoolSize limits the total number of concurrent connections this pool will keep. Ideal
64
64
// values for this setting are highly variable on app design, infrastructure, and database.
65
65
config .setMaximumPoolSize (5 );
66
66
// minimumIdle is the minimum number of idle connections Hikari maintains in the pool.
67
67
// Additional connections will be established to meet this value unless the pool is full.
68
68
config .setMinimumIdle (5 );
69
- // [END cloud_sql_mysql_limit_connections ]
69
+ // [END cloud_sql_mysql_servlet_limit ]
70
70
71
- // [START cloud_sql_mysql_connection_timeout ]
71
+ // [START cloud_sql_mysql_servlet_timeout ]
72
72
// setConnectionTimeout is the maximum number of milliseconds to wait for a connection checkout.
73
73
// Any attempt to retrieve a connection from this pool that exceeds the set limit will throw an
74
74
// SQLException.
75
75
config .setConnectionTimeout (10000 ); // 10 seconds
76
76
// idleTimeout is the maximum amount of time a connection can sit in the pool. Connections that
77
77
// sit idle for this many milliseconds are retried if minimumIdle is exceeded.
78
78
config .setIdleTimeout (600000 ); // 10 minutes
79
- // [END cloud_sql_mysql_connection_timeout ]
79
+ // [END cloud_sql_mysql_servlet_timeout ]
80
80
81
- // [START cloud_sql_mysql_connection_backoff ]
81
+ // [START cloud_sql_mysql_servlet_backoff ]
82
82
// Hikari automatically delays between failed connection attempts, eventually reaching a
83
83
// maximum delay of `connectionTimeout / 2` between attempts.
84
- // [END cloud_sql_mysql_connection_backoff ]
84
+ // [END cloud_sql_mysql_servlet_backoff ]
85
85
86
- // [START cloud_sql_mysql_connection_lifetime ]
86
+ // [START cloud_sql_mysql_servlet_lifetime ]
87
87
// maxLifetime is the maximum possible lifetime of a connection in the pool. Connections that
88
88
// live longer than this many milliseconds will be closed and reestablished between uses. This
89
89
// value should be several minutes shorter than the database's timeout value to avoid unexpected
90
90
// terminations.
91
91
config .setMaxLifetime (1800000 ); // 30 minutes
92
- // [END cloud_sql_mysql_connection_lifetime ]
92
+ // [END cloud_sql_mysql_servlet_lifetime ]
93
93
94
94
// [END_EXCLUDE]
95
95
96
96
// Initialize the connection pool using the configuration object.
97
97
DataSource pool = new HikariDataSource (config );
98
- // [END cloud_sql_mysql_connection_pool ]
98
+ // [END cloud_sql_mysql_servlet_create ]
99
99
return pool ;
100
100
}
101
101
0 commit comments