Skip to content

Commit

Permalink
Merge pull request #3017 from m1ngyuan/patch-1
Browse files Browse the repository at this point in the history
Remove `synchronized` methods/blocks from the codebase
  • Loading branch information
harawata committed Feb 13, 2024
2 parents 970bde0 + 0949fc7 commit 43b0f8c
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 270 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -135,29 +134,14 @@ public void parse() {
}
}
}
parsePendingMethods();
configuration.parsePendingMethods(false);
}

private static boolean canHaveStatement(Method method) {
// issue #237
return !method.isBridge() && !method.isDefault();
}

private void parsePendingMethods() {
Collection<MethodResolver> incompleteMethods = configuration.getIncompleteMethods();
synchronized (incompleteMethods) {
Iterator<MethodResolver> iter = incompleteMethods.iterator();
while (iter.hasNext()) {
try {
iter.next().resolve();
iter.remove();
} catch (IncompleteElementException e) {
// This method is still missing a resource
}
}
}
}

private void loadXmlResource() {
// Spring may not know the real resource name so we check a flag
// to prevent loading again a resource twice
Expand Down
55 changes: 4 additions & 51 deletions src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,10 +19,8 @@
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -101,9 +99,9 @@ public void parse() {
configuration.addLoadedResource(resource);
bindMapperForNamespace();
}
parsePendingResultMaps();
parsePendingCacheRefs();
parsePendingStatements();
configuration.parsePendingResultMaps(false);
configuration.parsePendingCacheRefs(false);
configuration.parsePendingStatements(false);
}

public XNode getSqlFragment(String refid) {
Expand Down Expand Up @@ -147,51 +145,6 @@ private void buildStatementFromContext(List<XNode> list, String requiredDatabase
}
}

private void parsePendingResultMaps() {
Collection<ResultMapResolver> incompleteResultMaps = configuration.getIncompleteResultMaps();
synchronized (incompleteResultMaps) {
Iterator<ResultMapResolver> iter = incompleteResultMaps.iterator();
while (iter.hasNext()) {
try {
iter.next().resolve();
iter.remove();
} catch (IncompleteElementException e) {
// ResultMap is still missing a resource...
}
}
}
}

private void parsePendingCacheRefs() {
Collection<CacheRefResolver> incompleteCacheRefs = configuration.getIncompleteCacheRefs();
synchronized (incompleteCacheRefs) {
Iterator<CacheRefResolver> iter = incompleteCacheRefs.iterator();
while (iter.hasNext()) {
try {
iter.next().resolveCacheRef();
iter.remove();
} catch (IncompleteElementException e) {
// Cache ref is still missing a resource...
}
}
}
}

private void parsePendingStatements() {
Collection<XMLStatementBuilder> incompleteStatements = configuration.getIncompleteStatements();
synchronized (incompleteStatements) {
Iterator<XMLStatementBuilder> iter = incompleteStatements.iterator();
while (iter.hasNext()) {
try {
iter.next().parseStatementNode();
iter.remove();
} catch (IncompleteElementException e) {
// Statement is still missing a resource...
}
}
}
}

private void cacheRefElement(XNode context) {
if (context != null) {
configuration.addCacheRef(builderAssistant.getCurrentNamespace(), context.getStringAttribute("namespace"));
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/apache/ibatis/cache/decorators/SoftCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.lang.ref.SoftReference;
import java.util.Deque;
import java.util.LinkedList;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.ibatis.cache.Cache;

Expand All @@ -34,6 +35,7 @@ public class SoftCache implements Cache {
private final ReferenceQueue<Object> queueOfGarbageCollectedEntries;
private final Cache delegate;
private int numberOfHardLinks;
private final ReentrantLock lock = new ReentrantLock();

public SoftCache(Cache delegate) {
this.delegate = delegate;
Expand Down Expand Up @@ -74,11 +76,14 @@ public Object getObject(Object key) {
delegate.removeObject(key);
} else {
// See #586 (and #335) modifications need more than a read lock
synchronized (hardLinksToAvoidGarbageCollection) {
lock.lock();
try {
hardLinksToAvoidGarbageCollection.addFirst(result);
if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
hardLinksToAvoidGarbageCollection.removeLast();
}
} finally {
lock.unlock();
}
}
}
Expand All @@ -95,8 +100,11 @@ public Object removeObject(Object key) {

@Override
public void clear() {
synchronized (hardLinksToAvoidGarbageCollection) {
lock.lock();
try {
hardLinksToAvoidGarbageCollection.clear();
} finally {
lock.unlock();
}
removeGarbageCollectedItems();
delegate.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,16 @@
*/
package org.apache.ibatis.cache.decorators;

import java.util.concurrent.locks.ReentrantLock;

import org.apache.ibatis.cache.Cache;

/**
* @author Clinton Begin
*/
public class SynchronizedCache implements Cache {

private final ReentrantLock lock = new ReentrantLock();
private final Cache delegate;

public SynchronizedCache(Cache delegate) {
Expand All @@ -34,28 +37,53 @@ public String getId() {
}

@Override
public synchronized int getSize() {
return delegate.getSize();
public int getSize() {
lock.lock();
try {
return delegate.getSize();
} finally {
lock.unlock();
}
}

@Override
public synchronized void putObject(Object key, Object object) {
delegate.putObject(key, object);
public void putObject(Object key, Object object) {
lock.lock();
try {
delegate.putObject(key, object);
} finally {
lock.unlock();
}
}

@Override
public synchronized Object getObject(Object key) {
return delegate.getObject(key);
public Object getObject(Object key) {
lock.lock();
try {
return delegate.getObject(key);
} finally {
lock.unlock();
}
}

@Override
public synchronized Object removeObject(Object key) {
return delegate.removeObject(key);
public Object removeObject(Object key) {
lock.lock();
try {
return delegate.removeObject(key);
} finally {
lock.unlock();
}
}

@Override
public synchronized void clear() {
delegate.clear();
public void clear() {
lock.lock();
try {
delegate.clear();
} finally {
lock.unlock();
}
}

@Override
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/apache/ibatis/cache/decorators/WeakCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.lang.ref.WeakReference;
import java.util.Deque;
import java.util.LinkedList;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.ibatis.cache.Cache;

Expand All @@ -34,6 +35,7 @@ public class WeakCache implements Cache {
private final ReferenceQueue<Object> queueOfGarbageCollectedEntries;
private final Cache delegate;
private int numberOfHardLinks;
private final ReentrantLock lock = new ReentrantLock();

public WeakCache(Cache delegate) {
this.delegate = delegate;
Expand Down Expand Up @@ -73,11 +75,14 @@ public Object getObject(Object key) {
if (result == null) {
delegate.removeObject(key);
} else {
synchronized (hardLinksToAvoidGarbageCollection) {
lock.lock();
try {
hardLinksToAvoidGarbageCollection.addFirst(result);
if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
hardLinksToAvoidGarbageCollection.removeLast();
}
} finally {
lock.unlock();
}
}
}
Expand All @@ -94,8 +99,11 @@ public Object removeObject(Object key) {

@Override
public void clear() {
synchronized (hardLinksToAvoidGarbageCollection) {
lock.lock();
try {
hardLinksToAvoidGarbageCollection.clear();
} finally {
lock.unlock();
}
removeGarbageCollectedItems();
delegate.clear();
Expand Down
Loading

0 comments on commit 43b0f8c

Please sign in to comment.