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.controller.backend.DatabaseBackend;
00031
import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager;
00032
import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread;
00033
00041 public class BeginTask extends AbstractTask
00042 {
00044 private String
login;
00045
00047 private int transactionId;
00048
00050 private long timeout;
00051
00061 public BeginTask(
int nbToComplete,
int totalNb,
long timeout, String login,
00062
int transactionId)
00063 {
00064 super(nbToComplete, totalNb);
00065
this.login = login;
00066
this.transactionId = transactionId;
00067
this.timeout = timeout;
00068 }
00069
00076 public void execute(
BackendWorkerThread backendThread)
throws SQLException
00077 {
00078
DatabaseBackend backend = backendThread.getBackend();
00079
if (backend.
isDisabling())
00080 {
00081
00082
00083
00084
notifyCompletion();
00085
return;
00086 }
00087
00088
try
00089 {
00090
AbstractConnectionManager cm = backend.
getConnectionManager(
login);
00091
if (cm == null)
00092 {
00093 SQLException se =
new SQLException(
00094
"No Connection Manager for Virtual Login:" +
login);
00095
try
00096 {
00097
notifyFailure(backendThread, 1, se);
00098 }
00099
catch (SQLException ignore)
00100 {
00101
00102 }
00103
throw se;
00104 }
00105
00106 Connection c = cm.
getConnection(
transactionId);
00107
00108
00109
if (c == null)
00110 {
00111 SQLException se =
new SQLException(
00112
"No more connection to start a new transaction.");
00113
try
00114 {
00115
if (!
notifyFailure(backendThread,
timeout, se))
00116
return;
00117 }
00118
catch (SQLException ignore)
00119 {
00120 }
00121 }
00122
00123 c.setAutoCommit(
false);
00124
notifySuccess();
00125 backend.
startTransaction(
new Long(
transactionId));
00126 }
00127
catch (Exception e)
00128 {
00129
try
00130 {
00131
if (!
notifyFailure(backendThread,
timeout,
new SQLException(e
00132 .getMessage())))
00133
return;
00134 }
00135
catch (SQLException ignore)
00136 {
00137 }
00138 String msg =
"Failed to begin transaction " +
transactionId
00139 +
" on backend " + backend.
getName() +
" (" + e +
")";
00140 backendThread.getLogger().error(msg);
00141
throw new SQLException(msg);
00142 }
00143 }
00144
00148 public String
toString()
00149 {
00150
return "BeginTask (" +
transactionId +
")";
00151 }
00152 }