From ef63486d3afcac295b6fa659679961d47c198352 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Mon, 9 Dec 2024 17:26:45 +0900 Subject: [PATCH] Replace synchronized method with Lock Should fix #1022 --- .../mybatis/spring/MyBatisExceptionTranslator.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java b/src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java index 9f389d117d..cfcdefe202 100644 --- a/src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java +++ b/src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java @@ -16,6 +16,7 @@ package org.mybatis.spring; import java.sql.SQLException; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Supplier; import javax.sql.DataSource; @@ -41,6 +42,7 @@ public class MyBatisExceptionTranslator implements PersistenceExceptionTranslato private final Supplier exceptionTranslatorSupplier; private SQLExceptionTranslator exceptionTranslator; + private ReentrantLock lock = new ReentrantLock(); /** * Creates a new {@code PersistenceExceptionTranslator} instance with {@code SQLErrorCodeSQLExceptionTranslator}. @@ -104,9 +106,14 @@ public DataAccessException translateExceptionIfPossible(RuntimeException e) { /** * Initializes the internal translator reference. */ - private synchronized void initExceptionTranslator() { - if (this.exceptionTranslator == null) { - this.exceptionTranslator = exceptionTranslatorSupplier.get(); + private void initExceptionTranslator() { + lock.lock(); + try { + if (this.exceptionTranslator == null) { + this.exceptionTranslator = exceptionTranslatorSupplier.get(); + } + } finally { + lock.unlock(); } }