Skip to content

Commit

Permalink
Merge pull request #1039 from harawata/gh/1022
Browse files Browse the repository at this point in the history
Replace synchronized method with Lock
  • Loading branch information
hazendaz authored Dec 9, 2024
2 parents 0cff0ea + ef63486 commit 1cc50c6
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,7 @@ public class MyBatisExceptionTranslator implements PersistenceExceptionTranslato

private final Supplier<SQLExceptionTranslator> exceptionTranslatorSupplier;
private SQLExceptionTranslator exceptionTranslator;
private ReentrantLock lock = new ReentrantLock();

/**
* Creates a new {@code PersistenceExceptionTranslator} instance with {@code SQLErrorCodeSQLExceptionTranslator}.
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 1cc50c6

Please sign in to comment.