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 9d0e6586683ec9e06fcd79721292c1cd63325c94..eb976e8f7cabbf6613cc00924984e29aa2069f3f 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;
 	}