From 7c74c3a51d28e1ba6027325ef77d32519e48360c Mon Sep 17 00:00:00 2001 From: Alex Huang Date: Tue, 31 May 2011 09:48:16 -0700 Subject: [PATCH] bug 9651: network tagging changes working now --- .../utils/component/ComponentLocator.java | 83 ++++++++++--------- .../com/cloud/utils/db/GenericDaoBase.java | 16 +++- utils/src/com/cloud/utils/db/Transaction.java | 10 +-- 3 files changed, 65 insertions(+), 44 deletions(-) diff --git a/utils/src/com/cloud/utils/component/ComponentLocator.java b/utils/src/com/cloud/utils/component/ComponentLocator.java index a709a02fa5f..8ac6dcc8eba 100755 --- a/utils/src/com/cloud/utils/component/ComponentLocator.java +++ b/utils/src/com/cloud/utils/component/ComponentLocator.java @@ -90,6 +90,7 @@ public class ComponentLocator implements ComponentLocatorMBean { protected static Callback[] s_callbacks = new Callback[] { NoOp.INSTANCE, new DatabaseCallback()}; protected static CallbackFilter s_callbackFilter = new DatabaseCallbackFilter(); protected static final List> s_interceptors = new ArrayList>(); + protected static CleanupThread s_janitor = null; protected HashMap> _adapterMap; protected HashMap> _managerMap; @@ -99,12 +100,18 @@ public class ComponentLocator implements ComponentLocatorMBean { protected HashMap, Class> _factories; static { - Runtime.getRuntime().addShutdownHook(new CleanupThread()); + if (s_janitor == null) { + s_janitor = new CleanupThread(); + Runtime.getRuntime().addShutdownHook(new CleanupThread()); + } } public ComponentLocator(String server) { _serverName = server; - Runtime.getRuntime().addShutdownHook(new CleanupThread()); + if (s_janitor == null) { + s_janitor = new CleanupThread(); + Runtime.getRuntime().addShutdownHook(new CleanupThread()); + } } public String getLocatorName() { @@ -237,7 +244,7 @@ public class ComponentLocator implements ComponentLocatorMBean { if (singleton.state == Singleton.State.Instantiated) { inject(info.clazz, info.instance); singleton.state = Singleton.State.Injected; - } + } if (singleton.state == Singleton.State.Injected) { if (!info.instance.configure(info.name, info.params)) { s_logger.error("Unable to configure DAO: " + info.name); @@ -398,7 +405,7 @@ public class ComponentLocator implements ComponentLocatorMBean { System.exit(1); } s.state = Singleton.State.Configured; - } + } } else { s_logger.info("Configuring Manager: " + info.name); try { @@ -526,7 +533,7 @@ public class ComponentLocator implements ComponentLocatorMBean { s_logger.info("Injecting singleton Adapter: " + info.getName()); inject(info.clazz, info.instance); singleton.state = Singleton.State.Injected; - } + } if (singleton.state == Singleton.State.Injected) { s_logger.info("Configuring singleton Adapter: " + info.getName()); if (!info.instance.configure(info.name, info.params)) { @@ -551,7 +558,7 @@ public class ComponentLocator implements ComponentLocatorMBean { s_logger.error("Unable to configure adapter: " + info.name, e); System.exit(1); } - } + } } } @@ -1015,43 +1022,45 @@ public class ComponentLocator implements ComponentLocatorMBean { protected static class CleanupThread extends Thread { @Override - public synchronized void run() { - for (ComponentLocator locator : s_locators.values()) { - Iterator> itAdapters = locator._adapterMap.values().iterator(); - while (itAdapters.hasNext()) { - Adapters adapters = itAdapters.next(); - itAdapters.remove(); - for (ComponentInfo adapter : adapters._infos) { - if (adapter.singleton) { - Singleton singleton = s_singletons.get(adapter.clazz); - if (singleton.state == Singleton.State.Started) { + public void run() { + synchronized (CleanupThread.class) { + for (ComponentLocator locator : s_locators.values()) { + Iterator> itAdapters = locator._adapterMap.values().iterator(); + while (itAdapters.hasNext()) { + Adapters adapters = itAdapters.next(); + itAdapters.remove(); + for (ComponentInfo adapter : adapters._infos) { + if (adapter.singleton) { + Singleton singleton = s_singletons.get(adapter.clazz); + if (singleton.state == Singleton.State.Started) { + s_logger.info("Asking " + adapter.getName() + " to shutdown."); + adapter.instance.stop(); + singleton.state = Singleton.State.Stopped; + } else { + s_logger.debug("Skippng " + adapter.getName() + " because it has already stopped"); + } + } else { s_logger.info("Asking " + adapter.getName() + " to shutdown."); adapter.instance.stop(); - singleton.state = Singleton.State.Stopped; - } else { - s_logger.debug("Skippng " + adapter.getName() + " because it has already stopped"); } - } else { - s_logger.info("Asking " + adapter.getName() + " to shutdown."); - adapter.instance.stop(); } } } - } - - for (ComponentLocator locator : s_locators.values()) { - Iterator> itManagers = locator._managerMap.values().iterator(); - while (itManagers.hasNext()) { - ComponentInfo manager = itManagers.next(); - itManagers.remove(); - if (manager.singleton == true) { - Singleton singleton = s_singletons.get(manager.clazz); - if (singleton != null && singleton.state == Singleton.State.Started) { - s_logger.info("Asking Manager " + manager.getName() + " to shutdown."); - manager.instance.stop(); - singleton.state = Singleton.State.Stopped; - } else { - s_logger.info("Skipping Manager " + manager.getName() + " because it is not in a state to shutdown."); + + for (ComponentLocator locator : s_locators.values()) { + Iterator> itManagers = locator._managerMap.values().iterator(); + while (itManagers.hasNext()) { + ComponentInfo manager = itManagers.next(); + itManagers.remove(); + if (manager.singleton == true) { + Singleton singleton = s_singletons.get(manager.clazz); + if (singleton != null && singleton.state == Singleton.State.Started) { + s_logger.info("Asking Manager " + manager.getName() + " to shutdown."); + manager.instance.stop(); + singleton.state = Singleton.State.Stopped; + } else { + s_logger.info("Skipping Manager " + manager.getName() + " because it is not in a state to shutdown."); + } } } } diff --git a/utils/src/com/cloud/utils/db/GenericDaoBase.java b/utils/src/com/cloud/utils/db/GenericDaoBase.java index 8b1f14e8f5b..2a31e6f84c6 100755 --- a/utils/src/com/cloud/utils/db/GenericDaoBase.java +++ b/utils/src/com/cloud/utils/db/GenericDaoBase.java @@ -1440,11 +1440,12 @@ public abstract class GenericDaoBase implements Gene EcInfo ec = (EcInfo)attr.attache; Transaction txn = Transaction.currentTxn(); + ResultSet rs = null; PreparedStatement pstmt = null; try { - pstmt = txn.prepareAutoCloseStatement(ec.selectSql); + pstmt = txn.prepareStatement(ec.selectSql); pstmt.setObject(1, _idField.get(entity)); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); ArrayList lst = new ArrayList(); if (ec.targetClass == Integer.class) { while (rs.next()) { @@ -1501,6 +1502,17 @@ public abstract class GenericDaoBase implements Gene throw new CloudRuntimeException("Error executing " + pstmt, e); } catch (IllegalAccessException e) { throw new CloudRuntimeException("Error executing " + pstmt, e); + } finally { + try { + if (rs != null) { + rs.close(); + } + if (pstmt != null) { + pstmt.close(); + } + } catch (SQLException e) { + s_logger.error("Why are we getting an exception at close? ", e); + } } } diff --git a/utils/src/com/cloud/utils/db/Transaction.java b/utils/src/com/cloud/utils/db/Transaction.java index 1ad60816d95..5d846bd8bd7 100755 --- a/utils/src/com/cloud/utils/db/Transaction.java +++ b/utils/src/com/cloud/utils/db/Transaction.java @@ -109,7 +109,7 @@ public class Transaction { // Usage of this transaction setup should be limited, it will always open a new transaction context regardless of whether or not there is other // transaction context in the stack. It is used in special use cases that we want to control DB connection explicitly and in the mean time utilize // the existing DAO features - // + // public static Transaction openNew(final String name, Connection conn) { assert(conn != null); Transaction txn = new Transaction(name, false, CONNECTED_DB); @@ -191,7 +191,7 @@ public class Transaction { s_logger.warn("Unexpected exception: ", e); return null; } - } + } protected void attach(TransactionAttachment value) { _stack.push(new StackElement(ATTACHMENT, value)); @@ -452,7 +452,6 @@ public class Transaction { * @throws SQLException */ public Connection getConnection() throws SQLException { - closePreviousStatement(); if (_conn == null) { if (s_logger.isTraceEnabled()) { s_logger.trace("conn: Creating a DB connection with " + (_txn ? " txn: " : " no txn: ") + buildName()); @@ -653,8 +652,9 @@ public class Transaction { try { s_logger.trace("conn: Closing DB connection"); - if(this._dbId != CONNECTED_DB) - _conn.close(); + if(this._dbId != CONNECTED_DB) { + _conn.close(); + } _conn = null; } catch (final SQLException e) {