Skip to content
Snippets Groups Projects
Commit 4228229f authored by Nikolaus Krismer's avatar Nikolaus Krismer
Browse files

reworked initConnection (and getConnection methods)

parent 9b4ec141
No related branches found
No related tags found
No related merge requests found
...@@ -632,32 +632,43 @@ public class Database { ...@@ -632,32 +632,43 @@ public class Database {
// FIXME: Find a fix for prepared statements with parameters // FIXME: Find a fix for prepared statements with parameters
// (we should NOT set strings in preparedStatemens just because we want to cache something) // (we should NOT set strings in preparedStatemens just because we want to cache something)
protected PreparedStatement getPstmt(final String query) { protected PreparedStatement getPstmt(final String query) {
initConnection();
if (pstmtsCacheMap == null) { if (pstmtsCacheMap == null) {
pstmtsCacheMap = new HashMap<>(); pstmtsCacheMap = new HashMap<>();
} }
PreparedStatement pstmt = pstmtsCacheMap.get(query);
try { try {
if (pstmtsCacheMap.containsKey(query) && !pstmtsCacheMap.get(query).isClosed()) { if (pstmt != null && !pstmt.isClosed()) {
return pstmtsCacheMap.get(query); return pstmt;
} }
final PreparedStatement pstmt = connection.prepareStatement(query); pstmt = getConnection().prepareStatement(query);
pstmtsCacheMap.put(query, pstmt);
return pstmt;
} catch (final SQLException e) { } catch (final SQLException e) {
e.printStackTrace(); e.printStackTrace();
pstmt = null;
} }
return null; if (pstmt != null) {
} pstmtsCacheMap.put(query, pstmt);
}
protected void initConnection() { return pstmt;
connection = ConfigIsochrone.getInstance().getConnection();
} }
protected Connection getConnection() { 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; return connection;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment