diff --git a/hugegraph-dist/src/assembly/static/conf/hugegraph.properties b/hugegraph-dist/src/assembly/static/conf/hugegraph.properties index 047d6b0259..ba72118c2a 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= +#jdbc.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..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 @@ -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,13 @@ public static synchronized PostgresqlOptions instance() { } return instance; } + + public static final ConfigOption POSTGRESQL_CONNECT_DATABASE = + new ConfigOption<>( + "jdbc.postgresql.connect_database", + "The database used to connect when init store, " + + "drop store or check store exist.", + 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('\'');