From 4228229f30d5b380d048f5b48112e5a946ac5c99 Mon Sep 17 00:00:00 2001 From: Nikolaus Krismer <niko@krismer.de> Date: Tue, 13 May 2014 21:53:21 +0200 Subject: [PATCH] reworked initConnection (and getConnection methods) --- .../it/unibz/inf/isochrone/db/Database.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/it/unibz/inf/isochrone/db/Database.java b/src/main/java/it/unibz/inf/isochrone/db/Database.java index 9d0e6586..eb976e8f 100644 --- a/src/main/java/it/unibz/inf/isochrone/db/Database.java +++ b/src/main/java/it/unibz/inf/isochrone/db/Database.java @@ -632,32 +632,43 @@ public class Database { // FIXME: Find a fix for prepared statements with parameters // (we should NOT set strings in preparedStatemens just because we want to cache something) protected PreparedStatement getPstmt(final String query) { - initConnection(); if (pstmtsCacheMap == null) { pstmtsCacheMap = new HashMap<>(); } + PreparedStatement pstmt = pstmtsCacheMap.get(query); try { - if (pstmtsCacheMap.containsKey(query) && !pstmtsCacheMap.get(query).isClosed()) { - return pstmtsCacheMap.get(query); + if (pstmt != null && !pstmt.isClosed()) { + return pstmt; } - final PreparedStatement pstmt = connection.prepareStatement(query); - pstmtsCacheMap.put(query, pstmt); - return pstmt; + pstmt = getConnection().prepareStatement(query); } catch (final SQLException e) { e.printStackTrace(); + pstmt = null; } - return null; - } + if (pstmt != null) { + pstmtsCacheMap.put(query, pstmt); + } - protected void initConnection() { - connection = ConfigIsochrone.getInstance().getConnection(); + return pstmt; } protected Connection getConnection() { - initConnection(); + if (connection == null) { + connection = ConfigIsochrone.getInstance().getConnection(); + } else { + try { + if (connection.isClosed()) { + connection = ConfigIsochrone.getInstance().getConnection(); + } + } catch (SQLException e) { + e.printStackTrace(); + connection = null; + } + } + return connection; } -- GitLab