summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-11 15:39:04 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-11 15:39:04 +0200
commitb848cb5d3bbe11b69462b2bb8d8a0bd331c902ff (patch)
tree400f0176843e048f99db01cbc0ccf968794c1513
parentcffee33f9fcad20cf9e41f6a847841f1c1390af2 (diff)
downloadDatafiller-b848cb5d3bbe11b69462b2bb8d8a0bd331c902ff.tar.gz
Wrap queries in a transaction with rollback
If a tweet cannot be processed, then it must be faulty. Do not fill the database with useless information.
-rw-r--r--src/main/DataFiller.java14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/main/DataFiller.java b/src/main/DataFiller.java
index 803d926..71f1822 100644
--- a/src/main/DataFiller.java
+++ b/src/main/DataFiller.java
@@ -44,6 +44,8 @@ public class DataFiller {
m_insertTweetUrl = new NamedPreparedStatement(m_connection, QueryUtils.insertTweetUrl);
m_insertUserUrl = new NamedPreparedStatement(m_connection, QueryUtils.insertUserUrl);
m_insertMentions = new NamedPreparedStatement(m_connection, QueryUtils.insertMentions);
+ // enable transactions
+ connection.setAutoCommit(false);
}
/**
@@ -68,6 +70,18 @@ public class DataFiller {
processTweet(tweet.retweeted_status);
}
+ try {
+ // assume that no previous transaction was started.
+ saveTweet(tweet);
+ // concludes the previous query, implicitly starts a new transaction
+ m_connection.commit();
+ } catch (Exception ex) {
+ m_connection.rollback();
+ throw ex;
+ }
+ }
+
+ private void saveTweet(Tweet tweet) throws SQLException {
String text = sanitizeTweetText(tweet.text);
// ensure that the user and tweet are known before adding relations
QueryUtils.setInsertParams(m_insertTweet, m_insertProfile, tweet, text);