aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2012-10-23 10:31:29 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2012-10-23 10:31:29 +1300
commitf151afb7046c79184bbb9dcf1215a99731728599 (patch)
treeae5ee7fad2a3d23de6d94b6f0a0a4d699376117a
parent0af1cebf1e2f3f719edf5391252fcd4004a75157 (diff)
downloadtraccar-server-f151afb7046c79184bbb9dcf1215a99731728599.tar.gz
traccar-server-f151afb7046c79184bbb9dcf1215a99731728599.tar.bz2
traccar-server-f151afb7046c79184bbb9dcf1215a99731728599.zip
Reset connection on error (fix #52)
-rw-r--r--src/org/traccar/TrackerEventHandler.java1
-rw-r--r--src/org/traccar/helper/AdvancedConnection.java4
-rw-r--r--src/org/traccar/helper/NamedParameterStatement.java23
3 files changed, 25 insertions, 3 deletions
diff --git a/src/org/traccar/TrackerEventHandler.java b/src/org/traccar/TrackerEventHandler.java
index 9e46372ca..bd3e90160 100644
--- a/src/org/traccar/TrackerEventHandler.java
+++ b/src/org/traccar/TrackerEventHandler.java
@@ -63,6 +63,7 @@ public class TrackerEventHandler extends SimpleChannelHandler {
try {
dataManager.addPosition(position);
} catch (Exception error) {
+ Log.info("Exception during query execution");
Log.warning(error.getMessage());
}
}
diff --git a/src/org/traccar/helper/AdvancedConnection.java b/src/org/traccar/helper/AdvancedConnection.java
index 30be4dba8..c0126974b 100644
--- a/src/org/traccar/helper/AdvancedConnection.java
+++ b/src/org/traccar/helper/AdvancedConnection.java
@@ -40,6 +40,10 @@ public class AdvancedConnection {
}
public final void reset() throws SQLException {
+ if (connection != null) {
+ connection.close();
+ connection = null;
+ }
if (user != null && password != null) {
connection = DriverManager.getConnection(url, user, password);
} else {
diff --git a/src/org/traccar/helper/NamedParameterStatement.java b/src/org/traccar/helper/NamedParameterStatement.java
index 0dfbb9551..cbbc187da 100644
--- a/src/org/traccar/helper/NamedParameterStatement.java
+++ b/src/org/traccar/helper/NamedParameterStatement.java
@@ -40,6 +40,8 @@ public class NamedParameterStatement {
* Database connection
*/
private AdvancedConnection connection;
+
+ private int autoGeneratedKeys;
/**
* Initialize statement
@@ -106,15 +108,30 @@ public class NamedParameterStatement {
return parsedQuery.toString();
}
+
+ public void reset(boolean resetConnection) throws SQLException {
+ if (statement != null) {
+ statement.close();
+ statement = null;
+ }
+ if (resetConnection) {
+ connection.reset();
+ }
+ statement = connection.getInstance().prepareStatement(parsedQuery, autoGeneratedKeys);
+ }
public void prepare(int autoGeneratedKeys) throws SQLException {
+ this.autoGeneratedKeys = autoGeneratedKeys;
try {
if (statement == null) {
- statement = connection.getInstance().prepareStatement(parsedQuery, autoGeneratedKeys);
+ reset(false);
+ } else if (statement.getWarnings() != null) {
+ reset(true);
}
} catch (SQLException error) {
- connection.reset();
- statement = connection.getInstance().prepareStatement(parsedQuery, autoGeneratedKeys);
+ Log.info("Exception during query preparation");
+ Log.warning(error.getMessage());
+ reset(true);
}
}