00001
00025
package org.objectweb.cjdbc.controller.loadbalancer.tasks;
00026
00027
import java.sql.Connection;
00028
import java.sql.SQLException;
00029
00030
import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
00031
import org.objectweb.cjdbc.common.sql.StoredProcedure;
00032
import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
00033
import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache;
00034
import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager;
00035
import org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer;
00036
import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread;
00037
import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet;
00038
00045 public class ReadStoredProcedureTask extends AbstractTask
00046 {
00047 private StoredProcedure
proc;
00048 private ControllerResultSet
result;
00049 private MetadataCache
metadataCache;
00050
00059 public ReadStoredProcedureTask(
int nbToComplete,
int totalNb,
00060 StoredProcedure proc, MetadataCache metadataCache)
00061 {
00062 super(nbToComplete, totalNb);
00063
this.proc = proc;
00064
this.metadataCache = metadataCache;
00065 }
00066
00074 public void execute(
BackendWorkerThread backendThread)
throws SQLException
00075 {
00076
DatabaseBackend backend = backendThread.getBackend();
00077
00078
AbstractConnectionManager cm = backend
00079 .
getConnectionManager(
proc.getLogin());
00080
if (cm == null)
00081 {
00082 SQLException se =
new SQLException(
00083
"No Connection Manager for Virtual Login:" +
proc.getLogin());
00084
try
00085 {
00086
notifyFailure(backendThread, 1, se);
00087 }
00088
catch (SQLException ignore)
00089 {
00090
00091 }
00092
throw se;
00093 }
00094
00095
if (
proc.isAutoCommit())
00096 {
00097 Connection c = null;
00098
try
00099 {
00100 c = cm.
getConnection();
00101 }
00102
catch (
UnreachableBackendException e1)
00103 {
00104 SQLException se =
new SQLException(
"Backend " + backend.
getName()
00105 +
" is no more reachable.");
00106
try
00107 {
00108
notifyFailure(backendThread, 1, se);
00109 }
00110
catch (SQLException ignore)
00111 {
00112 }
00113 backendThread.getLogger().error(
00114
"Disabling backend " + backend.
getName()
00115 +
" because it is no more reachable.");
00116 backend.
disable();
00117
throw se;
00118 }
00119
00120
00121
if (c == null)
00122 {
00123 SQLException se =
new SQLException(
"No more connections");
00124
try
00125 {
00126
if (!
notifyFailure(backendThread, (
long)
proc.getTimeout() * 1000, se))
00127
return;
00128 }
00129
catch (SQLException ignore)
00130 {
00131 }
00132
00133
00134 backendThread.kill();
00135 String msg =
"Stored procedure '"
00136 +
proc.getSQLShortForm(backend.
getSQLShortFormLength())
00137 +
"' failed on backend " + backend.
getName() +
" but "
00138 +
getSuccess() +
" succeeded (" + se +
")";
00139 backendThread.getLogger().error(msg);
00140
throw new SQLException(msg);
00141 }
00142
00143
00144
try
00145 {
00146
result =
AbstractLoadBalancer.executeReadStoredProcedureOnBackend(
proc,
00147 backend, c,
metadataCache);
00148 }
00149
catch (Exception e)
00150 {
00151
try
00152 {
00153
if (!
notifyFailure(backendThread, (
long)
proc.getTimeout() * 1000, e))
00154
return;
00155 }
00156
catch (SQLException ignore)
00157 {
00158 }
00159
00160
00161 backendThread.kill();
00162 String msg =
"Stored procedure '"
00163 +
proc.getSQLShortForm(backend.
getSQLShortFormLength())
00164 +
"' failed on backend " + backend.
getName() +
" but "
00165 +
getSuccess() +
" succeeded (" + e +
")";
00166 backendThread.getLogger().error(msg);
00167
throw new SQLException(msg);
00168 }
00169 finally
00170 {
00171 cm.
releaseConnection(c);
00172 }
00173 }
00174
else
00175 {
00176 Connection c;
00177
long tid =
proc.getTransactionId();
00178 Long lTid =
new Long(tid);
00179
00180
if (!backend.
isStartedTransaction(lTid))
00181 {
00182
try
00183 {
00184 c = cm.
getConnection(tid);
00185 }
00186
catch (
UnreachableBackendException e1)
00187 {
00188 SQLException se =
new SQLException(
"Backend " + backend.
getName()
00189 +
" is no more reachable.");
00190
try
00191 {
00192
notifyFailure(backendThread, 1, se);
00193 }
00194
catch (SQLException ignore)
00195 {
00196 }
00197 backendThread.getLogger().error(
00198
"Disabling backend " + backend.
getName()
00199 +
" because it is no more reachable.");
00200 backend.
disable();
00201
throw se;
00202 }
00203
00204
00205
if (c == null)
00206 {
00207 SQLException se =
new SQLException(
00208
"Unable to get connection for transaction " + tid);
00209
try
00210 {
00211
if (!
notifyFailure(backendThread, (
long)
proc.getTimeout() * 1000,
00212 se))
00213
return;
00214 }
00215
catch (SQLException ignore)
00216 {
00217 }
00218
00219
00220 backendThread.kill();
00221 String msg =
"Stored procedure '"
00222 +
proc.getSQLShortForm(backend.
getSQLShortFormLength())
00223 +
"' failed on backend " + backend.
getName() +
" but "
00224 +
getSuccess() +
" succeeded (" + se +
")";
00225 backendThread.getLogger().error(msg);
00226
throw new SQLException(msg);
00227 }
00228
00229
00230 backend.
startTransaction(lTid);
00231 c.setAutoCommit(
false);
00232 }
00233
else
00234 {
00235 c = cm.
retrieveConnection(tid);
00236
00237
00238
if (c == null)
00239 {
00240 SQLException se =
new SQLException(
00241
"Unable to retrieve connection for transaction " + tid);
00242
try
00243 {
00244
if (!
notifyFailure(backendThread, (
long)
proc.getTimeout() * 1000,
00245 se))
00246
return;
00247 }
00248
catch (SQLException ignore)
00249 {
00250 }
00251
00252
00253 backendThread.kill();
00254 String msg =
"Stored procedure '"
00255 +
proc.getSQLShortForm(backend.
getSQLShortFormLength())
00256 +
"' failed on backend " + backend.
getName() +
" but "
00257 +
getSuccess() +
" succeeded (" + se +
")";
00258 backendThread.getLogger().error(msg);
00259
throw new SQLException(msg);
00260 }
00261 }
00262
00263
00264
try
00265 {
00266
result =
AbstractLoadBalancer.executeReadStoredProcedureOnBackend(
proc,
00267 backend, c,
metadataCache);
00268
00269
00270
00271 }
00272
catch (Exception e)
00273 {
00274
try
00275 {
00276
if (!
notifyFailure(backendThread, (
long)
proc.getTimeout() * 1000, e))
00277
return;
00278 }
00279
catch (SQLException ignore)
00280 {
00281 }
00282
00283
00284 backendThread.kill();
00285 String msg =
"Stored procedure '"
00286 +
proc.getSQLShortForm(backend.
getSQLShortFormLength())
00287 +
"' failed on backend " + backend.
getName() +
" but "
00288 +
getSuccess() +
" succeeded (" + e +
")";
00289 backendThread.getLogger().error(msg);
00290
throw new SQLException(msg);
00291 }
00292 }
00293
notifySuccess();
00294 }
00295
00301 public ControllerResultSet
getResult()
00302 {
00303
return result;
00304 }
00305
00309 public String
toString()
00310 {
00311
return "ReadStoredProcedureTask (" +
proc.getSQL() +
")";
00312 }
00313 }