From a1b79835009c573ea3c4ecdcbd75b8f2aea39631 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Wed, 9 Dec 2020 20:25:13 +0800 Subject: [PATCH 1/2] fix psotgresql init failed due to not exist same name db with user Change-Id: I955fb0a474904470355b92dbc9bf3aa08ea4f61e fixed: #1285 --- .../assembly/static/conf/hugegraph.properties | 1 + .../backend/store/mysql/MysqlSessions.java | 22 +++++++++++++------ .../store/postgresql/PostgresqlOptions.java | 11 ++++++++++ .../store/postgresql/PostgresqlSessions.java | 5 +++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/hugegraph-dist/src/assembly/static/conf/hugegraph.properties b/hugegraph-dist/src/assembly/static/conf/hugegraph.properties index 047d6b0259..d1ba51ad17 100644 --- a/hugegraph-dist/src/assembly/static/conf/hugegraph.properties +++ b/hugegraph-dist/src/assembly/static/conf/hugegraph.properties @@ -77,6 +77,7 @@ cassandra.password= #jdbc.url=jdbc:postgresql://localhost:5432/ #jdbc.username=postgres #jdbc.password= +#postgresql.connect_database=template1 # palo backend config #palo.host=127.0.0.1 diff --git a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java index 87efedfd1e..7e02fc6520 100644 --- a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java +++ b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java @@ -30,6 +30,7 @@ import java.util.stream.IntStream; import org.apache.http.client.utils.URIBuilder; +import org.apache.logging.log4j.util.Strings; import org.slf4j.Logger; import com.baidu.hugegraph.backend.BackendException; @@ -213,13 +214,7 @@ private Connection open(boolean autoReconnect) throws SQLException { protected String buildUri(boolean withConnParams, boolean withDB, boolean autoReconnect, Integer timeout) { - String url = this.config.get(MysqlOptions.JDBC_URL); - if (!url.endsWith("/")) { - url = String.format("%s/", url); - } - if (withDB) { - url = String.format("%s%s", url, this.database()); - } + String url = this.buildUrlPrefix(withDB); int maxTimes = this.config.get(MysqlOptions.JDBC_RECONNECT_MAX_TIMES); int interval = this.config.get(MysqlOptions.JDBC_RECONNECT_INTERVAL); @@ -241,6 +236,19 @@ protected String buildUri(boolean withConnParams, boolean withDB, return builder.toString(); } + protected String buildUrlPrefix(boolean withDB) { + String url = this.config.get(MysqlOptions.JDBC_URL); + if (!url.endsWith("/")) { + url = String.format("%s/", url); + } + String database = withDB ? this.database() : this.connectDatabase(); + return String.format("%s%s", url, database); + } + + protected String connectDatabase() { + return Strings.EMPTY; + } + protected URIBuilder newConnectionURIBuilder() { return new URIBuilder(); } diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlOptions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlOptions.java index 75b48c6831..4bed8fd28d 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlOptions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlOptions.java @@ -19,7 +19,10 @@ package com.baidu.hugegraph.backend.store.postgresql; +import static com.baidu.hugegraph.config.OptionChecker.disallowEmpty; + import com.baidu.hugegraph.backend.store.mysql.MysqlOptions; +import com.baidu.hugegraph.config.ConfigOption; public class PostgresqlOptions extends MysqlOptions { @@ -36,4 +39,12 @@ public static synchronized PostgresqlOptions instance() { } return instance; } + + public static final ConfigOption POSTGRESQL_CONNECT_DATABASE = + new ConfigOption<>( + "postgresql.connect_database", + "The database used to connect when not specify database.", + disallowEmpty(), + "template1" + ); } diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java index 39bdf255fc..42e53b3d85 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java @@ -117,6 +117,11 @@ protected URIBuilder newConnectionURIBuilder() { return new URIBuilder().addParameter("loggerLevel", "OFF"); } + @Override + protected String connectDatabase() { + return this.config().get(PostgresqlOptions.POSTGRESQL_CONNECT_DATABASE); + } + public static String escapeAndWrapString(String value) { StringBuilder builder = new StringBuilder(8 + value.length()); builder.append('\''); From db518b5d102ad0d6532523c3b81afd825e9a1eff Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Thu, 10 Dec 2020 12:22:15 +0800 Subject: [PATCH 2/2] improve Change-Id: I6bbb41944699b388ac78e6ad21ee9a51dc8036f7 --- hugegraph-dist/src/assembly/static/conf/hugegraph.properties | 2 +- .../backend/store/postgresql/PostgresqlOptions.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hugegraph-dist/src/assembly/static/conf/hugegraph.properties b/hugegraph-dist/src/assembly/static/conf/hugegraph.properties index d1ba51ad17..ba72118c2a 100644 --- a/hugegraph-dist/src/assembly/static/conf/hugegraph.properties +++ b/hugegraph-dist/src/assembly/static/conf/hugegraph.properties @@ -77,7 +77,7 @@ cassandra.password= #jdbc.url=jdbc:postgresql://localhost:5432/ #jdbc.username=postgres #jdbc.password= -#postgresql.connect_database=template1 +#jdbc.postgresql.connect_database=template1 # palo backend config #palo.host=127.0.0.1 diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlOptions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlOptions.java index 4bed8fd28d..c2f0049417 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlOptions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlOptions.java @@ -42,8 +42,9 @@ public static synchronized PostgresqlOptions instance() { public static final ConfigOption POSTGRESQL_CONNECT_DATABASE = new ConfigOption<>( - "postgresql.connect_database", - "The database used to connect when not specify database.", + "jdbc.postgresql.connect_database", + "The database used to connect when init store, " + + "drop store or check store exist.", disallowEmpty(), "template1" );