Skip to content

Commit ef63486

Browse files
committed
Replace synchronized method with Lock
Should fix #1022
1 parent 0cff0ea commit ef63486

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.mybatis.spring;
1717

1818
import java.sql.SQLException;
19+
import java.util.concurrent.locks.ReentrantLock;
1920
import java.util.function.Supplier;
2021

2122
import javax.sql.DataSource;
@@ -41,6 +42,7 @@ public class MyBatisExceptionTranslator implements PersistenceExceptionTranslato
4142

4243
private final Supplier<SQLExceptionTranslator> exceptionTranslatorSupplier;
4344
private SQLExceptionTranslator exceptionTranslator;
45+
private ReentrantLock lock = new ReentrantLock();
4446

4547
/**
4648
* Creates a new {@code PersistenceExceptionTranslator} instance with {@code SQLErrorCodeSQLExceptionTranslator}.
@@ -104,9 +106,14 @@ public DataAccessException translateExceptionIfPossible(RuntimeException e) {
104106
/**
105107
* Initializes the internal translator reference.
106108
*/
107-
private synchronized void initExceptionTranslator() {
108-
if (this.exceptionTranslator == null) {
109-
this.exceptionTranslator = exceptionTranslatorSupplier.get();
109+
private void initExceptionTranslator() {
110+
lock.lock();
111+
try {
112+
if (this.exceptionTranslator == null) {
113+
this.exceptionTranslator = exceptionTranslatorSupplier.get();
114+
}
115+
} finally {
116+
lock.unlock();
110117
}
111118
}
112119

0 commit comments

Comments
 (0)