00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00034
00035
00036
00037
00038
00039
00040
00041 public class CommitTask extends AbstractTask
00042 {
00043
00044 private String login;
00045
00046
00047 private long transactionId;
00048
00049
00050 private long timeout;
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 public CommitTask(int nbToComplete, int totalNb, long timeout, String login,
00062 long transactionId)
00063 {
00064 super(nbToComplete, totalNb);
00065 this.login = login;
00066 this.transactionId = transactionId;
00067 this.timeout = timeout;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 public void executeTask(BackendWorkerThread backendThread) throws SQLException
00077 {
00078 DatabaseBackend db = backendThread.getBackend();
00079 Long lTid = new Long(transactionId);
00080
00081 AbstractConnectionManager cm = db.getConnectionManager(login);
00082 if (cm == null)
00083 {
00084 SQLException se = new SQLException(
00085 "No Connection Manager for Virtual Login:" + login);
00086 try
00087 {
00088 notifyFailure(backendThread, 1, se);
00089 }
00090 catch (SQLException ignore)
00091 {
00092
00093 }
00094 throw se;
00095 }
00096 Connection c = cm.retrieveConnection(transactionId);
00097
00098
00099 if (c == null)
00100 {
00101 db.stopTransaction(lTid);
00102 SQLException se = new SQLException(
00103 "Unable to retrieve connection for transaction " + transactionId);
00104
00105 try
00106 {
00107 if (!notifyFailure(backendThread, timeout, se))
00108 return;
00109 }
00110 catch (SQLException ignore)
00111 {
00112 }
00113
00114
00115 backendThread.kill();
00116 String msg = "Failed to commit transaction " + transactionId
00117 + " on backend " + db.getName() + " but " + getSuccess()
00118 + " succeeded (" + se + ")";
00119 backendThread.getLogger().error(msg);
00120 throw new SQLException(msg);
00121 }
00122
00123
00124 try
00125 {
00126 c.commit();
00127 c.setAutoCommit(true);
00128 }
00129 catch (Exception e)
00130 {
00131 try
00132 {
00133 if (!notifyFailure(backendThread, timeout, new SQLException(e
00134 .getMessage())))
00135 return;
00136 }
00137 catch (SQLException ignore)
00138 {
00139 }
00140
00141
00142 backendThread.kill();
00143 String msg = "Failed to commit transaction " + transactionId
00144 + " on backend " + db.getName() + " but " + getSuccess()
00145 + " succeeded (" + e + ")";
00146 backendThread.getLogger().error(msg);
00147 throw new SQLException(msg);
00148 }
00149 finally
00150 {
00151 cm.releaseConnection(transactionId);
00152 db.stopTransaction(lTid);
00153 }
00154 notifySuccess();
00155 }
00156
00157
00158
00159
00160 public String toString()
00161 {
00162 return "CommitTask (" + transactionId + ")";
00163 }
00164 }