DB: db connection management 39/21739/1
authorgreatim <jaewon81.lim@samsung.com>
Tue, 27 May 2014 05:35:58 +0000 (14:35 +0900)
committergreatim <jaewon81.lim@samsung.com>
Tue, 27 May 2014 05:35:58 +0000 (14:35 +0900)
make sure db connection returned whatever db transaction finished successfully or not

Change-Id: I2a05b63abdc61141482035b301da21b3806bae5d
Signed-off-by: greatim <jaewon81.lim@samsung.com>
org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/database/SqlConnectionManager.java

index 46f8a6e..3bbf117 100644 (file)
@@ -33,9 +33,9 @@ public class SqlConnectionManager {
                CONNECTED,
                TO_BE_CLOSED
        }
-       
+
        private static State state = State.NOT_CONNECTED;
-       
+
        public static boolean establishConnection(String dbFilePath) {
                boolean result = true;
                try {
@@ -153,7 +153,7 @@ public class SqlConnectionManager {
                                Connection conn;
                                while ((conn = queryConnections.poll()) != null) {
                                        // if it is last connection of database
-                                       if(queryConnections.size() == 0) {
+                                       if (queryConnections.size() == 0) {
                                                Statement st = null;
                                                try {
                                                        st = conn.createStatement();
@@ -166,7 +166,7 @@ public class SqlConnectionManager {
                                                        CommonUtil.tryClose(st);
                                                }
                                        }
-                                       
+
                                        try {
                                                conn.close();
                                        } catch (SQLException e) {
@@ -177,7 +177,7 @@ public class SqlConnectionManager {
 
                        updateConnections = null;
                        queryConnections = null;
-                       
+
                        state = State.NOT_CONNECTED;
                }
        }
@@ -242,12 +242,13 @@ public class SqlConnectionManager {
                        if (conn != null) {
                                sm = conn.createStatement();
                                sm.executeUpdate(statement);
-                               putUpdateConnection(conn);
                        }
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        CommonUtil.tryClose(sm);
+                       if (conn != null)
+                               putUpdateConnection(conn);
                }
        }
 
@@ -370,13 +371,13 @@ public class SqlConnectionManager {
                                        prep.executeBatch();
                                conn.commit();
                                conn.setAutoCommit(true);
-                               
-                               putUpdateConnection(conn);
                        }
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        CommonUtil.tryClose(prep);
+                       if (conn != null)
+                               putUpdateConnection(conn);
                }
        }
 
@@ -433,13 +434,13 @@ public class SqlConnectionManager {
                                        prep.executeBatch();
                                conn.commit();
                                conn.setAutoCommit(true);
-                               
-                               putUpdateConnection(conn);
                        }
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        CommonUtil.tryClose(prep);
+                       if (conn != null)
+                               putUpdateConnection(conn);
                }
        }
 
@@ -448,7 +449,7 @@ public class SqlConnectionManager {
                Statement sm = null;
                ResultSet rs = null;
                List<List<Object>> result = null;
-               
+
                try {
                        conn = getQueryConnection();
                        if (conn != null) {
@@ -488,13 +489,13 @@ public class SqlConnectionManager {
                                                result.add(rowData);
                                        }
                                }
-                               
-                               putQueryConnection(conn);
                        }
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        CommonUtil.tryClose(sm);
+                       if (conn != null)
+                               putQueryConnection(conn);
                }
 
                return result;