@@ -67,28 +67,25 @@ public void performDataMigration(Connection conn) {
6767 }
6868
6969 private void addExtractTemplateAndVolumeColumns (Connection conn ) {
70- PreparedStatement pstmt = null ;
71- ResultSet rs = null ;
7270
73- try {
71+ try (PreparedStatement selectTemplateInfostmt = conn .prepareStatement ("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'template_store_ref' AND COLUMN_NAME = 'download_url_created'" );
72+ ResultSet templateInfoResults = selectTemplateInfostmt .executeQuery ();
73+ PreparedStatement addDownloadUrlCreatedToTemplateStorerefstatement = conn .prepareStatement ("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url_created` datetime" );
74+ PreparedStatement addDownloadUrlToTemplateStorerefstatement = conn .prepareStatement ("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url` varchar(255)" );
75+ PreparedStatement selectVolumeInfostmt = conn .prepareStatement ("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'volume_store_ref' AND COLUMN_NAME = 'download_url_created'" );
76+ ResultSet volumeInfoResults = selectVolumeInfostmt .executeQuery ();
77+ PreparedStatement addDownloadUrlCreatedToVolumeStorerefstatement = conn .prepareStatement ("ALTER TABLE `cloud`.`volume_store_ref` ADD COLUMN `download_url_created` datetime" );
78+ ) {
7479
7580 // Add download_url_created, download_url to template_store_ref
76- pstmt = conn .prepareStatement ("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'template_store_ref' AND COLUMN_NAME = 'download_url_created'" );
77- rs = pstmt .executeQuery ();
78- if (!rs .next ()) {
79- pstmt = conn .prepareStatement ("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url_created` datetime" );
80- pstmt .executeUpdate ();
81-
82- pstmt = conn .prepareStatement ("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url` varchar(255)" );
83- pstmt .executeUpdate ();
81+ if (!templateInfoResults .next ()) {
82+ addDownloadUrlCreatedToTemplateStorerefstatement .executeUpdate ();
83+ addDownloadUrlToTemplateStorerefstatement .executeUpdate ();
8484 }
8585
8686 // Add download_url_created to volume_store_ref - note download_url already exists
87- pstmt = conn .prepareStatement ("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'volume_store_ref' AND COLUMN_NAME = 'download_url_created'" );
88- rs = pstmt .executeQuery ();
89- if (!rs .next ()) {
90- pstmt = conn .prepareStatement ("ALTER TABLE `cloud`.`volume_store_ref` ADD COLUMN `download_url_created` datetime" );
91- pstmt .executeUpdate ();
87+ if (!volumeInfoResults .next ()) {
88+ addDownloadUrlCreatedToVolumeStorerefstatement .executeUpdate ();
9289 }
9390
9491 } catch (SQLException e ) {
@@ -97,21 +94,11 @@ private void addExtractTemplateAndVolumeColumns(Connection conn) {
9794 }
9895
9996 private void secondaryIpsAccountAndDomainIdsUpdate (Connection conn ) {
100- PreparedStatement pstmt = null ;
101- PreparedStatement pstmtVm = null ;
102- PreparedStatement pstmtNw = null ;
103- PreparedStatement pstmtUpdate = null ;
104-
105- ResultSet rs1 = null ;
106- ResultSet vmRs = null ;
107- ResultSet networkRs = null ;
108-
10997 String secondIpsSql = "SELECT id, vmId, network_id, account_id, domain_id, ip4_address FROM `cloud`.`nic_secondary_ips`" ;
11098
111- try {
112- pstmt = conn .prepareStatement (secondIpsSql );
113- rs1 = pstmt .executeQuery ();
114-
99+ try (PreparedStatement pstmt = conn .prepareStatement (secondIpsSql );
100+ ResultSet rs1 = pstmt .executeQuery ();
101+ ) {
115102 while (rs1 .next ()) {
116103 long ipId = rs1 .getLong (1 );
117104 long vmId = rs1 .getLong (2 );
@@ -120,130 +107,77 @@ private void secondaryIpsAccountAndDomainIdsUpdate(Connection conn) {
120107 long domainId = rs1 .getLong (5 );
121108 String ipAddr = rs1 .getString (6 );
122109
123- pstmtVm = conn .prepareStatement ("SELECT account_id, domain_id FROM `cloud`.`vm_instance` where id = ?" );
124- pstmtVm .setLong (1 ,vmId );
125-
126- vmRs = pstmtVm .executeQuery ();
127-
128- if (vmRs .next ()) {
129- long vmAccountId = vmRs .getLong (1 );
130- long vmDomainId = vmRs .getLong (2 );
131-
132- if (vmAccountId != accountId && vmAccountId != domainId ) {
133- // update the secondary ip accountid and domainid to vm accountid domainid
134- // check the network type. If network is shared accountid doaminid needs to be updated in
135- // in both nic_secondary_ips table and user_ip_address table
136-
137- pstmtUpdate = conn .prepareStatement ("UPDATE `cloud`.`nic_secondary_ips` SET account_id = ?, domain_id= ? WHERE id = ?" );
138- pstmtUpdate .setLong (1 , vmAccountId );
139- pstmtUpdate .setLong (2 ,vmDomainId );
140- pstmtUpdate .setLong (3 ,ipId );
141- pstmtUpdate .executeUpdate ();
142- pstmtUpdate .close ();
143-
144- pstmtNw = conn .prepareStatement ("SELECT guest_type FROM `cloud`.`networks` where id = ?" );
145- pstmtNw .setLong (1 ,networkId );
146-
147- networkRs = pstmtNw .executeQuery ();
148- if (networkRs .next ()) {
149- String guesttype = networkRs .getString (1 );
150-
151- if (guesttype .equals (Network .GuestType .Shared .toString ())) {
152- pstmtUpdate = conn .prepareStatement ("UPDATE `cloud`.`user_ip_address` SET account_id = ?, domain_id= ? WHERE public_ip_address = ?" );
153- pstmtUpdate .setLong (1 ,vmAccountId );
154- pstmtUpdate .setLong (2 ,vmDomainId );
155- pstmtUpdate .setString (3 ,ipAddr );
156- pstmtUpdate .executeUpdate ();
157- pstmtUpdate .close ();
158-
159- }
160- }
161- networkRs .close ();
162- networkRs = null ;
163- pstmtNw .close ();
164- pstmtNw = null ;
110+ try (PreparedStatement pstmtVm = conn .prepareStatement ("SELECT account_id, domain_id FROM `cloud`.`vm_instance` where id = ?" );) {
111+ pstmtVm .setLong (1 ,vmId );
112+
113+ try (ResultSet vmRs = pstmtVm .executeQuery ();) {
114+
115+ if (vmRs .next ()) {
116+ long vmAccountId = vmRs .getLong (1 );
117+ long vmDomainId = vmRs .getLong (2 );
118+
119+ if (vmAccountId != accountId && vmAccountId != domainId ) {
120+ // update the secondary ip accountid and domainid to vm accountid domainid
121+ // check the network type. If network is shared accountid doaminid needs to be updated in
122+ // in both nic_secondary_ips table and user_ip_address table
123+
124+ try (PreparedStatement pstmtUpdate = conn .prepareStatement ("UPDATE `cloud`.`nic_secondary_ips` SET account_id = ?, domain_id= ? WHERE id = ?" );) {
125+ pstmtUpdate .setLong (1 , vmAccountId );
126+ pstmtUpdate .setLong (2 ,vmDomainId );
127+ pstmtUpdate .setLong (3 ,ipId );
128+ pstmtUpdate .executeUpdate ();
129+ } catch (SQLException e ) {
130+ throw new CloudRuntimeException ("Exception while updating secondary ip for nic " + ipId , e );
131+ }
132+
133+ try (PreparedStatement pstmtNw = conn .prepareStatement ("SELECT guest_type FROM `cloud`.`networks` where id = ?" );) {
134+ pstmtNw .setLong (1 ,networkId );
135+
136+ try (ResultSet networkRs = pstmtNw .executeQuery ();) {
137+ if (networkRs .next ()) {
138+ String guesttype = networkRs .getString (1 );
139+
140+ if (guesttype .equals (Network .GuestType .Shared .toString ())) {
141+ try (PreparedStatement pstmtUpdate = conn .prepareStatement ("UPDATE `cloud`.`user_ip_address` SET account_id = ?, domain_id= ? WHERE public_ip_address = ?" );) {
142+ pstmtUpdate .setLong (1 ,vmAccountId );
143+ pstmtUpdate .setLong (2 ,vmDomainId );
144+ pstmtUpdate .setString (3 ,ipAddr );
145+ pstmtUpdate .executeUpdate ();
146+ } catch (SQLException e ) {
147+ throw new CloudRuntimeException ("Exception while updating public ip " + ipAddr , e );
148+ }
149+ }
150+ }
151+ } catch (SQLException e ) {
152+ throw new CloudRuntimeException ("Exception while retrieving guest type for network " + networkId , e );
153+ }
154+
155+ } catch (SQLException e ) {
156+ throw new CloudRuntimeException ("Exception while retrieving guest type for network " + networkId , e );
157+ }
158+ } // if
159+ } // if
165160 }
166- } //if
167-
168- pstmtVm .close ();
169- pstmtVm = null ;
170- vmRs .close ();
171- vmRs = null ;
161+ }
172162 } // while
173-
174-
175163 } catch (SQLException e ) {
176164 throw new CloudRuntimeException ("Exception while Moving private zone information to dedicated resources" , e );
177- } finally {
178-
179- if (pstmt != null ) {
180- try {
181- pstmt .close ();
182-
183- } catch (SQLException e ) {
184- }
185- }
186-
187-
188- if (rs1 != null ) {
189- try {
190- rs1 .close ();
191- } catch (SQLException e ) {
192- }
193- }
194-
195-
196-
197- if (pstmtVm != null ) {
198- try {
199- pstmtVm .close ();
200- } catch (SQLException e ) {
201- }
202- }
203-
204- if (vmRs != null ) {
205- try {
206- vmRs .close ();
207- } catch (SQLException e ) {
208- }
209- }
210-
211-
212-
213- if (pstmtNw != null ) {
214- try {
215- pstmtNw .close ();
216-
217- } catch (SQLException e ) {
218- }
219- }
220-
221-
222- if (networkRs != null ) {
223- try {
224- networkRs .close ();
225- } catch (SQLException e ) {
226- }
227- }
228165 }
229166 s_logger .debug ("Done updating vm nic secondary ip account and domain ids" );
230167 }
231168
232169
233170 private void moveCidrsToTheirOwnTable (Connection conn ) {
234- PreparedStatement pstmtItem = null ;
235- PreparedStatement pstmtCidr = null ;
236- ResultSet rsItems = null ;
171+ s_logger .debug ("Moving network acl item cidrs to a row per cidr" );
237172
238173 String networkAclItemSql = "SELECT id, cidr FROM `cloud`.`network_acl_item`" ;
174+ String networkAclItemCidrSql = "INSERT INTO `cloud`.`network_acl_item_cidrs` (network_acl_item_id, cidr) VALUES (?,?)" ;
239175
240- s_logger . debug ( "Moving network acl item cidrs to a row per cidr" );
241- try {
242- pstmtItem = conn .prepareStatement (networkAclItemSql );
243- rsItems = pstmtItem . executeQuery ();
176+ try ( PreparedStatement pstmtItem = conn . prepareStatement ( networkAclItemSql );
177+ ResultSet rsItems = pstmtItem . executeQuery ();
178+ PreparedStatement pstmtCidr = conn .prepareStatement (networkAclItemCidrSql );
179+ ) {
244180
245- String networkAclItemCidrSql = "INSERT INTO `cloud`.`network_acl_item_cidrs` (network_acl_item_id, cidr) VALUES (?,?)" ;
246- pstmtCidr = conn .prepareStatement (networkAclItemCidrSql );
247181
248182 // for each network acl item
249183 while (rsItems .next ()) {
@@ -262,58 +196,33 @@ private void moveCidrsToTheirOwnTable(Connection conn) {
262196 }
263197 } catch (SQLException e ) {
264198 throw new CloudRuntimeException ("Exception while Moving network acl item cidrs to a row per cidr" , e );
265- } finally {
266-
267- if (pstmtItem != null ) {
268- try {
269- pstmtItem .close ();
270-
271- } catch (SQLException e ) {
272- }
273- }
274- if (pstmtCidr != null ) {
275- try {
276- pstmtCidr .close ();
277-
278- } catch (SQLException e ) {
279- }
280- }
281199 }
282200 s_logger .debug ("Done moving network acl item cidrs to a row per cidr" );
283201 }
284202
285203 private void updateVlanUris (Connection conn ) {
286204 s_logger .debug ("updating vlan URIs" );
287- PreparedStatement pstmt = null ;
288- ResultSet rs = null ;
289- try {
290- pstmt = conn .prepareStatement ("SELECT id, vlan_id FROM `cloud`.`vlan` where vlan_id not like '%:%'" );
291- rs = pstmt .executeQuery ();
292- while (rs .next ()) {
293- long id = rs .getLong (1 );
294- String vlan = rs .getString (2 );
205+ try (PreparedStatement selectstatement = conn .prepareStatement ("SELECT id, vlan_id FROM `cloud`.`vlan` where vlan_id not like '%:%'" );
206+ ResultSet results = selectstatement .executeQuery ()) {
207+
208+ while (results .next ()) {
209+ long id = results .getLong (1 );
210+ String vlan = results .getString (2 );
295211 if (vlan == null || "" .equals (vlan )) {
296212 continue ;
297213 }
298214 String vlanUri = BroadcastDomainType .Vlan .toUri (vlan ).toString ();
299- pstmt = conn .prepareStatement ("update `cloud`.`vlan` set vlan_id=? where id=?" );
300- pstmt .setString (1 , vlanUri );
301- pstmt .setLong (2 , id );
302- pstmt .executeUpdate ();
215+ try (PreparedStatement updatestatement = conn .prepareStatement ("update `cloud`.`vlan` set vlan_id=? where id=?" );)
216+ {
217+ updatestatement .setString (1 , vlanUri );
218+ updatestatement .setLong (2 , id );
219+ updatestatement .executeUpdate ();
220+ } catch (SQLException e ) {
221+ throw new CloudRuntimeException ("Unable to update vlan URI " + vlanUri + " for vlan record " + id , e );
222+ }
303223 }
304224 } catch (SQLException e ) {
305225 throw new CloudRuntimeException ("Unable to update vlan URIs " , e );
306- } finally {
307- try {
308- if (rs != null ) {
309- rs .close ();
310- }
311-
312- if (pstmt != null ) {
313- pstmt .close ();
314- }
315- } catch (SQLException e ) {
316- }
317226 }
318227 s_logger .debug ("Done updateing vlan URIs" );
319228 }
0 commit comments