feat: Update Cloud SQL MySQL samples to include more connection methods#7054
Conversation
777bccf to
a111d15
Compare
|
Are you going to add any tags? (SnippetBot is unhappy) |
Yes, I'll need to change the tags in our documentation right after merging this. |
|
@shubha-rajan Let's pin the tags in the docs. Once we're done with all the samples, we'll make a pass and unpin everything. |
Co-authored-by: Jack Wotherspoon <jackwoth@google.com>
| @SuppressFBWarnings( | ||
| value = "USBR_UNNECESSARY_STORE_BEFORE_RETURN", | ||
| justification = "Necessary for sample region tag.") |
There was a problem hiding this comment.
Can we scrap these SuppressFBWarnings or suppress in the pom somehow?
There was a problem hiding this comment.
They're already scrapped. I think you're looking at an outdated version?
| // See the link below for more information on how to configure SSL Certificates for use with | ||
| // MySQL Connector/J | ||
| // |
There was a problem hiding this comment.
// See the _link below_ for more information on how to configure SSL Certificates for use with
// MySQL Connector/J
// 👀There was a problem hiding this comment.
added the link
| private void createTable(DataSource pool) throws SQLException { | ||
| // Safely attempt to create the table schema. | ||
| try (Connection conn = pool.getConnection()) { | ||
| String stmt = | ||
| "CREATE TABLE IF NOT EXISTS votes ( " | ||
| + "vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, candidate CHAR(6) NOT NULL," | ||
| + " PRIMARY KEY (vote_id) );"; | ||
| try (PreparedStatement createTableStatement = conn.prepareStatement(stmt);) { | ||
| createTableStatement.execute(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Used to validate user input. All user provided data should be validated and sanitized before | ||
| // being used something like a SQL query. Returns null if invalid. | ||
| @Nullable | ||
| private String validateTeam(String input) { | ||
| if (input != null) { | ||
| input = input.toUpperCase(Locale.ENGLISH); | ||
| // Must be either "TABS" or "SPACES" | ||
| if (!"TABS".equals(input) && !"SPACES".equals(input)) { | ||
| return null; | ||
| } | ||
| } | ||
| return input; | ||
| } |
There was a problem hiding this comment.
Is there any way to abstract some of this out so it's shared between the function and the servlet?
…nectorConnectionPoolFactory.java Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com>
…plateData.java Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com>
| private static final String DB_NAME = System.getenv("DB_NAME"); | ||
|
|
||
| public static DataSource createConnectionPool() { | ||
|
|
| // Unix sockets are not natively supported in Java, so it is necessary to use the Cloud SQL JDBC | ||
| // Socket Factory to connect. | ||
| // Note: For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections | ||
| // which is usually preferable to using the Cloud SQL Proxy with Unix sockets. | ||
| // See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details. |
There was a problem hiding this comment.
nit: Should we s/SocketFactory/Java connector here?
| import java.util.Locale; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| public class InputValidator { |
There was a problem hiding this comment.
nit: You could merge InputValidator and DatabaseSetup into a utils class instead of each having it's own file if you wanted
|
|
||
| // [END cloud_sql_mysql_servlet_connect_connector] | ||
| // Unix sockets are not natively supported in Java, so it is necessary to use the Cloud SQL | ||
| // Java Connector to connect. |
There was a problem hiding this comment.
I think we need another sentence here that explains what enabling the Unix socket does. Something like, "When setting INSTANCE_UNIX_SOCKET, the connector will do X, Y, and Z," where X, Y, and Z are the things it does differently than just using the connector without a Unix socket.
|
Merge-on-green attempted to merge your PR for 6 hours, but it was not mergeable because either one of your required status checks failed, one of your required reviews was not approved, or there is a do not merge label. Learn more about your required status checks here: https://help.github.com/en/github/administering-a-repository/enabling-required-status-checks. You can remove and reapply the label to re-run the bot. |
Please review this PR to bring this sample into compliance with the standards in go/csql-v2-connectivity-samples. This PR only covers MySQL; I'll update the other two samples once this one is reviewed.
pom.xmlparent set to latestshared-configurationmvn clean verifyrequiredmvn -P lint checkstyle:checkrequiredmvn -P lint clean compile pmd:cpd-check spotbugs:checkadvisory only