Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A515A200D2D for ; Fri, 27 Oct 2017 20:14:56 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A3D5E160BF2; Fri, 27 Oct 2017 18:14:56 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 031571609DD for ; Fri, 27 Oct 2017 20:14:55 +0200 (CEST) Received: (qmail 19868 invoked by uid 500); 27 Oct 2017 18:14:55 -0000 Mailing-List: contact commits-help@geode.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.apache.org Delivered-To: mailing list commits@geode.apache.org Received: (qmail 19858 invoked by uid 99); 27 Oct 2017 18:14:55 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 27 Oct 2017 18:14:55 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 3B46D81C95; Fri, 27 Oct 2017 18:14:52 +0000 (UTC) Date: Fri, 27 Oct 2017 18:14:52 +0000 To: "commits@geode.apache.org" Subject: [geode] branch feature/GEODE-3781 updated: Added prepared statement cache Debug logging added that should be cleaned up later MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <150912809206.1121.14630738181663845567@gitbox.apache.org> From: agingade@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: geode X-Git-Refname: refs/heads/feature/GEODE-3781 X-Git-Reftype: branch X-Git-Oldrev: 77f119d0271ffb5f6fb875d7fa3986dc40c083a0 X-Git-Newrev: fdbfb14bf96515d3d30cb8fc5725e57fff7d0601 X-Git-Rev: fdbfb14bf96515d3d30cb8fc5725e57fff7d0601 X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated archived-at: Fri, 27 Oct 2017 18:14:56 -0000 This is an automated email from the ASF dual-hosted git repository. agingade pushed a commit to branch feature/GEODE-3781 in repository https://gitbox.apache.org/repos/asf/geode.git The following commit(s) were added to refs/heads/feature/GEODE-3781 by this push: new fdbfb14 Added prepared statement cache Debug logging added that should be cleaned up later fdbfb14 is described below commit fdbfb14bf96515d3d30cb8fc5725e57fff7d0601 Author: Anil AuthorDate: Fri Oct 27 11:13:50 2017 -0700 Added prepared statement cache Debug logging added that should be cleaned up later --- .../apache/geode/connectors/jdbc/ColumnValue.java | 5 + .../geode/connectors/jdbc/JDBCAsyncWriter.java | 1 + .../apache/geode/connectors/jdbc/JDBCManager.java | 120 ++++++++++++++------- 3 files changed, 86 insertions(+), 40 deletions(-) diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/ColumnValue.java b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/ColumnValue.java index de422d5..fa540c2 100644 --- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/ColumnValue.java +++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/ColumnValue.java @@ -36,4 +36,9 @@ public class ColumnValue { public Object getValue() { return this.value; } + + @Override + public String toString() { + return "ColumnValue [isKey=" + isKey + ", columnName=" + columnName + ", value=" + value + "]"; + } } diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriter.java b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriter.java index 804301b..c394d12 100644 --- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriter.java +++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriter.java @@ -56,6 +56,7 @@ public class JDBCAsyncWriter implements AsyncEventListener { // In that case need to serialize and deserialize. try { PdxInstance value = (PdxInstance) event.getDeserializedValue(); + logger.info("AsyncEventListener event : " + event); this.manager.write(event.getRegion(), event.getOperation(), event.getKey(), value); successfulEvents += 1; } catch (RuntimeException ex) { diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java index 0ec643b..7c7ecea 100644 --- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java +++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java @@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentMap; import org.apache.geode.cache.Operation; import org.apache.geode.cache.Region; import org.apache.geode.pdx.PdxInstance; +import org.apache.geode.pdx.internal.PdxInstanceImpl; public class JDBCManager { @@ -45,8 +46,12 @@ public class JDBCManager { public void write(Region region, Operation operation, Object key, PdxInstance value) { String tableName = getTableName(region); + int pdxTypeId = 0; + if (value != null) { + pdxTypeId = ((PdxInstanceImpl) value).getPdxType().getTypeId(); + } List columnList = getColumnToValueList(tableName, key, value, operation); - int updateCount = executeWrite(columnList, tableName, operation, false); + int updateCount = executeWrite(columnList, tableName, operation, pdxTypeId, false); if (operation.isDestroy()) { return; } @@ -57,7 +62,7 @@ public class JDBCManager { } else { upsertOp = Operation.UPDATE; } - updateCount = executeWrite(columnList, tableName, upsertOp, true); + updateCount = executeWrite(columnList, tableName, upsertOp, pdxTypeId, true); } if (updateCount != 1) { throw new IllegalStateException("Unexpected updateCount " + updateCount); @@ -65,23 +70,25 @@ public class JDBCManager { } private int executeWrite(List columnList, String tableName, Operation operation, - boolean handleException) { - PreparedStatement pstmt = getQueryStatement(columnList, tableName, operation); - try { - int idx = 0; - for (ColumnValue cv : columnList) { - idx++; - pstmt.setObject(idx, cv.getValue()); - } - pstmt.execute(); - return pstmt.getUpdateCount(); - } catch (SQLException e) { - if (handleException || operation.isDestroy()) { - handleSQLException(e); + int pdxTypeId, boolean handleException) { + PreparedStatement pstmt = getPreparedStatement(columnList, tableName, operation, pdxTypeId); + synchronized (pstmt) { + try { + int idx = 0; + for (ColumnValue cv : columnList) { + idx++; + pstmt.setObject(idx, cv.getValue()); + } + pstmt.execute(); + return pstmt.getUpdateCount(); + } catch (SQLException e) { + if (handleException || operation.isDestroy()) { + handleSQLException(e); + } + return 0; + } finally { + clearStatement(pstmt); } - return 0; - } finally { - clearStatement(pstmt); } } @@ -179,30 +186,63 @@ public class JDBCManager { return result; } - // private final ConcurrentMap preparedStatementCache = new - // ConcurrentHashMap<>(); + private final ConcurrentMap preparedStatementCache = + new ConcurrentHashMap<>(); - private PreparedStatement getQueryStatement(List columnList, String tableName, - Operation operation) { - // ConcurrentMap cache = getPreparedStatementCache(operation); - // return cache.computeIfAbsent(query, k -> { - // String query = getQueryString(tableName, columnList, operation); - // Connection con = getConnection(); - // try { - // return con.prepareStatement(k); - // } catch (SQLException e) { - // handleSQLException(e); - // } - // }); - String query = getQueryString(tableName, columnList, operation); - System.out.println("query=" + query); - Connection con = getConnection(); - try { - return con.prepareStatement(query); - } catch (SQLException e) { - handleSQLException(e); - return null; // this line is never reached + private static class StatementKey { + private final int pdxTypeId; + private final Operation operation; + private final String tableName; + + public StatementKey(int pdxTypeId, Operation operation, String tableName) { + this.pdxTypeId = pdxTypeId; + this.operation = operation; + this.tableName = tableName; } + + @Override + public int hashCode() { + return operation.hashCode() + pdxTypeId + tableName.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + StatementKey other = (StatementKey) obj; + if (!operation.equals(other.operation)) { + return false; + } + if (pdxTypeId != other.pdxTypeId) { + return false; + } + if (!tableName.equals(other.tableName)) { + return false; + } + return true; + } + } + + private PreparedStatement getPreparedStatement(List columnList, String tableName, + Operation operation, int pdxTypeId) { + System.out.println("getPreparedStatement : " + pdxTypeId + "operation: " + operation + + " columns: " + columnList); + StatementKey key = new StatementKey(pdxTypeId, operation, tableName); + return preparedStatementCache.computeIfAbsent(key, k -> { + String query = getQueryString(tableName, columnList, operation); + System.out.println("query=" + query); + Connection con = getConnection(); + try { + return con.prepareStatement(query); + } catch (SQLException e) { + handleSQLException(e); + return null; // this line is never reached + } + }); } private List getColumnToValueList(String tableName, Object key, PdxInstance value, -- To stop receiving notification emails like this one, please contact ['"commits@geode.apache.org" '].