Skip to content

Commit 350ac4c

Browse files
committed
Fixed Resource Leaks
Signed-off-by: Santhosh Edukulla <santhosh.edukulla@gmail.com>
1 parent 0f528df commit 350ac4c

8 files changed

Lines changed: 132 additions & 133 deletions

File tree

engine/schema/src/com/cloud/upgrade/dao/Upgrade441to450.java

Lines changed: 87 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -82,126 +82,131 @@ public File[] getCleanupScripts() {
8282
}
8383

8484
private void updateSystemVmTemplates(Connection conn) {
85-
PreparedStatement pstmt = null;
86-
ResultSet rs = null;
8785
s_logger.debug("Updating System Vm template IDs");
88-
try{
86+
try {
8987
//Get all hypervisors in use
9088
Set<Hypervisor.HypervisorType> hypervisorsListInUse = new HashSet<Hypervisor.HypervisorType>();
91-
try {
92-
pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null");
93-
rs = pstmt.executeQuery();
94-
while(rs.next()){
95-
switch (Hypervisor.HypervisorType.getType(rs.getString(1))) {
96-
case XenServer: hypervisorsListInUse.add(Hypervisor.HypervisorType.XenServer);
97-
break;
98-
case KVM: hypervisorsListInUse.add(Hypervisor.HypervisorType.KVM);
99-
break;
100-
case VMware: hypervisorsListInUse.add(Hypervisor.HypervisorType.VMware);
101-
break;
102-
case Hyperv: hypervisorsListInUse.add(Hypervisor.HypervisorType.Hyperv);
103-
break;
104-
case LXC: hypervisorsListInUse.add(Hypervisor.HypervisorType.LXC);
105-
break;
89+
try (PreparedStatement sel_hyp_type_pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null");) {
90+
try (ResultSet hyp_type_rs = sel_hyp_type_pstmt.executeQuery();) {
91+
while (hyp_type_rs.next()) {
92+
switch (Hypervisor.HypervisorType.getType(hyp_type_rs.getString(1))) {
93+
case XenServer:
94+
hypervisorsListInUse.add(Hypervisor.HypervisorType.XenServer);
95+
break;
96+
case KVM:
97+
hypervisorsListInUse.add(Hypervisor.HypervisorType.KVM);
98+
break;
99+
case VMware:
100+
hypervisorsListInUse.add(Hypervisor.HypervisorType.VMware);
101+
break;
102+
case Hyperv:
103+
hypervisorsListInUse.add(Hypervisor.HypervisorType.Hyperv);
104+
break;
105+
case LXC:
106+
hypervisorsListInUse.add(Hypervisor.HypervisorType.LXC);
107+
break;
108+
}
106109
}
110+
} catch (SQLException e) {
111+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception:" + e.getMessage(), e);
107112
}
108113
} catch (SQLException e) {
109-
throw new CloudRuntimeException("Error while listing hypervisors in use", e);
114+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception:" + e.getMessage(), e);
110115
}
111-
112-
Map<Hypervisor.HypervisorType, String> NewTemplateNameList = new HashMap<Hypervisor.HypervisorType, String>(){
113-
{ put(Hypervisor.HypervisorType.XenServer, "systemvm-xenserver-4.5");
116+
Map<Hypervisor.HypervisorType, String> NewTemplateNameList = new HashMap<Hypervisor.HypervisorType, String>() {
117+
{
118+
put(Hypervisor.HypervisorType.XenServer, "systemvm-xenserver-4.5");
114119
put(Hypervisor.HypervisorType.VMware, "systemvm-vmware-4.5");
115120
put(Hypervisor.HypervisorType.KVM, "systemvm-kvm-4.5");
116121
put(Hypervisor.HypervisorType.LXC, "systemvm-lxc-4.5");
117122
put(Hypervisor.HypervisorType.Hyperv, "systemvm-hyperv-4.5");
118123
}
119124
};
120-
121-
Map<Hypervisor.HypervisorType, String> routerTemplateConfigurationNames = new HashMap<Hypervisor.HypervisorType, String>(){
122-
{ put(Hypervisor.HypervisorType.XenServer, "router.template.xen");
125+
Map<Hypervisor.HypervisorType, String> routerTemplateConfigurationNames = new HashMap<Hypervisor.HypervisorType, String>() {
126+
{
127+
put(Hypervisor.HypervisorType.XenServer, "router.template.xen");
123128
put(Hypervisor.HypervisorType.VMware, "router.template.vmware");
124129
put(Hypervisor.HypervisorType.KVM, "router.template.kvm");
125130
put(Hypervisor.HypervisorType.LXC, "router.template.lxc");
126131
put(Hypervisor.HypervisorType.Hyperv, "router.template.hyperv");
127132
}
128133
};
129-
130-
Map<Hypervisor.HypervisorType, String> newTemplateUrl = new HashMap<Hypervisor.HypervisorType, String>(){
131-
{ put(Hypervisor.HypervisorType.XenServer, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-xen.vhd.bz2");
134+
Map<Hypervisor.HypervisorType, String> newTemplateUrl = new HashMap<Hypervisor.HypervisorType, String>() {
135+
{
136+
put(Hypervisor.HypervisorType.XenServer, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-xen.vhd.bz2");
132137
put(Hypervisor.HypervisorType.VMware, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-vmware.ova");
133138
put(Hypervisor.HypervisorType.KVM, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2");
134139
put(Hypervisor.HypervisorType.LXC, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2");
135140
put(Hypervisor.HypervisorType.Hyperv, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-hyperv.vhd.zip");
136141
}
137142
};
138-
139-
Map<Hypervisor.HypervisorType, String> newTemplateChecksum = new HashMap<Hypervisor.HypervisorType, String>(){
140-
{ put(Hypervisor.HypervisorType.XenServer, "2b15ab4401c2d655264732d3fc600241");
143+
Map<Hypervisor.HypervisorType, String> newTemplateChecksum = new HashMap<Hypervisor.HypervisorType, String>() {
144+
{
145+
put(Hypervisor.HypervisorType.XenServer, "2b15ab4401c2d655264732d3fc600241");
141146
put(Hypervisor.HypervisorType.VMware, "3106a79a4ce66cd7f6a7c50e93f2db57");
142147
put(Hypervisor.HypervisorType.KVM, "aa9f501fecd3de1daeb9e2f357f6f002");
143148
put(Hypervisor.HypervisorType.LXC, "aa9f501fecd3de1daeb9e2f357f6f002");
144149
put(Hypervisor.HypervisorType.Hyperv, "70bd30ea02ee9ed67d2c6b85c179cee9");
145150
}
146151
};
147-
148-
for (Map.Entry<Hypervisor.HypervisorType, String> hypervisorAndTemplateName : NewTemplateNameList.entrySet()){
152+
for (Map.Entry<Hypervisor.HypervisorType, String> hypervisorAndTemplateName : NewTemplateNameList.entrySet()) {
149153
s_logger.debug("Updating " + hypervisorAndTemplateName.getKey() + " System Vms");
150-
try {
154+
try (PreparedStatement sel_templ_pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1");)
155+
{
151156
//Get 4.5.0 system Vm template Id for corresponding hypervisor
152-
pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1");
153-
pstmt.setString(1, hypervisorAndTemplateName.getValue());
154-
rs = pstmt.executeQuery();
155-
if(rs.next()){
156-
long templateId = rs.getLong(1);
157-
rs.close();
158-
pstmt.close();
159-
pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");
160-
pstmt.setLong(1, templateId);
161-
pstmt.executeUpdate();
162-
pstmt.close();
163-
// update templete ID of system Vms
164-
pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?");
165-
pstmt.setLong(1, templateId);
166-
pstmt.setString(2, hypervisorAndTemplateName.getKey().toString());
167-
pstmt.executeUpdate();
168-
pstmt.close();
169-
// Change value of global configuration parameter router.template.* for the corresponding hypervisor
170-
pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?");
171-
pstmt.setString(1, hypervisorAndTemplateName.getValue());
172-
pstmt.setString(2, routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()));
173-
pstmt.executeUpdate();
174-
pstmt.close();
175-
} else {
176-
if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())){
177-
throw new CloudRuntimeException("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms");
157+
sel_templ_pstmt.setString(1, hypervisorAndTemplateName.getValue());
158+
try (ResultSet rs = sel_templ_pstmt.executeQuery();)
159+
{
160+
if (rs.next()) {
161+
long templateId = rs.getLong(1);
162+
try(PreparedStatement update_cloud_templ_pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");)
163+
{
164+
update_cloud_templ_pstmt.setLong(1, templateId);
165+
update_cloud_templ_pstmt.executeUpdate();
166+
// update templete ID of system Vms
167+
}catch (SQLException e) {
168+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception:" + e.getMessage(), e);
169+
}
170+
try(PreparedStatement update_instance_pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?");) {
171+
update_instance_pstmt.setLong(1, templateId);
172+
update_instance_pstmt.setString(2, hypervisorAndTemplateName.getKey().toString());
173+
update_instance_pstmt.executeUpdate();
174+
}catch (SQLException e) {
175+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception:" + e.getMessage(), e);
176+
}
177+
// Change value of global configuration parameter router.template.* for the corresponding hypervisor
178+
try(PreparedStatement update_cloud_pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?");)
179+
{
180+
update_cloud_pstmt.setString(1, hypervisorAndTemplateName.getValue());
181+
update_cloud_pstmt.setString(2, routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()));
182+
update_cloud_pstmt.executeUpdate();
183+
}catch (SQLException e) {
184+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception:" + e.getMessage(), e);
185+
}
178186
} else {
179-
s_logger.warn("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() + " hypervisor is not used, so not failing upgrade");
180-
// Update the latest template URLs for corresponding hypervisor
181-
pstmt = conn.prepareStatement("UPDATE `cloud`.`vm_template` SET url = ? , checksum = ? WHERE hypervisor_type = ? AND type = 'SYSTEM' AND removed is null order by id desc limit 1");
182-
pstmt.setString(1, newTemplateUrl.get(hypervisorAndTemplateName.getKey()));
183-
pstmt.setString(2, newTemplateChecksum.get(hypervisorAndTemplateName.getKey()));
184-
pstmt.setString(3, hypervisorAndTemplateName.getKey().toString());
185-
pstmt.executeUpdate();
186-
pstmt.close();
187+
if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())) {
188+
throw new CloudRuntimeException("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms");
189+
} else {
190+
s_logger.warn("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() + " hypervisor is not used, so not failing upgrade");
191+
// Update the latest template URLs for corresponding hypervisor
192+
try (PreparedStatement update_pstmt = conn.prepareStatement("UPDATE `cloud`.`vm_template` SET url = ? , checksum = ? WHERE hypervisor_type = ? AND type = 'SYSTEM' AND removed is null order by id desc limit 1");) {
193+
update_pstmt.setString(1, newTemplateUrl.get(hypervisorAndTemplateName.getKey()));
194+
update_pstmt.setString(2, newTemplateChecksum.get(hypervisorAndTemplateName.getKey()));
195+
update_pstmt.setString(3, hypervisorAndTemplateName.getKey().toString());
196+
update_pstmt.executeUpdate();
197+
} catch (SQLException e) {
198+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception:" + e.getMessage(), e);
199+
}
200+
}
187201
}
202+
} catch (SQLException e) {
203+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception:" + e.getMessage(), e);
188204
}
189-
} catch (SQLException e) {
190-
throw new CloudRuntimeException("Error while updating "+ hypervisorAndTemplateName.getKey() +" systemVm template", e);
191-
}
192-
}
193-
s_logger.debug("Updating System Vm Template IDs Complete");
194-
} finally {
195-
try {
196-
if (rs != null) {
197-
rs.close();
198205
}
199-
200-
if (pstmt != null) {
201-
pstmt.close();
202-
}
203-
} catch (SQLException e) {
206+
s_logger.debug("Updating System Vm Template IDs Complete");
204207
}
208+
}catch(SQLException e){
209+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception:" + e.getMessage(), e);
205210
}
206211
}
207212

framework/db/test/com/cloud/utils/db/DbTestUtils.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,16 @@ public static void executeScript(String file, boolean autoCommit, boolean stopOn
3232
if (cleanScript == null) {
3333
throw new RuntimeException("Unable to clean the database because I can't find " + file);
3434
}
35-
3635
Connection conn = TransactionLegacy.getStandaloneConnection();
37-
3836
ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError);
39-
FileReader reader;
40-
try {
41-
reader = new FileReader(cleanScript);
37+
try(FileReader reader = new FileReader(cleanScript);)
38+
{
39+
runner.runScript(reader);
40+
conn.close();
4241
} catch (FileNotFoundException e) {
4342
throw new RuntimeException("Unable to read " + file, e);
44-
}
45-
try {
46-
runner.runScript(reader);
47-
} catch (IOException e) {
43+
}catch (IOException e) {
4844
throw new RuntimeException("Unable to read " + file, e);
49-
} catch (SQLException e) {
50-
throw new RuntimeException("Unable to execute " + file, e);
51-
}
52-
53-
try {
54-
conn.close();
5545
} catch (SQLException e) {
5646
throw new RuntimeException("Unable to close DB connection", e);
5747
}
@@ -62,24 +52,15 @@ public static void executeUsageScript(String file, boolean autoCommit, boolean s
6252
if (cleanScript == null) {
6353
throw new RuntimeException("Unable to clean the database because I can't find " + file);
6454
}
65-
6655
Connection conn = TransactionLegacy.getStandaloneUsageConnection();
67-
6856
ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError);
69-
FileReader reader;
70-
try {
71-
reader = new FileReader(cleanScript);
72-
} catch (FileNotFoundException e) {
73-
throw new RuntimeException("Unable to read " + file, e);
74-
}
75-
try {
57+
try(FileReader reader = new FileReader(cleanScript);) {
7658
runner.runScript(reader);
77-
} catch (IOException e) {
78-
throw new RuntimeException("Unable to read " + file, e);
79-
} catch (SQLException e) {
80-
throw new RuntimeException("Unable to execute " + file, e);
59+
} catch (IOException e){
60+
throw new RuntimeException("executeUsageScript:Exception:"+e.getMessage(),e);
61+
}catch (SQLException e){
62+
throw new RuntimeException("executeUsageScript:Exception:"+e.getMessage(),e);
8163
}
82-
8364
try {
8465
conn.close();
8566
} catch (SQLException e) {

plugins/storage/volume/nexenta/src/org/apache/cloudstack/storage/datastore/util/NexentaNmsClient.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,13 @@ public NmsResponse execute(Class responseClass, String object, String method, Ob
197197
if (!isSuccess(status)) {
198198
throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + status);
199199
}
200-
BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
201-
String tmp;
202-
while ((tmp = buffer.readLine()) != null) {
203-
sb.append(tmp);
200+
try(BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));) {
201+
String tmp;
202+
while ((tmp = buffer.readLine()) != null) {
203+
sb.append(tmp);
204+
}
205+
}catch (IOException ex) {
206+
throw new CloudRuntimeException(ex.getMessage());
204207
}
205208
} catch (ClientProtocolException ex) {
206209
throw new CloudRuntimeException(ex.getMessage());

plugins/storage/volume/solidfire/src/org/apache/cloudstack/storage/datastore/util/SolidFireUtil.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,12 +1667,13 @@ private static String executeJsonRpc(SolidFireConnection sfConnection, String st
16671667
throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + response.getStatusLine().getStatusCode());
16681668
}
16691669

1670-
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
1671-
1672-
String strOutput;
1673-
1674-
while ((strOutput = br.readLine()) != null) {
1675-
sb.append(strOutput);
1670+
try(BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));) {
1671+
String strOutput;
1672+
while ((strOutput = br.readLine()) != null) {
1673+
sb.append(strOutput);
1674+
}
1675+
}catch (IOException ex) {
1676+
throw new CloudRuntimeException(ex.getMessage());
16761677
}
16771678
} catch (UnsupportedEncodingException ex) {
16781679
throw new CloudRuntimeException(ex.getMessage());

0 commit comments

Comments
 (0)