@@ -73,47 +73,44 @@ private void updateNetworkDefaultOfferingsForVPCWithFirewallService(Connection c
7373
7474 try {
7575 for (String uniqueName : defaultVpcOfferingUniqueNames ) {
76- PreparedStatement pstmt = conn .prepareStatement ("SELECT id FROM `cloud`.`network_offerings` WHERE unique_name = ?" );
77- pstmt .setString (1 , uniqueName );
78-
79- ResultSet rs = pstmt . executeQuery ();
80- if (! rs . next ()) {
81- continue ;
82- }
83-
84- long offeringId = rs . getLong ( 1 );
85- rs . close ();
86- pstmt . close ();
87-
88- // Insert into ntwk_offering_service_map
89- pstmt = conn . prepareStatement ( "INSERT IGNORE INTO `cloud`.`ntwk_offering_service_map` " +
90- "(network_offering_id, service, provider, created) " +
91- "VALUES (?, 'Firewall', 'VpcVirtualRouter', now())" );
92- pstmt . setLong ( 1 , offeringId );
93- pstmt . executeUpdate ();
94- pstmt . close ( );
95-
96- // Update existing networks (ntwk_service_map)
97- pstmt = conn . prepareStatement ( "SELECT id FROM `cloud`.`networks` WHERE network_offering_id = ?" );
98- pstmt . setLong ( 1 , offeringId );
99-
100- rs = pstmt . executeQuery ();
101- while ( rs . next ( )) {
102- long networkId = rs . getLong ( 1 );
103- PreparedStatement insertService = conn . prepareStatement ( "INSERT INGORE INTO `cloud`.`ntwk_service_map` " +
104- "(network_id, service, provider, created) " +
105- "VALUES (?, 'Firewall', 'VpcVirtualRouter', now())" );
106- insertService . setLong ( 1 , networkId );
107- insertService . executeUpdate ();
108- insertService . close ();
76+ try ( PreparedStatement pstmt = conn .prepareStatement ("SELECT id FROM `cloud`.`network_offerings` WHERE unique_name = ?" )) {
77+ pstmt .setString (1 , uniqueName );
78+ try ( ResultSet rs = pstmt . executeQuery ()) {
79+ if (! rs . next ()) {
80+ continue ;
81+ }
82+ long offeringId = rs . getLong ( 1 );
83+ // Insert into ntwk_offering_service_map
84+ try ( PreparedStatement insertOfferingPstmt = conn . prepareStatement (
85+ "INSERT IGNORE INTO `cloud`.`ntwk_offering_service_map` " +
86+ "(network_offering_id, service, provider, created) " +
87+ "VALUES (?, 'Firewall', 'VpcVirtualRouter', now())" )) {
88+ insertOfferingPstmt . setLong ( 1 , offeringId );
89+ insertOfferingPstmt . executeUpdate ();
90+ }
91+ // Update existing networks (ntwk_service_map)
92+ try ( PreparedStatement selectNetworksPstmt = conn . prepareStatement (
93+ "SELECT id FROM `cloud`.`networks` WHERE network_offering_id = ?" )) {
94+ selectNetworksPstmt . setLong ( 1 , offeringId );
95+ try ( ResultSet networksRs = selectNetworksPstmt . executeQuery ()) {
96+ while ( networksRs . next ()) {
97+ long networkId = networksRs . getLong ( 1 );
98+ try ( PreparedStatement insertService = conn . prepareStatement (
99+ "INSERT INGORE INTO `cloud`.`ntwk_service_map` " +
100+ "(network_id, service, provider, created) " +
101+ "VALUES (?, 'Firewall', 'VpcVirtualRouter', now())" )) {
102+ insertService . setLong ( 1 , networkId );
103+ insertService . executeUpdate ();
104+ }
105+ }
106+ }
107+ }
108+ }
109109 }
110-
111- rs .close ();
112- pstmt .close ();
113110 }
114111
115112 } catch (SQLException e ) {
116- logger . warn ("Exception while updating VPC default offerings with Firewall service: " + e .getMessage (), e );
113+ throw new CloudRuntimeException ("Exception while updating VPC default offerings with Firewall service: " + e .getMessage (), e );
117114 }
118115 }
119116
@@ -132,47 +129,47 @@ private void updateVpcOfferingsWithFirewallService(Connection conn) {
132129
133130 try {
134131 for (String uniqueName : vpcOfferingUniqueNames ) {
135- PreparedStatement pstmt = conn .prepareStatement ("SELECT id FROM `cloud`.`vpc_offerings` WHERE unique_name = ?" );
136- pstmt .setString (1 , uniqueName );
137132
138- ResultSet rs = pstmt .executeQuery ();
139- if (!rs .next ()) {
140- continue ;
133+ try (PreparedStatement pstmt = conn .prepareStatement ("SELECT id FROM `cloud`.`vpc_offerings` WHERE unique_name = ?" )) {
134+ pstmt .setString (1 , uniqueName );
135+ try (ResultSet rs = pstmt .executeQuery ()) {
136+ if (!rs .next ()) {
137+ continue ;
138+ }
139+
140+ long vpcOfferingId = rs .getLong (1 );
141+ // Insert into vpc_offering_service_map
142+ try (PreparedStatement insertOfferingPstmt = conn .prepareStatement (
143+ "INSERT IGNORE INTO `cloud`.`vpc_offering_service_map` " +
144+ "(vpc_offering_id, service, provider, created) " +
145+ "VALUES (?, 'Firewall', 'VpcVirtualRouter', now())" )) {
146+
147+ insertOfferingPstmt .setLong (1 , vpcOfferingId );
148+ insertOfferingPstmt .executeUpdate ();
149+ }
150+
151+ // Update existing VPCs (vpc_service_map)
152+ try (PreparedStatement selectVpcsPstmt = conn .prepareStatement ("SELECT id FROM `cloud`.`vpcs` WHERE vpc_offering_id = ?" )) {
153+ selectVpcsPstmt .setLong (1 , vpcOfferingId );
154+ try (ResultSet vpcsRs = selectVpcsPstmt .executeQuery ()) {
155+ while (vpcsRs .next ()) {
156+ long vpcId = vpcsRs .getLong (1 );
157+ try (PreparedStatement insertService = conn .prepareStatement (
158+ "INSERT IGNORE INTO `cloud`.`vpc_service_map` " +
159+ "(vpc_id, service, provider, created) " +
160+ "VALUES (?, 'Firewall', 'VpcVirtualRouter', now())" )) {
161+ insertService .setLong (1 , vpcId );
162+ insertService .executeUpdate ();
163+ }
164+ }
165+ }
166+ }
167+ }
141168 }
142-
143- long vpcOfferingId = rs .getLong (1 );
144- rs .close ();
145- pstmt .close ();
146-
147- // Insert into vpc_offering_service_map
148- pstmt = conn .prepareStatement ("INSERT IGNORE INTO `cloud`.`vpc_offering_service_map` " +
149- "(vpc_offering_id, service, provider, created) " +
150- "VALUES (?, 'Firewall', 'VpcVirtualRouter', now())" );
151- pstmt .setLong (1 , vpcOfferingId );
152- pstmt .executeUpdate ();
153- pstmt .close ();
154-
155- // Update existing VPCs
156- pstmt = conn .prepareStatement ("SELECT id FROM `cloud`.`vpcs` WHERE vpc_offering_id = ?" );
157- pstmt .setLong (1 , vpcOfferingId );
158-
159- rs = pstmt .executeQuery ();
160- while (rs .next ()) {
161- long vpcId = rs .getLong (1 );
162- PreparedStatement insertService = conn .prepareStatement ("INSERT IGNORE INTO `cloud`.`vpc_service_map` " +
163- "(vpc_id, service, provider, created) " +
164- "VALUES (?, 'Firewall', 'VpcVirtualRouter', now())" );
165- insertService .setLong (1 , vpcId );
166- insertService .executeUpdate ();
167- insertService .close ();
168- }
169-
170- rs .close ();
171- pstmt .close ();
172169 }
173170
174171 } catch (SQLException e ) {
175- logger . warn ("Exception while updating VPC offerings with Firewall service: " + e .getMessage (), e );
172+ throw new CloudRuntimeException ("Exception while updating VPC offerings with Firewall service: " + e .getMessage (), e );
176173 }
177174 }
178175}
0 commit comments