Skip to content

Commit 629c781

Browse files
committed
Use try-with-resources
1 parent 6dd422e commit 629c781

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

samples/mysql-schema/src/main/java/com/github/containersolutions/operator/sample/SchemaController.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.sql.DriverManager;
1616
import java.sql.ResultSet;
1717
import java.sql.SQLException;
18+
import java.sql.Statement;
1819
import java.util.Base64;
1920

2021
import static java.lang.String.format;
@@ -38,21 +39,27 @@ public SchemaController(KubernetesClient kubernetesClient) {
3839
public UpdateControl<Schema> createOrUpdateResource(Schema schema, Context<Schema> context) {
3940
try (Connection connection = getConnection()) {
4041
if (!schemaExists(connection, schema.getMetadata().getName())) {
41-
connection.createStatement().execute(format("CREATE SCHEMA `%1$s` DEFAULT CHARACTER SET %2$s",
42+
try (Statement statement = connection.createStatement()) {
43+
statement.execute(format("CREATE SCHEMA `%1$s` DEFAULT CHARACTER SET %2$s",
4244
schema.getMetadata().getName(),
4345
schema.getSpec().getEncoding()));
46+
}
4447

4548
String password = RandomStringUtils.randomAlphanumeric(16);
4649
String userName = String.format(USERNAME_FORMAT,
4750
schema.getMetadata().getName());
4851
String secretName = String.format(SECRET_FORMAT,
4952
schema.getMetadata().getName());
50-
connection.createStatement().execute(format(
51-
"CREATE USER '%1$s' IDENTIFIED BY '%2$s'",
52-
userName, password));
53-
connection.createStatement().execute(format(
54-
"GRANT ALL ON `%1$s`.* TO '%2$s'",
55-
schema.getMetadata().getName(), userName));
53+
try (Statement statement = connection.createStatement()) {
54+
statement.execute(format("CREATE USER '%1$s' IDENTIFIED BY '%2$s'",
55+
userName,
56+
password));
57+
}
58+
try (Statement statement = connection.createStatement()) {
59+
statement.execute(format("GRANT ALL ON `%1$s`.* TO '%2$s'",
60+
schema.getMetadata().getName(),
61+
userName));
62+
}
5663
Secret credentialsSecret = new SecretBuilder()
5764
.withNewMetadata().withName(secretName).endMetadata()
5865
.addToData("MYSQL_USERNAME", Base64.getEncoder().encodeToString(userName.getBytes()))
@@ -95,11 +102,17 @@ public boolean deleteResource(Schema schema, Context<Schema> context) {
95102

96103
try (Connection connection = getConnection()) {
97104
if (schemaExists(connection, schema.getMetadata().getName())) {
98-
connection.createStatement().execute("DROP DATABASE `" + schema.getMetadata().getName() + "`");
105+
try (Statement statement = connection.createStatement()) {
106+
statement.execute(format("DROP DATABASE `%1$s`",
107+
schema.getMetadata().getName()));
108+
}
99109
log.info("Deleted Schema '{}'", schema.getMetadata().getName());
100110

101111
if (userExists(connection, schema.getStatus().getUserName())) {
102-
connection.createStatement().execute("DROP USER '" + schema.getStatus().getUserName() + "'");
112+
try (Statement statement = connection.createStatement()) {
113+
statement.execute(format("DROP USER '%1$s'",
114+
schema.getStatus().getUserName()));
115+
}
103116
log.info("Deleted User '{}'", schema.getStatus().getUserName());
104117
}
105118

@@ -134,9 +147,10 @@ private boolean schemaExists(Connection connection, String schemaName) throws SQ
134147
}
135148

136149
private boolean userExists(Connection connection, String userName) throws SQLException {
137-
ResultSet resultSet = connection.createStatement().executeQuery(
138-
format("SELECT User FROM mysql.user WHERE User='%1$s'", userName)
139-
);
140-
return resultSet.first();
150+
try (Statement statement = connection.createStatement()) {
151+
ResultSet resultSet = statement.executeQuery(format("SELECT User FROM mysql.user WHERE User='%1$s'",
152+
userName));
153+
return resultSet.first();
154+
}
141155
}
142156
}

0 commit comments

Comments
 (0)