Author: tv Date: Fri Sep 9 13:25:02 2016 New Revision: 1760015 URL: http://svn.apache.org/viewvc?rev=1760015&view=rev Log: TORQUE-347: Use ConcurrentMaps for databases, managers and peers Remove duplicate code
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java?rev=1760015&r1=1760014&r2=1760015&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java (original) +++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java Fri Sep 9 13:25:02 2016 @@ -22,11 +22,12 @@ package org.apache.torque; import java.sql.Connection; import java.sql.SQLException; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; @@ -81,14 +82,14 @@ public class TorqueInstance * and other operations where the database map needs to stay * in a defined state must be synchronized to this map. */ - private final Map<String, Database> databases - = Collections.synchronizedMap(new HashMap<String, Database>()); + private final ConcurrentMap<String, Database> databases + = new ConcurrentHashMap<String, Database>(); /** A repository of Manager instances. */ - private Map<String, AbstractBaseManager<?>> managers; + private ConcurrentMap<String, AbstractBaseManager<?>> managers; /** A repository of Peer instances. */ - private Map<Class<?>, BasePeerImpl<?>> peers; + private ConcurrentMap<Class<?>, BasePeerImpl<?>> peers; /** A repository of idBroker instances. */ private final Set<IDBroker> idBrokers = new HashSet<IDBroker>(); @@ -118,6 +119,19 @@ public class TorqueInstance } /** + * Check if this TorqueInstance has been initialized. + * + * @throws TorqueException if instance is not initialized + */ + private void checkInit() throws TorqueException + { + if (!isInit()) + { + throw new TorqueException("Torque is not initialized."); + } + } + + /** * Initializes this instance of Torque. * * @throws TorqueException if Torque is already initialized @@ -678,9 +692,13 @@ public class TorqueInstance { try { - manager = (AbstractBaseManager<?>) + AbstractBaseManager<?> newManager = (AbstractBaseManager<?>) Class.forName(className).newInstance(); - managers.put(name, manager); + manager = managers.putIfAbsent(name, newManager); + if (manager == null) + { + manager = newManager; + } } catch (Exception e) { @@ -865,42 +883,39 @@ public class TorqueInstance // shut down the data source factories TorqueException exception = null; - synchronized (databases) + for (Map.Entry<String, Database> databaseMapEntry + : databases.entrySet()) { - for (Map.Entry<String, Database> databaseMapEntry - : databases.entrySet()) + Object databaseKey = databaseMapEntry.getKey(); + Database database = databaseMapEntry.getValue(); + if (DEFAULT_NAME.equals(databaseKey) && defaultDsfIsReference) { - Object databaseKey = databaseMapEntry.getKey(); - Database database = databaseMapEntry.getValue(); - if (DEFAULT_NAME.equals(databaseKey) && defaultDsfIsReference) - { - // the DataSourceFactory of the database with the name - // DEFAULT_NAME is just a reference to another entry. - // Do not close because this leads to closing - // the same DataSourceFactory twice. - database.setDataSourceFactory(null); - continue; - } + // the DataSourceFactory of the database with the name + // DEFAULT_NAME is just a reference to another entry. + // Do not close because this leads to closing + // the same DataSourceFactory twice. + database.setDataSourceFactory(null); + continue; + } - try + try + { + DataSourceFactory dataSourceFactory + = database.getDataSourceFactory(); + if (dataSourceFactory != null) { - DataSourceFactory dataSourceFactory - = database.getDataSourceFactory(); - if (dataSourceFactory != null) - { - dataSourceFactory.close(); - database.setDataSourceFactory(null); - } + dataSourceFactory.close(); + database.setDataSourceFactory(null); } - catch (TorqueException e) + } + catch (TorqueException e) + { + log.error("Error while closing the DataSourceFactory " + + databaseKey, + e); + if (exception == null) { - log.error("Error while closing the DataSourceFactory " - + databaseKey, - e); - if (exception == null) - { - exception = e; - } + exception = e; } } } @@ -916,8 +931,8 @@ public class TorqueInstance */ private void resetConfiguration() { - managers = new HashMap<String, AbstractBaseManager<?>>(); - peers = new HashMap<Class<?>, BasePeerImpl<?>>(); + managers = new ConcurrentHashMap<String, AbstractBaseManager<?>>(); + peers = new ConcurrentHashMap<Class<?>, BasePeerImpl<?>>(); isInit = false; } @@ -936,7 +951,7 @@ public class TorqueInstance { throw new TorqueException("Torque is not initialized"); } - return getDatabaseMap(getDefaultDB()); + return getDatabaseMap(name); } /** @@ -955,10 +970,7 @@ public class TorqueInstance { if (name == null) { - if (!Torque.isInit()) - { - throw new TorqueException("Torque is not initialized"); - } + checkInit(); name = getDefaultDB(); } Database database = getOrCreateDatabase(name); @@ -1013,11 +1025,9 @@ public class TorqueInstance public Connection getConnection(final String name) throws TorqueException { - if (!isInit()) - { - throw new TorqueException("Torque is not initialized"); - } - try + checkInit(); + + try { return getDatabase(name) .getDataSourceFactory() @@ -1044,10 +1054,7 @@ public class TorqueInstance public DataSourceFactory getDataSourceFactory(final String name) throws TorqueException { - if (!isInit()) - { - throw new TorqueException("Torque is not initialized"); - } + checkInit(); Database database = getDatabase(name); @@ -1085,10 +1092,8 @@ public class TorqueInstance final String password) throws TorqueException { - if (!isInit()) - { - throw new TorqueException("Torque is not initialized"); - } + checkInit(); + try { return getDataSourceFactory(name) @@ -1112,10 +1117,8 @@ public class TorqueInstance */ public Adapter getAdapter(final String name) throws TorqueException { - if (!isInit()) - { - throw new TorqueException("Torque is not initialized"); - } + checkInit(); + Database database = getDatabase(name); if (database == null) { @@ -1182,11 +1185,9 @@ public class TorqueInstance public String getSchema(final String name) throws TorqueException { - if (!isInit()) - { - throw new TorqueException("Torque is not initialized"); - } - Database database = getDatabase(name); + checkInit(); + + Database database = getDatabase(name); if (database == null) { return null; @@ -1207,11 +1208,9 @@ public class TorqueInstance */ public Database getDatabase(String databaseName) throws TorqueException { - if (!isInit()) - { - throw new TorqueException("Torque is not initialized."); - } - if (databaseName == null) + checkInit(); + + if (databaseName == null) { databaseName = getDefaultDB(); } @@ -1233,10 +1232,8 @@ public class TorqueInstance */ public Map<String, Database> getDatabases() throws TorqueException { - if (!isInit()) - { - throw new TorqueException("Torque is not initialized."); - } + checkInit(); + return Collections.unmodifiableMap(databases); } @@ -1258,15 +1255,18 @@ public class TorqueInstance { throw new NullPointerException("databaseName is null"); } - synchronized (databases) + + Database result = databases.get(databaseName); + if (result == null) { - Database result = databases.get(databaseName); + Database newDatabase = new Database(databaseName); + result = databases.putIfAbsent(databaseName, newDatabase); if (result == null) { - result = new Database(databaseName); - databases.put(databaseName, result); + result = newDatabase; } - return result; } + + return result; } } --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org For additional commands, e-mail: torque-dev-h...@db.apache.org