Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace synchronized method with Lock #1039

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading