15
15
import java .sql .DriverManager ;
16
16
import java .sql .ResultSet ;
17
17
import java .sql .SQLException ;
18
+ import java .sql .Statement ;
18
19
import java .util .Base64 ;
19
20
20
21
import static java .lang .String .format ;
@@ -38,21 +39,27 @@ public SchemaController(KubernetesClient kubernetesClient) {
38
39
public UpdateControl <Schema > createOrUpdateResource (Schema schema , Context <Schema > context ) {
39
40
try (Connection connection = getConnection ()) {
40
41
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" ,
42
44
schema .getMetadata ().getName (),
43
45
schema .getSpec ().getEncoding ()));
46
+ }
44
47
45
48
String password = RandomStringUtils .randomAlphanumeric (16 );
46
49
String userName = String .format (USERNAME_FORMAT ,
47
50
schema .getMetadata ().getName ());
48
51
String secretName = String .format (SECRET_FORMAT ,
49
52
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
+ }
56
63
Secret credentialsSecret = new SecretBuilder ()
57
64
.withNewMetadata ().withName (secretName ).endMetadata ()
58
65
.addToData ("MYSQL_USERNAME" , Base64 .getEncoder ().encodeToString (userName .getBytes ()))
@@ -95,11 +102,17 @@ public boolean deleteResource(Schema schema, Context<Schema> context) {
95
102
96
103
try (Connection connection = getConnection ()) {
97
104
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
+ }
99
109
log .info ("Deleted Schema '{}'" , schema .getMetadata ().getName ());
100
110
101
111
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
+ }
103
116
log .info ("Deleted User '{}'" , schema .getStatus ().getUserName ());
104
117
}
105
118
@@ -134,9 +147,10 @@ private boolean schemaExists(Connection connection, String schemaName) throws SQ
134
147
}
135
148
136
149
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
+ }
141
155
}
142
156
}
0 commit comments