From 4f703fe7d1850af80b4c8f9f3e114de0ff1cc607 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Fri, 24 May 2019 21:33:33 +0800 Subject: [PATCH 1/2] suppress error log when postgre database does not exist May 24, 2019 1:25:14 PM org.postgresql.Driver connect SEVERE: Connection error: org.postgresql.util.PSQLException: FATAL: database "hugegraph" does not exist Change-Id: I48aebac67ec98f5c05b96239b497940150542dcc --- .../hugegraph/backend/store/mysql/MysqlSessions.java | 6 +++++- .../backend/store/postgresql/PostgresqlSessions.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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 6a87c9eb9d..96514cd17d 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 @@ -94,7 +94,7 @@ private Connection open(boolean autoReconnect) throws SQLException { int maxTimes = this.config.get(MysqlOptions.JDBC_RECONNECT_MAX_TIMES); int interval = this.config.get(MysqlOptions.JDBC_RECONNECT_INTERVAL); - URIBuilder uriBuilder = new URIBuilder(); + URIBuilder uriBuilder = this.newConnectionURIBuilder(); uriBuilder.setPath(url) .setParameter("rewriteBatchedStatements", "true") .setParameter("useServerPrepStmts", "false") @@ -118,6 +118,10 @@ private Connection connect(String url) throws SQLException { return DriverManager.getConnection(url, username, password); } + protected URIBuilder newConnectionURIBuilder() { + return new URIBuilder(); + } + @Override protected void doClose() { // pass 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 638d63c061..d0287fda3e 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 @@ -22,6 +22,7 @@ import java.sql.Connection; import java.sql.SQLException; +import org.apache.http.client.utils.URIBuilder; import org.postgresql.util.PSQLException; import org.slf4j.Logger; @@ -69,4 +70,15 @@ public void createDatabase() { // Ignore exception if database already exists } } + + @Override + protected String buildCreateDatabase(String database) { + return String.format(POSTGRESQL_DB_CREATE, database); + } + + @Override + protected URIBuilder newConnectionURIBuilder() { + // Suppress error log when database does not exist + return new URIBuilder().addParameter("loggerLevel", "OFF"); + } } From 4f7e18b3972cc9034081af8856ca123071014322 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Mon, 27 May 2019 18:15:10 +0800 Subject: [PATCH 2/2] tiny improve Change-Id: Ie89d957260dcd54827137ce57c8fee192bf6b600 --- .../backend/store/postgresql/PostgresqlSessions.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 d0287fda3e..fb882b7db2 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 @@ -50,12 +50,12 @@ public void createDatabase() { // Create database with non-database-session LOG.debug("Create database: {}", this.database()); - String sql = String.format(POSTGRESQL_DB_CREATE, this.database()); + String sql = this.buildCreateDatabase(this.database()); try (Connection conn = this.openWithoutDB(0)) { try { conn.createStatement().execute(sql); } catch (PSQLException e) { - // CockroackDB not support 'template' args of CREATE DATABASE + // CockroackDB not support 'template' arg of CREATE DATABASE if (e.getMessage().contains("syntax error at or near " + "\"template\"")) { sql = String.format(COCKROACH_DB_CREATE, this.database());