From 8214596eb5948e6e55de498eaedb67a1a0049481 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 22 Apr 2014 21:55:26 +1200 Subject: Reconnect to database on errors (fix #675) --- .../traccar/helper/NamedParameterStatement.java | 25 ++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/helper/NamedParameterStatement.java b/src/org/traccar/helper/NamedParameterStatement.java index 833f4aea3..f66d8c4dd 100644 --- a/src/org/traccar/helper/NamedParameterStatement.java +++ b/src/org/traccar/helper/NamedParameterStatement.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2014 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,9 +39,11 @@ public class NamedParameterStatement { /** * Database connection */ - private AdvancedConnection connection; + private final AdvancedConnection connection; private int autoGeneratedKeys; + + private boolean failed; /** * Initialize statement @@ -118,6 +120,7 @@ public class NamedParameterStatement { connection.reset(); } statement = connection.getInstance().prepareStatement(parsedQuery, autoGeneratedKeys); + failed = false; } public void prepare(int autoGeneratedKeys) throws SQLException { @@ -125,7 +128,7 @@ public class NamedParameterStatement { try { if (statement == null) { reset(false); - } else if (statement.getWarnings() != null) { + } else if (failed || statement.getWarnings() != null) { reset(true); } } catch (SQLException firstError) { @@ -133,6 +136,7 @@ public class NamedParameterStatement { reset(true); } catch (SQLException secondError) { Log.warning(secondError); + failed = true; throw secondError; } } @@ -146,15 +150,24 @@ public class NamedParameterStatement { * Execute query with result */ public ResultSet executeQuery() throws SQLException { - return statement.executeQuery(); + try { + return statement.executeQuery(); + } catch (SQLException error) { + failed = true; + throw error; + } } - /** * Executes query without result */ public int executeUpdate() throws SQLException { - return statement.executeUpdate(); + try { + return statement.executeUpdate(); + } catch (SQLException error) { + failed = true; + throw error; + } } /** -- cgit v1.2.3