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

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


The following commit(s) were added to refs/heads/feature/GEODE-4009 by this 
push:
     new 7840e85  Add DataSource interface and builder
7840e85 is described below

commit 7840e852e881dc612b992fa252cde536233dd11d
Author: Anil <aging...@pivotal.io>
AuthorDate: Thu Dec 21 13:28:45 2017 -0800

    Add DataSource interface and builder
---
 .../jdbc/internal/ConnectionConfiguration.java     |  7 ----
 .../jdbc/internal/ConnectionManager.java           | 40 +++++++++----------
 .../jdbc/internal/HikariJdbcDataSource.java        | 45 ++++++++++++++++++++++
 .../connectors/jdbc/internal/JdbcDataSource.java   | 22 +++++++++++
 .../jdbc/internal/JdbcDataSourceBuilder.java       | 28 ++++++++++++++
 .../jdbc/internal/ConnectionManagerUnitTest.java   | 36 ++++++++---------
 6 files changed, 130 insertions(+), 48 deletions(-)

diff --git 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionConfiguration.java
 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionConfiguration.java
index d993e54..6a69079 100644
--- 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionConfiguration.java
+++ 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionConfiguration.java
@@ -63,16 +63,9 @@ public class ConnectionConfiguration implements Serializable 
{
 
   public Properties getConnectionProperties() {
     Properties properties = new Properties();
-
     if (parameters != null) {
       properties.putAll(parameters);
     }
-    if (user != null) {
-      properties.put(USER, user);
-    }
-    if (password != null) {
-      properties.put(PASSWORD, password);
-    }
     return properties;
   }
 
diff --git 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionManager.java
 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionManager.java
index bd3da6c..22c7a95 100644
--- 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionManager.java
+++ 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionManager.java
@@ -16,7 +16,6 @@ package org.apache.geode.connectors.jdbc.internal;
 
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
-import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -27,17 +26,13 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
-import javax.sql.DataSource;
-
-import com.zaxxer.hikari.HikariDataSource;
-
 import org.apache.geode.cache.Operation;
 import org.apache.geode.pdx.PdxInstance;
 
 class ConnectionManager {
 
   private final InternalJdbcConnectorService configService;
-  private final Map<String, HikariDataSource> dataSourceMap = new 
ConcurrentHashMap<>();
+  private final Map<String, JdbcDataSource> dataSourceMap = new 
ConcurrentHashMap<>();
   private final ConcurrentMap<String, String> tableToPrimaryKeyMap = new 
ConcurrentHashMap<>();
   private final ThreadLocal<PreparedStatementCache> preparedStatementCache = 
new ThreadLocal<>();
 
@@ -49,20 +44,21 @@ class ConnectionManager {
     return configService.getMappingForRegion(regionName);
   }
 
-  synchronized DataSource createDataSource(ConnectionConfiguration config) {
-    DataSource dataSource = dataSourceMap.get(config.getName());
-    if (dataSource != null) {
-      return dataSource;
+  private synchronized JdbcDataSource createDataSource(ConnectionConfiguration 
config) {
+    JdbcDataSource dataSource = dataSourceMap.get(config.getName());
+    if (dataSource == null) {
+      dataSource = buildJdbcDataSource(config);
+      dataSourceMap.put(config.getName(), dataSource);
     }
-    HikariDataSource ds = new HikariDataSource();
-    ds.setJdbcUrl(config.getUrl());
-    ds.setDataSourceProperties(config.getConnectionProperties());
-    dataSourceMap.put(config.getName(), ds);
-    return ds;
+    return dataSource;
   }
 
-  private DataSource getDataSource(ConnectionConfiguration config) {
-    DataSource dataSource = dataSourceMap.get(config.getName());
+  JdbcDataSource buildJdbcDataSource(ConnectionConfiguration config) {
+    return new JdbcDataSourceBuilder(config).create();
+  }
+
+  private JdbcDataSource getDataSource(ConnectionConfiguration config) {
+    JdbcDataSource dataSource = dataSourceMap.get(config.getName());
     if (dataSource != null) {
       return dataSource;
     }
@@ -183,12 +179,16 @@ class ConnectionManager {
   }
 
   private void handleSQLException(SQLException e) {
-    throw new IllegalStateException("NYI: handleSQLException", e);
+    throw new IllegalStateException("JDBC connector detected unexpected 
SQLException", e);
   }
 
-  private void close(HikariDataSource dataSource) {
+  private void close(JdbcDataSource dataSource) {
     if (dataSource != null) {
-      dataSource.close();
+      try {
+        dataSource.close();
+      } catch (Exception e) {
+        // TODO ignored for now; should it be logged?
+      }
     }
   }
 }
diff --git 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/HikariJdbcDataSource.java
 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/HikariJdbcDataSource.java
new file mode 100644
index 0000000..62a1261
--- /dev/null
+++ 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/HikariJdbcDataSource.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ */
+package org.apache.geode.connectors.jdbc.internal;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import com.zaxxer.hikari.HikariDataSource;
+
+public class HikariJdbcDataSource implements JdbcDataSource {
+
+  private final HikariDataSource delegate;
+
+  public HikariJdbcDataSource(ConnectionConfiguration config) {
+    HikariDataSource ds = new HikariDataSource();
+    ds.setJdbcUrl(config.getUrl());
+    ds.setUsername(config.getUser());
+    ds.setPassword(config.getPassword());
+    ds.setDataSourceProperties(config.getConnectionProperties());
+    this.delegate = ds;
+  }
+
+  @Override
+  public Connection getConnection() throws SQLException {
+    return this.delegate.getConnection();
+  }
+
+  @Override
+  public void close() {
+    this.delegate.close();
+  }
+
+}
diff --git 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSource.java
 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSource.java
new file mode 100644
index 0000000..1e7f753
--- /dev/null
+++ 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSource.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ */
+package org.apache.geode.connectors.jdbc.internal;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+public interface JdbcDataSource extends AutoCloseable {
+  Connection getConnection() throws SQLException;
+}
diff --git 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceBuilder.java
 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceBuilder.java
new file mode 100644
index 0000000..4663a89
--- /dev/null
+++ 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceBuilder.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ */
+package org.apache.geode.connectors.jdbc.internal;
+
+public class JdbcDataSourceBuilder {
+  private final ConnectionConfiguration configuration;
+
+  public JdbcDataSourceBuilder(ConnectionConfiguration configuration) {
+    this.configuration = configuration;
+  }
+
+  public JdbcDataSource create() {
+    return new HikariJdbcDataSource(this.configuration);
+  }
+
+}
diff --git 
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/ConnectionManagerUnitTest.java
 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/ConnectionManagerUnitTest.java
index 17d8128..8e1d124 100644
--- 
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/ConnectionManagerUnitTest.java
+++ 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/ConnectionManagerUnitTest.java
@@ -31,6 +31,8 @@ import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.List;
 
+import javax.sql.DataSource;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
@@ -48,6 +50,7 @@ public class ConnectionManagerUnitTest {
   private static final String KEY_COLUMN = "keyColumn";
 
   private JdbcConnectorService configService;
+  private JdbcDataSource dataSource;
   private ConnectionManager manager;
   private Connection connection;
   private RegionMapping mapping;
@@ -57,8 +60,10 @@ public class ConnectionManagerUnitTest {
 
   private ConnectionConfiguration connectionConfig;
 
+
   @Before
   public void setup() throws Exception {
+    dataSource = mock(JdbcDataSource.class);
     configService = mock(JdbcConnectorService.class);
     manager = spy(new ConnectionManager(configService));
     connection = mock(Connection.class);
@@ -69,7 +74,8 @@ public class ConnectionManagerUnitTest {
 
     when(mapping.getTableName()).thenReturn(TABLE_NAME);
     when(mapping.getRegionToTableName()).thenReturn(TABLE_NAME);
-    doReturn(connection).when(manager).createDataSource(connectionConfig);
+    doReturn(dataSource).when(manager).buildJdbcDataSource(connectionConfig);
+    doReturn(connection).when(dataSource).getConnection();
 
     key = new Object();
   }
@@ -107,9 +113,11 @@ public class ConnectionManagerUnitTest {
   @Test
   public void retrievesDifferentConnectionForEachConfig() throws Exception {
     Connection secondConnection = mock(Connection.class);
+    JdbcDataSource dataSource2 = mock(JdbcDataSource.class);
     ConnectionConfiguration secondConnectionConfig =
         new ConnectionConfiguration("newName", "url", null, null, null);
-    
doReturn(secondConnection).when(manager).createDataSource(secondConnectionConfig);
+    
doReturn(dataSource2).when(manager).buildJdbcDataSource(secondConnectionConfig);
+    doReturn(secondConnection).when(dataSource2).getConnection();
 
     Connection returnedConnection = manager.getConnection(connectionConfig);
     Connection secondReturnedConnection = 
manager.getConnection(secondConnectionConfig);
@@ -120,30 +128,16 @@ public class ConnectionManagerUnitTest {
   }
 
   @Test
-  public void retrievesANewConnectionIfCachedOneIsClosed() throws Exception {
-    manager.getConnection(connectionConfig);
-    when(connection.isClosed()).thenReturn(true);
-    Connection secondConnection = mock(Connection.class);
-    
doReturn(secondConnection).when(manager).createDataSource(connectionConfig);
-
-    Connection secondReturnedConnection = 
manager.getConnection(connectionConfig);
-
-    assertThat(secondReturnedConnection).isSameAs(secondConnection);
-  }
-
-  @Test
-  public void closesAllConnections() throws Exception {
-    Connection secondConnection = mock(Connection.class);
+  public void closesAllDataSources() throws Exception {
+    JdbcDataSource dataSource2 = mock(JdbcDataSource.class);
     ConnectionConfiguration secondConnectionConfig =
         new ConnectionConfiguration("newName", "url", null, null, null);
-    
doReturn(secondConnection).when(manager).createDataSource(secondConnectionConfig);
+    
doReturn(dataSource2).when(manager).buildJdbcDataSource(secondConnectionConfig);
     manager.getConnection(connectionConfig);
     manager.getConnection(secondConnectionConfig);
-
     manager.close();
-
-    verify(connection).close();
-    verify(secondConnection).close();
+    verify(dataSource).close();
+    verify(dataSource2).close();
   }
 
   @Test

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

Reply via email to