You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ag...@apache.org on 2017/10/25 23:16:11 UTC

[geode] branch feature/GEODE-3781 updated (ad900a1 -> eae6c07)

This is an automated email from the ASF dual-hosted git repository.

agingade pushed a change to branch feature/GEODE-3781
in repository https://gitbox.apache.org/repos/asf/geode.git.


    from ad900a1  spotless
     new 7c70ae3  Added implementation for building the pdx field table column mapping.
     new eae6c07  implemented getConnection

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/geode/connectors/jdbc/JDBCManager.java  | 116 ++++++++++++++-------
 1 file changed, 79 insertions(+), 37 deletions(-)

-- 
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <co...@geode.apache.org>'].

[geode] 02/02: implemented getConnection

Posted by ag...@apache.org.
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

commit eae6c079562b56fec8c362d346afbafdd8676d2b
Author: Anil <ag...@pivotal.io>
AuthorDate: Wed Oct 25 16:15:53 2017 -0700

    implemented getConnection
---
 .../apache/geode/connectors/jdbc/JDBCManager.java  | 35 +++++++++++++++++-----
 1 file changed, 28 insertions(+), 7 deletions(-)

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 ed8bcd9..7c0283c 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
@@ -44,12 +44,6 @@ public class JDBCManager {
     this.config = config;
   }
 
-  private void establishConnection() {
-    // Class.forName(this.config.getDriver());
-    // conn = DriverManager.getConnection(this.config.getURL());
-    // stmt = conn.createStatement();
-  }
-
   public class ColumnValue {
 
     final private boolean isKey;
@@ -144,7 +138,34 @@ public class JDBCManager {
   }
 
   private Connection getConnection() {
-    return null; // NYI
+    Connection result = this.conn;
+    try {
+      if (result != null && !result.isClosed()) {
+        return result;
+      }
+    }
+    catch (SQLException ignore) {
+      // If isClosed throws fall through and connect again
+    }
+
+    if (result == null) {
+      try {
+        Class.forName(this.config.getDriver());
+      }
+      catch (ClassNotFoundException e) {
+        // TODO: consider a different exception
+        throw new IllegalStateException("Driver class " + this.config.getDriver() + " not found", e);
+      }
+    }
+    try {
+      result = DriverManager.getConnection(this.config.getURL());
+    }
+    catch (SQLException e) {
+      // TODO: consider a different exception
+      throw new IllegalStateException("Could not connect to " + this.config.getURL(), e);
+    }
+    this.conn = result;
+    return result;
   }
 
   // private final ConcurrentMap<String, PreparedStatement> preparedStatementCache = new

-- 
To stop receiving notification emails like this one, please contact
"commits@geode.apache.org" <co...@geode.apache.org>.

[geode] 01/02: Added implementation for building the pdx field table column mapping.

Posted by ag...@apache.org.
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

commit 7c70ae365d871121666943dc0df85604fd786180
Author: Anil <ag...@pivotal.io>
AuthorDate: Wed Oct 25 15:59:02 2017 -0700

    Added implementation for building the pdx field table column mapping.
---
 .../apache/geode/connectors/jdbc/JDBCManager.java  | 81 ++++++++++++++--------
 1 file changed, 51 insertions(+), 30 deletions(-)

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 15daaed..ed8bcd9 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
@@ -20,6 +20,7 @@ import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -77,13 +78,26 @@ public class JDBCManager {
   public void write(Region region, Operation operation, Object key, PdxInstance value) {
     String tableName = getTableName(region);
     List<ColumnValue> columnList = getColumnToValueList(tableName, key, value, operation);
-    String query = getQueryString(tableName, columnList, operation);
-    PreparedStatement statement = getQueryStatement(columnList, query);
+    PreparedStatement pstmt = getQueryStatement(columnList, tableName, operation);
     try {
-      statement.execute(query);
+      int idx = 0;
+      for (ColumnValue cv : columnList) {
+        idx++;
+        pstmt.setObject(idx, cv.getValue());
+      }
+      pstmt.execute();
     } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
+    } finally {
+      clearStatement(pstmt);
+    }
+  }
+
+  private void clearStatement(PreparedStatement ps) {
+    try {
+      ps.clearParameters();
+    } catch (SQLException ignore) {
     }
   }
 
@@ -133,47 +147,54 @@ public class JDBCManager {
     return null; // NYI
   }
 
-  private final ConcurrentMap<String, PreparedStatement> preparedStatementCache =
-      new ConcurrentHashMap<>();
+  // private final ConcurrentMap<String, PreparedStatement> preparedStatementCache = new
+  // ConcurrentHashMap<>();
 
-  private PreparedStatement getQueryStatement(List<ColumnValue> columnList, String query) {
-    return preparedStatementCache.computeIfAbsent(query, k -> {
-      Connection con = getConnection();
-      try {
-        return con.prepareStatement(k);
-      } catch (SQLException e) {
-        throw new IllegalStateException("TODO handle exception", e);
-      }
-    });
+  private PreparedStatement getQueryStatement(List<ColumnValue> columnList, String tableName,
+      Operation operation) {
+    // ConcurrentMap<String, PreparedStatement> 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) {
+    // throw new IllegalStateException("TODO handle exception", e);
+    // }
+    // });
+    String query = getQueryString(tableName, columnList, operation);
+    Connection con = getConnection();
+    try {
+      return con.prepareStatement(query);
+    } catch (SQLException e) {
+      throw new IllegalStateException("TODO handle exception", e);
+    }
   }
 
   private List<ColumnValue> getColumnToValueList(String tableName, Object key, PdxInstance value,
       Operation operation) {
-    Set<String> keyColumnNames = getKeyColumnNames(tableName);
+    String keyColumnName = getKeyColumnName(tableName);
+    ColumnValue keyCV = new ColumnValue(true, keyColumnName, key);
+    if (operation.isDestroy()) {
+      return Collections.singletonList(keyCV);
+    }
+
     List<String> fieldNames = value.getFieldNames();
     List<ColumnValue> result = new ArrayList<>(fieldNames.size() + 1);
     for (String fieldName : fieldNames) {
-      String columnName = mapFieldNameToColumnName(fieldName, tableName);
-      if (columnName == null) {
-        // this field is not mapped to a column
-        if (isFieldExcluded(fieldName)) {
-          continue;
-        } else {
-          throw new IllegalStateException(
-              "No column on table " + tableName + " was found for the field named " + fieldName);
-        }
+      if (isFieldExcluded(fieldName)) {
+        continue;
       }
-      boolean isKey = keyColumnNames.contains(columnName);
-
-      if (operation.isDestroy() && !isKey) {
+      String columnName = mapFieldNameToColumnName(fieldName, tableName);
+      if (columnName.equals(keyColumnName)) {
         continue;
       }
-      // TODO: what if isKey and columnValue needs to be the key object instead of from PdxInstance?
       Object columnValue = value.getField(fieldName);
-      ColumnValue cv = new ColumnValue(isKey, fieldName, columnValue);
+      ColumnValue cv = new ColumnValue(false, fieldName, columnValue);
       // TODO: any need to order the items in the list?
       result.add(cv);
     }
+    result.add(keyCV);
     return result;
   }
 
@@ -187,7 +208,7 @@ public class JDBCManager {
     return fieldName;
   }
 
-  private Set<String> getKeyColumnNames(String tableName) {
+  private String getKeyColumnName(String tableName) {
     // TODO Auto-generated method stub
     return null;
   }

-- 
To stop receiving notification emails like this one, please contact
"commits@geode.apache.org" <co...@geode.apache.org>.