[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: Transaction.php 64 2005-05-13 02:43:56Z root $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://propel.phpdb.org>. 20 */ 21 22 /** 23 * Utility class to make it easier to begin, commit, and rollback transactions. 24 * 25 * This can be used to handle cases where transaction support is optional. 26 * The second parameter of beginOptionalTransaction() will determine with a transaction 27 * is used or not. If a transaction is not used, the commit and rollback methods 28 * do not have any effect. Instead it simply makes the logic easier to follow 29 * by cutting down on the if statements based solely on whether a transaction 30 * is needed or not. 31 * 32 * @author Hans Lellelid <hans@xmpl.org> (Propel) 33 * @author Stephen Haberman <stephenh@chase3000.com> (Torque) 34 * @version $Revision: 64 $ 35 * @package propel.util 36 */ 37 class Transaction { 38 39 /** 40 * Begin a transaction. This method will fallback gracefully to 41 * return a normal connection, if the database being accessed does 42 * not support transactions. 43 * 44 * @param string $dbName Name of database. 45 * @return Connection The Connection for the transaction. 46 * @throws PropelException 47 */ 48 public static function begin($dbName) 49 { 50 $con = Propel::getConnection($dbName); 51 try { 52 $con->setAutoCommit(false); 53 } catch (SQLException $e) { 54 throw new PropelException($e); 55 } 56 return $con; 57 } 58 59 /** 60 * Begin a transaction. This method will fallback gracefully to 61 * return a normal connection, if the database being accessed does 62 * not support transactions. 63 * 64 * @param sring $dbName Name of database. 65 * @param boolean $useTransaction If false, a transaction won't be used. 66 * @return Connection The Connection for the transaction. 67 * @throws PropelException 68 */ 69 public static function beginOptional($dbName, $useTransaction) 70 { 71 $con = Propel::getConnection($dbName); 72 try { 73 if ($useTransaction) { 74 $con->setAutoCommit(false); 75 } 76 } catch (SQLException $e) { 77 throw new PropelException($e); 78 } 79 return $con; 80 } 81 82 /** 83 * Commit a transaction. This method takes care of releasing the 84 * connection after the commit. In databases that do not support 85 * transactions, it only returns the connection. 86 * 87 * @param Connection $con The Connection for the transaction. 88 * @return void 89 * @throws PropelException 90 */ 91 public static function commit($con) 92 { 93 if ($con === null) { 94 throw new PropelException( 95 "Connection object was null. " 96 . "This could be due to a misconfiguration. " 97 . "Check the logs and Propel properties " 98 . "to better determine the cause."); 99 } 100 try { 101 if ($con->getAutoCommit() === false) { 102 $con->commit(); 103 $con->setAutoCommit(true); 104 } 105 } catch (SQLException $e) { 106 throw new PropelException($e); 107 } 108 } 109 110 /** 111 * Roll back a transaction in databases that support transactions. 112 * It also releases the connection. In databases that do not support 113 * transactions, this method will log the attempt and release the 114 * connection. 115 * 116 * @param Connection $con The Connection for the transaction. 117 * @return void 118 * @throws PropelException 119 */ 120 public static function rollback($con) 121 { 122 if ($con === null) { 123 throw new PropelException( 124 "Connection object was null. " 125 . "This could be due to a misconfiguration. " 126 . "Check the logs and Propel properties " 127 . "to better determine the cause."); 128 } 129 130 try { 131 if ($con->getAutoCommit() === false) { 132 $con->rollback(); 133 $con->setAutoCommit(true); 134 } 135 } catch (SQLException $e) { 136 Propel::log( 137 "An attempt was made to rollback a transaction " 138 . "but the database did not allow the operation to be " 139 . "rolled back: " . $e->getMessage(), Propel::LOG_ERR); 140 throw new PropelException($e); 141 } 142 } 143 144 /** 145 * Roll back a transaction without throwing errors if they occur. 146 * 147 * @param Connection $con The Connection for the transaction. 148 * @return void 149 */ 150 public static function safeRollback($con) 151 { 152 try { 153 Transaction::rollback($con); 154 } catch (PropelException $e) { 155 Propel::log("An error occured during rollback: " . $e->getMessage(), Propel::LOG_ERR); 156 } 157 } 158 159 160 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |