You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eagle.apache.org by mw...@apache.org on 2016/07/25 09:36:49 UTC
[12/47] incubator-eagle git commit: EAGLE-291 : JDBC: Update
transactions fail in PostgreSQL
EAGLE-291 : JDBC: Update transactions fail in PostgreSQL
The transactions in Postgres was failing due to insert fail transaction already in progress and we were trying to update if insert fails. Need to rollback the failed insert transaction and then continue with update transaction. Postgres is more strict as compared to MySql, so we need save point so that we can roll it back, if required.
Author: Jaspaul <ja...@gmail.com>
Closes #194 from Jashchahal/dev.
Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/8dad5fe6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/8dad5fe6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/8dad5fe6
Branch: refs/heads/master
Commit: 8dad5fe6fd83b76674e4df6e6bd50e0b11f3bec0
Parents: 6768fe4
Author: Jaspaul <ja...@gmail.com>
Authored: Mon May 23 11:13:34 2016 +0800
Committer: Hao Chen <ha...@apache.org>
Committed: Mon May 23 11:13:34 2016 +0800
----------------------------------------------------------------------
.../jdbc/entity/impl/JdbcEntityWriterImpl.java | 21 ++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/8dad5fe6/eagle-core/eagle-query/eagle-storage-jdbc/src/main/java/org/apache/eagle/storage/jdbc/entity/impl/JdbcEntityWriterImpl.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-query/eagle-storage-jdbc/src/main/java/org/apache/eagle/storage/jdbc/entity/impl/JdbcEntityWriterImpl.java b/eagle-core/eagle-query/eagle-storage-jdbc/src/main/java/org/apache/eagle/storage/jdbc/entity/impl/JdbcEntityWriterImpl.java
index 5a04ae6..a72c83c 100644
--- a/eagle-core/eagle-query/eagle-storage-jdbc/src/main/java/org/apache/eagle/storage/jdbc/entity/impl/JdbcEntityWriterImpl.java
+++ b/eagle-core/eagle-query/eagle-storage-jdbc/src/main/java/org/apache/eagle/storage/jdbc/entity/impl/JdbcEntityWriterImpl.java
@@ -37,6 +37,7 @@ import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.sql.Savepoint;
/**
* @since 3/27/15
@@ -63,11 +64,13 @@ public class JdbcEntityWriterImpl<E extends TaggedLogAPIEntity> implements JdbcE
if(LOG.isDebugEnabled()) LOG.debug("Writing "+entities.size()+" entities");
StopWatch stopWatch = new StopWatch();
stopWatch.start();
- Connection connection = ConnectionManagerFactory.getInstance().getConnection();
- // set auto commit false and commit by hands for 3x~5x better performance
- connection.setAutoCommit(false);
-
+ Connection connection = null;
+ Savepoint insertDup = null;
+ int i = 0 ;
try {
+ connection = ConnectionManagerFactory.getInstance().getConnection();
+ // set auto commit false and commit by hands for 3x~5x better performance
+ connection.setAutoCommit(false);
TorqueStatementPeerImpl<E> peer = connectionManager.getStatementExecutor(this.jdbcEntityDefinition.getJdbcTableName());
for (E entity : entities) {
entity.setEncodedRowkey(peer.getPrimaryKeyBuilder().build(entity));
@@ -75,6 +78,9 @@ public class JdbcEntityWriterImpl<E extends TaggedLogAPIEntity> implements JdbcE
ObjectKey key = null;
try {
+ //save point , so that we can roll back just current entry, if required.
+ i++ ;
+ insertDup = connection.setSavepoint("insertEntity"+i);
// TODO: implement batch insert for better performance
key = peer.delegate().doInsert(columnValues,connection);
@@ -89,6 +95,7 @@ public class JdbcEntityWriterImpl<E extends TaggedLogAPIEntity> implements JdbcE
} catch (ConstraintViolationException e){
//this message will be different in each DB type ...using duplicate keyword to catch for broader set of DBs. moreover we are already inside ConstraintViolationException exception, do we even need this check?
if(e.getMessage().toLowerCase().contains("duplicate")){
+ connection.rollback(insertDup); // need to rollback current Insert entity, as it is duplicate record, need to update. Postgresql is strict in transaction handling(need rollback) as compared to MySql
String primaryKey = entity.getEncodedRowkey();
if(primaryKey==null) {
primaryKey = ConnectionManagerFactory.getInstance().getStatementExecutor().getPrimaryKeyBuilder().build(entity);
@@ -110,12 +117,14 @@ public class JdbcEntityWriterImpl<E extends TaggedLogAPIEntity> implements JdbcE
connection.commit();
}catch (Exception ex) {
LOG.error("Failed to write records, rolling back",ex);
- connection.rollback();
+ if(connection!=null)
+ connection.rollback();
throw ex;
}finally {
stopWatch.stop();
if(LOG.isDebugEnabled()) LOG.debug("Closing connection");
- connection.close();
+ if(connection!=null)
+ connection.close();
}
LOG.info(String.format("Wrote %s records in %s ms (table: %s)",keys.size(),stopWatch.getTime(),this.jdbcEntityDefinition.getJdbcTableName()));