Consistent fine-tuning of synchronized and concurrent data structures

In particular, avoiding synchronized Sets and Maps wherever possible (preferring a ConcurrentHashMap even instead of a synchronized Set) and specifying appropriate ConcurrentHashMap initial capacities (even if we end up choosing 16).
This commit is contained in:
Juergen Hoeller
2012-12-12 23:46:26 +01:00
parent a2f8902b3a
commit b9df7d68d9
58 changed files with 380 additions and 387 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2012 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.
@@ -16,9 +16,8 @@
package org.springframework.orm.hibernate3;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.FlushMode;
import org.hibernate.Session;
@@ -44,10 +43,10 @@ public class SessionHolder extends ResourceHolderSupport {
private static final Object DEFAULT_KEY = new Object();
/**
* This Map needs to be synchronized because there might be multi-threaded
* This Map needs to be concurrent because there might be multi-threaded
* access in the case of JTA with remote transaction propagation.
*/
private final Map<Object, Session> sessionMap = Collections.synchronizedMap(new HashMap<Object, Session>(1));
private final Map<Object, Session> sessionMap = new ConcurrentHashMap<Object, Session>(1);
private Transaction transaction;
@@ -89,12 +88,10 @@ public class SessionHolder extends ResourceHolderSupport {
}
public Session getAnySession() {
synchronized (this.sessionMap) {
if (!this.sessionMap.isEmpty()) {
return this.sessionMap.values().iterator().next();
}
return null;
if (!this.sessionMap.isEmpty()) {
return this.sessionMap.values().iterator().next();
}
return null;
}
public void addSession(Session session) {
@@ -120,10 +117,8 @@ public class SessionHolder extends ResourceHolderSupport {
}
public boolean doesNotHoldNonDefaultSession() {
synchronized (this.sessionMap) {
return this.sessionMap.isEmpty() ||
(this.sessionMap.size() == 1 && this.sessionMap.containsKey(DEFAULT_KEY));
}
return this.sessionMap.isEmpty() ||
(this.sessionMap.size() == 1 && this.sessionMap.containsKey(DEFAULT_KEY));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -182,10 +182,10 @@ public class PersistenceAnnotationBeanPostProcessor
private transient ListableBeanFactory beanFactory;
private transient final Map<Class<?>, InjectionMetadata> injectionMetadataCache =
new ConcurrentHashMap<Class<?>, InjectionMetadata>();
new ConcurrentHashMap<Class<?>, InjectionMetadata>(64);
private final Map<Object, EntityManager> extendedEntityManagersToClose =
new ConcurrentHashMap<Object, EntityManager>();
new ConcurrentHashMap<Object, EntityManager>(16);
/**