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