You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ma...@apache.org on 2022/08/17 13:01:36 UTC

[shardingsphere] 01/01: Use connection pool for JDBCRepository

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

machen pushed a commit to branch dev_19654
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git

commit 565f687be594cf7f491954349c881111431e330c
Author: chen.ma <ma...@163.com>
AuthorDate: Wed Aug 17 21:01:24 2022 +0800

    Use connection pool for JDBCRepository
---
 .../pom.xml                                        |  6 +++
 .../repository/standalone/jdbc/JDBCRepository.java | 46 +++++++++++++---------
 2 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/pom.xml b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-re [...]
index 5299816f6d5..0cbe598e40a 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/pom.xml
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/pom.xml
@@ -32,5 +32,11 @@
             <artifactId>shardingsphere-standalone-mode-repository-api</artifactId>
             <version>${project.version}</version>
         </dependency>
+        
+        <dependency>
+            <groupId>com.zaxxer</groupId>
+            <artifactId>HikariCP</artifactId>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/ [...]
index e603700e5c4..0d01991a121 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.mode.repository.standalone.jdbc;
 
 import com.google.common.base.Strings;
+import com.zaxxer.hikari.HikariDataSource;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository;
@@ -27,7 +28,6 @@ import org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRe
 import org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProviderFactory;
 
 import java.sql.Connection;
-import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -48,18 +48,22 @@ public final class JDBCRepository implements StandalonePersistRepository {
     
     private static final String SEPARATOR = "/";
     
-    private Connection connection;
-    
     private JDBCRepositoryProvider provider;
     
+    private HikariDataSource hikariDataSource;
+    
     @SneakyThrows
     @Override
     public void init(final Properties props) {
         JDBCRepositoryProperties jdbcRepositoryProps = new JDBCRepositoryProperties(props);
-        connection = DriverManager.getConnection(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.JDBC_URL),
-                jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.USERNAME), jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PASSWORD));
-        provider = JDBCRepositoryProviderFactory.getInstance(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PROVIDER));
-        try (Statement statement = connection.createStatement()) {
+        hikariDataSource = new HikariDataSource();
+        hikariDataSource.setJdbcUrl(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.JDBC_URL));
+        hikariDataSource.setUsername(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.USERNAME));
+        hikariDataSource.setPassword(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PASSWORD));
+        try (
+                Connection connection = hikariDataSource.getConnection();
+                Statement statement = connection.createStatement()) {
+            provider = JDBCRepositoryProviderFactory.getInstance(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PROVIDER));
             statement.execute(provider.dropTableSQL());
             statement.execute(provider.createTableSQL());
         }
@@ -67,7 +71,9 @@ public final class JDBCRepository implements StandalonePersistRepository {
     
     @Override
     public String get(final String key) {
-        try (PreparedStatement preparedStatement = connection.prepareStatement(provider.selectByKeySQL())) {
+        try (
+                Connection connection = hikariDataSource.getConnection();
+                PreparedStatement preparedStatement = connection.prepareStatement(provider.selectByKeySQL())) {
             preparedStatement.setString(1, key);
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
                 if (resultSet.next()) {
@@ -82,7 +88,9 @@ public final class JDBCRepository implements StandalonePersistRepository {
     
     @Override
     public List<String> getChildrenKeys(final String key) {
-        try (PreparedStatement preparedStatement = connection.prepareStatement(provider.selectByParentKeySQL())) {
+        try (
+                Connection connection = hikariDataSource.getConnection();
+                PreparedStatement preparedStatement = connection.prepareStatement(provider.selectByParentKeySQL())) {
             preparedStatement.setString(1, key);
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
                 List<String> resultChildren = new LinkedList<>();
@@ -133,7 +141,9 @@ public final class JDBCRepository implements StandalonePersistRepository {
     }
     
     private void insert(final String key, final String value, final String parent) throws SQLException {
-        try (PreparedStatement preparedStatement = connection.prepareStatement(provider.insertSQL())) {
+        try (
+                Connection connection = hikariDataSource.getConnection();
+                PreparedStatement preparedStatement = connection.prepareStatement(provider.insertSQL())) {
             preparedStatement.setString(1, UUID.randomUUID().toString());
             preparedStatement.setString(2, key);
             preparedStatement.setString(3, value);
@@ -143,7 +153,9 @@ public final class JDBCRepository implements StandalonePersistRepository {
     }
     
     private void update(final String key, final String value) throws SQLException {
-        try (PreparedStatement preparedStatement = connection.prepareStatement(provider.updateSQL())) {
+        try (
+                Connection connection = hikariDataSource.getConnection();
+                PreparedStatement preparedStatement = connection.prepareStatement(provider.updateSQL())) {
             preparedStatement.setString(1, value);
             preparedStatement.setString(2, key);
             preparedStatement.executeUpdate();
@@ -152,7 +164,9 @@ public final class JDBCRepository implements StandalonePersistRepository {
     
     @Override
     public void delete(final String key) {
-        try (PreparedStatement preparedStatement = connection.prepareStatement(provider.deleteSQL())) {
+        try (
+                Connection connection = hikariDataSource.getConnection();
+                PreparedStatement preparedStatement = connection.prepareStatement(provider.deleteSQL())) {
             preparedStatement.setString(1, key);
             preparedStatement.executeUpdate();
         } catch (final SQLException ex) {
@@ -162,13 +176,7 @@ public final class JDBCRepository implements StandalonePersistRepository {
     
     @Override
     public void close() {
-        try {
-            if (null != connection) {
-                connection.close();
-            }
-        } catch (final SQLException ex) {
-            log.error(String.format("Failed to release %s database resources.", getType()), ex);
-        }
+        hikariDataSource.close();
     }
     
     @Override