Fix resource leaks in test cases
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / jdbc / JDBCConnection.java
index bf1244f2e3cd90787984105d5395cb7df3c9e853..a6b5ca60638a803432839425706337e1c4726c56 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.controller.md.sal.dom.xsql.jdbc;
 
 import java.io.BufferedInputStream;
@@ -73,8 +81,9 @@ public class JDBCConnection implements Connection, Runnable {
     }
 
     private void init() throws Exception {
-        if (addr.startsWith("http://"))
+        if (addr.startsWith("http://")) {
             addr = addr.substring(7);
+        }
         System.err.print("Address is:" + addr);
         socket = new Socket(addr, 40004);
         try {
@@ -91,17 +100,16 @@ public class JDBCConnection implements Connection, Runnable {
 
     public JDBCConnection(boolean server) {
         try {
-            ServerSocket s = new ServerSocket(50003);
-            socket = s.accept();
-            try {
-                in = new DataInputStream(new BufferedInputStream(
-                        socket.getInputStream()));
-                out = new DataOutputStream(new BufferedOutputStream(
-                        socket.getOutputStream()));
-                new JDBCObjectReader();
-                new Thread(this).start();
-            } catch (Exception err) {
-                err.printStackTrace();
+            try (ServerSocket s = new ServerSocket(50003)) {
+                socket = s.accept();
+                try {
+                    in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
+                    out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
+                    new JDBCObjectReader();
+                    new Thread(this).start();
+                } catch (Exception err) {
+                    err.printStackTrace();
+                }
             }
         } catch (Exception err) {
             err.printStackTrace();