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