Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CommitTask.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): Julie Marguerite.
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  * Task to commit a transaction.
00036  * 
00037  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00038  * @author <a href="mailto:Julie.Marguerite@inria.fr">Julie Marguerite </a>
00039  * @version 1.0
00040  */
00041 public class CommitTask extends AbstractTask
00042 {
00043   /** Login used by the connection. */
00044   private String login;
00045 
00046   /** Unique transaction identifier. */
00047   private long   transactionId;
00048 
00049   /** Request timeout in milliseconds. */
00050   private long   timeout;
00051 
00052   /**
00053    * Commits a transaction given a login and a transaction id.
00054    * 
00055    * @param nbToComplete number of threads that must succeed before returning
00056    * @param totalNb total number of threads
00057    * @param timeout request timeout in ms
00058    * @param login the login used by the connection
00059    * @param transactionId a unique transaction identifier
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    * Commits a transaction with the given backend thread.
00072    * 
00073    * @param backendThread the backend thread that will execute the task
00074    * @throws SQLException if an error occurs
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     // Sanity check
00099     if (c == null)
00100     { // Bad connection
00101       db.stopTransaction(lTid);
00102       SQLException se = new SQLException(
00103           "Unable to retrieve connection for transaction " + transactionId);
00104 
00105       try
00106       { // All backends failed, just ignore
00107         if (!notifyFailure(backendThread, timeout, se))
00108           return;
00109       }
00110       catch (SQLException ignore)
00111       {
00112       }
00113       // Disable this backend (it is no more in sync) by killing the backend
00114       // thread
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     // Execute Query
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       // Disable this backend (it is no more in sync) by killing the backend
00141       // thread
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    * @see java.lang.Object#toString()
00159    */
00160   public String toString()
00161   {
00162     return "CommitTask (" + transactionId + ")";
00163   }
00164 }

Generated on Mon Apr 11 22:01:30 2005 for C-JDBC by  doxygen 1.3.9.1