From 75ce563aae7a070404e5bb9a40ae8a24f68ec23c Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Tue, 3 Oct 2023 15:10:08 +0800 Subject: [PATCH] perf: remove use of DriverManager.println Closes: #984 --- src/main/java/org/sqlite/core/NativeDB.java | 18 +++++++++++----- .../sqlite/architecture/CodingRulesTest.java | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/sqlite/core/NativeDB.java b/src/main/java/org/sqlite/core/NativeDB.java index 5b26f832b..03b368ccf 100644 --- a/src/main/java/org/sqlite/core/NativeDB.java +++ b/src/main/java/org/sqlite/core/NativeDB.java @@ -18,8 +18,9 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; -import java.sql.DriverManager; import java.sql.SQLException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sqlite.BusyHandler; import org.sqlite.Collation; import org.sqlite.Function; @@ -29,6 +30,7 @@ /** This class provides a thin JNI layer over the SQLite3 C API. */ public final class NativeDB extends DB { + private static final Logger logger = LoggerFactory.getLogger(NativeDB.class); private static final int DEFAULT_BACKUP_BUSY_SLEEP_TIME_MILLIS = 100; private static final int DEFAULT_BACKUP_NUM_BUSY_BEFORE_FAIL = 3; private static final int DEFAULT_PAGES_PER_BACKUP_STEP = 100; @@ -88,8 +90,11 @@ protected synchronized void _open(String file, int openFlags) throws SQLExceptio /** @see org.sqlite.core.DB#_exec(java.lang.String) */ @Override public synchronized int _exec(String sql) throws SQLException { - DriverManager.println( - "DriverManager [" + Thread.currentThread().getName() + "] [SQLite EXEC] " + sql); + logger.atTrace() + .setMessage("DriverManager [{}] [SQLite EXEC] {}") + .addArgument(() -> Thread.currentThread().getName()) + .addArgument(sql) + .log(); return _exec_utf8(stringToUtf8ByteArray(sql)); } @@ -121,8 +126,11 @@ public synchronized int _exec(String sql) throws SQLException { /** @see org.sqlite.core.DB#prepare(java.lang.String) */ @Override protected synchronized SafeStmtPtr prepare(String sql) throws SQLException { - DriverManager.println( - "DriverManager [" + Thread.currentThread().getName() + "] [SQLite PREP] " + sql); + logger.atTrace() + .setMessage("DriverManager [{}] [SQLite EXEC] {}") + .addArgument(() -> Thread.currentThread().getName()) + .addArgument(sql) + .log(); return new SafeStmtPtr(this, prepare_utf8(stringToUtf8ByteArray(sql))); } diff --git a/src/test/java/org/sqlite/architecture/CodingRulesTest.java b/src/test/java/org/sqlite/architecture/CodingRulesTest.java index c37ae4d91..ceb156b1d 100644 --- a/src/test/java/org/sqlite/architecture/CodingRulesTest.java +++ b/src/test/java/org/sqlite/architecture/CodingRulesTest.java @@ -3,13 +3,20 @@ import static com.tngtech.archunit.base.DescribedPredicate.not; import static com.tngtech.archunit.core.domain.JavaClass.Predicates.equivalentTo; import static com.tngtech.archunit.lang.conditions.ArchPredicates.are; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; import static com.tngtech.archunit.library.GeneralCodingRules.*; +import com.tngtech.archunit.core.domain.JavaCall; +import com.tngtech.archunit.core.domain.JavaClass; import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.properties.HasName; +import com.tngtech.archunit.core.domain.properties.HasOwner; import com.tngtech.archunit.core.importer.ImportOption; import com.tngtech.archunit.junit.AnalyzeClasses; import com.tngtech.archunit.junit.ArchTest; import com.tngtech.archunit.lang.ArchRule; +import com.tngtech.archunit.lang.conditions.ArchConditions; +import java.sql.DriverManager; import org.sqlite.util.OSInfo; @AnalyzeClasses( @@ -30,4 +37,18 @@ void no_access_to_standard_streams(JavaClasses importedClasses) { @ArchTest private final ArchRule no_jodatime = NO_CLASSES_SHOULD_USE_JODATIME; @ArchTest private final ArchRule no_java_util_logging = NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING; + + @ArchTest + private final ArchRule no_driver_manager_println = + noClasses() + .should( + ArchConditions.callMethodWhere( + JavaCall.Predicates.target(HasName.Predicates.name("println")) + .and( + JavaCall.Predicates.target( + HasOwner.Predicates.With.owner( + JavaClass.Predicates + .assignableTo( + DriverManager + .class)))))); }