1 24 25 package org.objectweb.cjdbc.controller.core.shutdown; 26 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 31 import org.objectweb.cjdbc.common.i18n.Translate; 32 import org.objectweb.cjdbc.controller.cache.result.AbstractResultCache; 33 import org.objectweb.cjdbc.controller.recoverylog.RecoveryLog; 34 import org.objectweb.cjdbc.controller.virtualdatabase.DistributedVirtualDatabase; 35 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase; 36 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabaseWorkerThread; 37 38 46 public abstract class VirtualDatabaseShutdownThread extends ShutdownThread 47 { 48 protected VirtualDatabase virtualDatabase; 49 50 57 public VirtualDatabaseShutdownThread(VirtualDatabase vdb, int level) 58 { 59 super(level); 60 this.virtualDatabase = vdb; 61 } 62 63 68 protected void shutdownCacheRecoveryLogAndGroupCommunication() 69 { 70 AbstractResultCache resultCache = virtualDatabase.getRequestManager() 72 .getResultCache(); 73 if (resultCache != null) 74 resultCache.shutdown(); 75 76 RecoveryLog recoveryLog = virtualDatabase.getRequestManager() 78 .getRecoveryLog(); 79 if (recoveryLog != null) 80 recoveryLog.shutdown(); 81 82 if (virtualDatabase.isDistributed()) 83 { 84 logger.info("Shutting down group communication"); 85 try 86 { 87 ((DistributedVirtualDatabase) virtualDatabase).quitChannel(); 88 } 89 catch (Exception e) 90 { 91 logger 92 .warn( 93 "An error occured while shutting down the group communication channel", 94 e); 95 } 96 } 97 } 98 99 103 protected void disableAllBackends() 104 { 105 if (virtualDatabase.getRequestManager().getRecoveryLog() != null) 106 { 107 try 108 { virtualDatabase.storeBackendsInfo(); 110 virtualDatabase.disableAllBackendsWithCheckpoint(new Date ().toString()); 111 } 112 catch (Exception ve) 113 { 114 logger.error(Translate 115 .get("controller.shutdown.backends.exception", ve)); 116 } 117 } 118 else 119 { try 121 { 122 virtualDatabase.disableAllBackends(); 123 } 124 catch (Exception vde) 125 { 126 logger.error(Translate.get("controller.shutdown.backends.exception", 127 vde)); 128 } 129 } 130 } 131 132 135 protected void terminateVirtualDatabaseWorkerThreads() 136 { 137 ArrayList threads = virtualDatabase.getActiveThreads(); 138 logger.info(Translate.get("controller.shutdown.active.threads", threads 139 .size())); 140 VirtualDatabaseWorkerThread wt; 141 synchronized (threads) 142 { 143 for (int i = 0; i < threads.size(); i++) 144 { 145 if (logger.isDebugEnabled()) 146 logger.debug(Translate.get("controller.shutdown.database.thread", 147 new String []{virtualDatabase.getVirtualDatabaseName(), 148 String.valueOf(i)})); 149 wt = ((VirtualDatabaseWorkerThread) threads.get(i)); 150 wt.shutdown(); 151 } 152 threads.clear(); 153 } 154 155 virtualDatabase.setPoolConnectionThreads(false); 157 ArrayList idleThreads = virtualDatabase.getPendingConnections(); 158 synchronized (idleThreads) 159 { 160 idleThreads.notifyAll(); 161 } 162 } 163 164 168 protected void waitForClientsToDisconnect() 169 { 170 boolean wait = true; 171 while (wait) 172 { 173 ArrayList threads = virtualDatabase.getActiveThreads(); 174 synchronized (threads) 175 { 176 int nbThreads = threads.size(); 177 if (logger.isDebugEnabled()) 178 logger.debug(Translate.get("controller.shutdown.active.threads", 179 nbThreads)); 180 if (nbThreads == 0) 181 wait = false; 182 } 183 if (wait) 184 { 185 synchronized (this) 186 { 187 try 188 { 189 wait(1000); 190 } 191 catch (InterruptedException e) 192 { 193 } 195 } 196 } 197 } 198 } 199 200 204 protected void waitForTransactionsAndWritesToComplete() 205 { 206 try 207 { 208 virtualDatabase.getRequestManager().getScheduler() 209 .suspendNewTransactionsForCheckpoint(); 210 } 211 catch (SQLException e) 212 { 213 logger 214 .error( 215 "An error occured while waiting for current transactions to complete.", 216 e); 217 } 218 try 219 { 220 virtualDatabase.getRequestManager().getScheduler().suspendWrites(); 221 } 222 catch (SQLException e) 223 { 224 logger.error( 225 "An error occured while waiting for pending writes to complete.", e); 226 } 227 } 228 229 } | Popular Tags |