postgresql-jdbc.git / summary / log / commit / refs
commit ea4caddf0cea82e59d70e7eb938dd08db55667bb
Author: Philippe Marschall <philippe.marschall@gmail.com>
Commit: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
Date: Sat Dec 26 16:22:12 2015 +0000
refactor: do not synchronize on ConcurrentHashMap
PGPoolingDataSource synchronizes on ConcurrentHashMap. This is
generally discouraged because ConcurrentHashMap has its own
synchronization mechanisms that allow for higher parallelism.
Replace synchronization in PGPoolingDataSource with atomic operations
of ConcurrentHashMap.
Fixes #462, #468
.../org/postgresql/ds/PGPoolingDataSource.java | 30 +++++++++-------------
1 file changed, 12 insertions(+), 18 deletions(-)
diff --git a/pgjdbc/src/main/java/org/postgresql/ds/PGPoolingDataSource.java b/pgjdbc/src/main/java/org/postgresql/ds/PGPoolingDataSource.java
index 603cf9e9..75e0b606 100644
--- a/pgjdbc/src/main/java/org/postgresql/ds/PGPoolingDataSource.java
+++ b/pgjdbc/src/main/java/org/postgresql/ds/PGPoolingDataSource.java
@@ -15,9 +15,9 @@ import org.postgresql.util.PSQLState;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
-import java.util.Map;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
@@ -50,7 +50,7 @@ import javax.sql.PooledConnection;
* implementation may provide advanced features, such as using a single pool
* across all VMs in a cluster.</p>
*
- * <p>This implementation supports JDK 1.3 and higher.</p>
+ * <p>This implementation supports JDK 1.5 and higher.</p>
*
* @author Aaron Mulder (ammulder@chariotsolutions.com)
*/
@@ -58,7 +58,7 @@ public class PGPoolingDataSource
extends BaseDataSource
implements DataSource
{
- protected static Map<String, PGPoolingDataSource> dataSources = new ConcurrentHashMap<String, PGPoolingDataSource>();
+ protected static ConcurrentMap<String, PGPoolingDataSource> dataSources = new ConcurrentHashMap<String, PGPoolingDataSource>();
public static PGPoolingDataSource getDataSource(String name)
{
@@ -270,19 +270,16 @@ public class PGPoolingDataSource
{
return ;
}
- synchronized (dataSources)
+ PGPoolingDataSource previous = dataSources.putIfAbsent(dataSourceName, this);
+ if (previous != null)
{
- if (getDataSource(dataSourceName) != null)
- {
- throw new IllegalArgumentException("DataSource with name '" + dataSourceName + "' already exists!");
- }
- if (this.dataSourceName != null)
- {
- dataSources.remove(this.dataSourceName);
- }
- this.dataSourceName = dataSourceName;
- addDataSource(dataSourceName);
+ throw new IllegalArgumentException("DataSource with name '" + dataSourceName + "' already exists!");
}
+ if (this.dataSourceName != null)
+ {
+ dataSources.remove(this.dataSourceName);
+ }
+ this.dataSourceName = dataSourceName;
}
/**
@@ -408,10 +405,7 @@ public class PGPoolingDataSource
}
protected void removeStoredDataSource() {
- synchronized (dataSources)
- {
- dataSources.remove(dataSourceName);
- }
+ dataSources.remove(dataSourceName);
}
protected void addDataSource(String dataSourceName)
[parent: 00c7eb32a162]