-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Update Cloud SQL MySQL samples to include more connection methods #7054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
bd79d41
update mysql sample to include new region tags
shubha-rajan 65b4ef7
add license header
shubha-rajan aa506d7
move connection code for TCP and connector to separate files
shubha-rajan d8f9198
add functions sample
shubha-rajan 6e1ead5
add missing license headers
shubha-rajan 09b8580
Update region tag
shubha-rajan b1915ee
reformat code
shubha-rajan a111d15
fix function sample
shubha-rajan 34b7b4d
get function working
shubha-rajan 0c2ef0f
update instructions for deploying to Functions
shubha-rajan dbf82e3
Update cloud-sql/mysql/servlet/.env.yaml
shubha-rajan 2367b8e
add imports and environment variables to sample
shubha-rajan b51e07f
Update README.md
shubha-rajan 0134a35
Update ConnectorConnectionPoolFactory.java
shubha-rajan 9d992b3
formatting
shubha-rajan 90016c2
Update cloud-sql/mysql/servlet/src/main/java/com/example/cloudsql/Con…
shubha-rajan 8fc722f
Update TcpConnectionPoolFactory.java
shubha-rajan d78947c
Update cloud-sql/mysql/servlet/src/main/java/com/example/cloudsql/Tem…
shubha-rajan dc809d5
Factor out common methods in servlet and cloud function
shubha-rajan ff96d05
combine DatabaseSetup and InputValidator classes
shubha-rajan 77b0a55
refactor cloud function to use initialization on demand
shubha-rajan e2e3183
formatting
shubha-rajan eeebe2e
Update ConnectorConnectionPoolFactory.java
shubha-rajan ba8e5c7
Update ConnectorConnectionPoolFactory.java
shubha-rajan b4b461c
Update TcpConnectionPoolFactory.java
shubha-rajan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| INSTANCE_CONNECTION_NAME: <PROJECT-ID>:<INSTANCE-REGION>:INSTANCE-NAME> | ||
| INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:INSTANCE-NAME> | ||
| INSTANCE_HOST: '127.0.0.1' | ||
| DB_PORT: 3306 | ||
| DB_USER: <YOUR_DB_USER_NAME> | ||
| DB_PASS: <YOUR_DB_PASSWORD> | ||
| DB_NAME: <YOUR_DB_NAME> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
cloud-sql/mysql/servlet/src/main/java/com/example/cloudsql/ConnectionPoolFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.cloudsql; | ||
|
|
||
| import com.zaxxer.hikari.HikariConfig; | ||
|
|
||
| public class ConnectionPoolFactory { | ||
|
|
||
| public static HikariConfig configureConnectionPool(HikariConfig config) { | ||
| // [START cloud_sql_mysql_servlet_limit] | ||
| // maximumPoolSize limits the total number of concurrent connections this pool will keep. Ideal | ||
| // values for this setting are highly variable on app design, infrastructure, and database. | ||
| config.setMaximumPoolSize(5); | ||
| // minimumIdle is the minimum number of idle connections Hikari maintains in the pool. | ||
| // Additional connections will be established to meet this value unless the pool is full. | ||
| config.setMinimumIdle(5); | ||
| // [END cloud_sql_mysql_servlet_limit] | ||
|
|
||
| // [START cloud_sql_mysql_servlet_timeout] | ||
| // setConnectionTimeout is the maximum number of milliseconds to wait for a connection checkout. | ||
| // Any attempt to retrieve a connection from this pool that exceeds the set limit will throw an | ||
| // SQLException. | ||
| config.setConnectionTimeout(10000); // 10 seconds | ||
| // idleTimeout is the maximum amount of time a connection can sit in the pool. Connections that | ||
| // sit idle for this many milliseconds are retried if minimumIdle is exceeded. | ||
| config.setIdleTimeout(600000); // 10 minutes | ||
| // [END cloud_sql_mysql_servlet_timeout] | ||
|
|
||
| // [START cloud_sql_mysql_servlet_backoff] | ||
| // Hikari automatically delays between failed connection attempts, eventually reaching a | ||
| // maximum delay of `connectionTimeout / 2` between attempts. | ||
| // [END cloud_sql_mysql_servlet_backoff] | ||
|
|
||
| // [START cloud_sql_mysql_servlet_lifetime] | ||
| // maxLifetime is the maximum possible lifetime of a connection in the pool. Connections that | ||
| // live longer than this many milliseconds will be closed and reestablished between uses. This | ||
| // value should be several minutes shorter than the database's timeout value to avoid unexpected | ||
| // terminations. | ||
| config.setMaxLifetime(1800000); // 30 minutes | ||
| // [END cloud_sql_mysql_servlet_lifetime] | ||
| return config; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.