00001
00025
package org.objectweb.cjdbc.controller.core.shutdown;
00026
00027
import java.sql.SQLException;
00028
import java.util.ArrayList;
00029
import java.util.Date;
00030
00031
import org.objectweb.cjdbc.common.exceptions.ShutdownException;
00032
import org.objectweb.cjdbc.common.i18n.Translate;
00033
import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
00034
import org.objectweb.cjdbc.controller.virtualdatabase.DistributedVirtualDatabase;
00035
import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
00036
import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabaseWorkerThread;
00037
00046 public class VirtualDatabaseShutdownThread extends ShutdownThread
00047 {
00048 VirtualDatabase
database;
00049 int shutdownLevel;
00050
00057 public VirtualDatabaseShutdownThread(VirtualDatabase db,
int ilevel)
00058 {
00059 super(ilevel,
ShutdownThread.DATABASE, db.getName());
00060
this.database = db;
00061
shutdownLevel = ilevel;
00062 }
00063
00067 public void shutdownFast() throws
ShutdownException
00068 {
00069
terminateClients();
00070
closeVirtualDatabase();
00071
shutdownBackends(
"Fast Shutdown " +
new Date());
00072 }
00073
00077 public void shutdownImmediate() throws
ShutdownException
00078 {
00079
closeVirtualDatabase();
00080 }
00081
00085 public void shutdownSmart() throws
ShutdownException
00086 {
00087
closeVirtualDatabase();
00088
waitForDisconnectClients();
00089
shutdownBackends(
"Smart Shutdown " +
new Date());
00090 }
00091
00092 private void terminateClients()
00093 {
00094 logger.
info(
"Shutting down clients");
00095 ArrayList threads =
database.getActiveThreads();
00096 logger.
info(
Translate.get(
"controller.shutdown.active.threads",
""
00097 + threads.size()));
00098
VirtualDatabaseWorkerThread wt;
00099
synchronized (threads)
00100 {
00101
for (
int i = 0; i < threads.size(); i++)
00102 {
00103 logger.
info(
Translate.get(
"controller.shutdown.database.thread",
00104
new String[]{
database.getName(),
"" + i}));
00105 wt = ((
VirtualDatabaseWorkerThread) threads.get(i));
00106 wt.
shutdown();
00107 }
00108 }
00109 database.getActiveThreads().clear();
00110 }
00111
00112 private void closeVirtualDatabase()
00113 {
00114
if (database.isDistributed())
00115 {
00116 logger.info(
"Shutting down group communication");
00117 ((
DistributedVirtualDatabase) database).quitChannel();
00118 }
00119 }
00120
00121 private void waitForDisconnectClients()
00122 {
00123
boolean wait =
true;
00124
while (wait)
00125 {
00126 ArrayList threads = database.getActiveThreads();
00127
synchronized (threads)
00128 {
00129
int nbThreads = threads.size();
00130 logger.debug(
Translate.get(
"controller.shutdown.active.threads",
""
00131 + nbThreads));
00132
if (nbThreads == 0)
00133 {
00134 wait =
false;
00135 }
00136 }
00137
if (wait)
00138 {
00139
synchronized (
this)
00140 {
00141
try
00142 {
00143 wait(1000);
00144 }
00145
catch (InterruptedException e)
00146 {
00147
00148 }
00149 }
00150 }
00151 }
00152 }
00153
00154 private void shutdownBackends(String checkpoint)
00155 {
00156
if (database.getRequestManager().getRecoveryLog() != null)
00157 {
00158
try
00159 {
00160
00161 database.storeBackendsInfo();
00162 database.disableAllBackendForCheckpoint(checkpoint);
00163
00164 }
00165
catch (Exception ve)
00166 {
00167 logger.error(
Translate
00168 .get(
"controller.shutdown.backends.exception", ve));
00169 }
00170 }
00171
else
00172 {
00173
00174
try
00175 {
00176 database.disableAllBackend();
00177 }
00178
catch (Exception vde)
00179 {
00180 logger.error(
Translate.get(
"controller.shutdown.backends.exception",
00181 vde));
00182 }
00183 }
00184
00185 ArrayList backends = database.getBackends();
00186
int size = backends.size();
00187
DatabaseBackend backend;
00188
for (
int i = 0; i < size; i++)
00189 {
00190 backend = (
DatabaseBackend) backends.get(i);
00191
try
00192 {
00193 backend.
finalizeConnections();
00194 }
00195
catch (SQLException e)
00196 {
00197 logger
00198 .error(
Translate.get(
"controller.shutdown.backends.exception", e));
00199 }
00200 }
00201 }
00202
00203 }