Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TDbTransaction class file
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TDbTransaction.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data
11
 */
12
 
13
Prado::using('System.Data.TDbDataReader');
14
 
15
/**
16
 * TDbTransaction class.
17
 *
18
 * TDbTransaction represents a DB transaction.
19
 * It is usually created by calling {@link TDbConnection::beginTransaction}.
20
 *
21
 * The following code is a common scenario of using transactions:
22
 * <code>
23
 * try
24
 * {
25
 *    $transaction=$connection->beginTransaction();
26
 *    $connection->createCommand($sql1)->execute();
27
 *    $connection->createCommand($sql2)->execute();
28
 *    //.... other SQL executions
29
 *    $transaction->commit();
30
 * }
31
 * catch(Exception $e)
32
 * {
33
 *    $transaction->rollBack();
34
 * }
35
 * </code>
36
 *
37
 * @author Qiang Xue <qiang.xue@gmail.com>
38
 * @version $Id: TDbTransaction.php 2541 2008-10-21 15:05:13Z qiang.xue $
39
 * @package System.Data
40
 * @since 3.0
41
 */
42
class TDbTransaction extends TComponent
43
{
44
	private $_connection=null;
45
	private $_active;
46
 
47
	/**
48
	 * Constructor.
49
	 * @param TDbConnection the connection associated with this transaction
50
	 * @see TDbConnection::beginTransaction
51
	 */
52
	public function __construct(TDbConnection $connection)
53
	{
54
		$this->_connection=$connection;
55
		$this->setActive(true);
56
	}
57
 
58
	/**
59
	 * Commits a transaction.
60
	 * @throws TDbException if the transaction or the DB connection is not active.
61
	 */
62
	public function commit()
63
	{
64
		if($this->_active && $this->_connection->getActive())
65
		{
66
			$this->_connection->getPdoInstance()->commit();
67
			$this->_active=false;
68
		}
69
		else
70
			throw new TDbException('dbtransaction_transaction_inactive');
71
	}
72
 
73
	/**
74
	 * Rolls back a transaction.
75
	 * @throws TDbException if the transaction or the DB connection is not active.
76
	 */
77
	public function rollback()
78
	{
79
		if($this->_active && $this->_connection->getActive())
80
		{
81
			$this->_connection->getPdoInstance()->rollBack();
82
			$this->_active=false;
83
		}
84
		else
85
			throw new TDbException('dbtransaction_transaction_inactive');
86
	}
87
 
88
	/**
89
	 * @return TDbConnection the DB connection for this transaction
90
	 */
91
	public function getConnection()
92
	{
93
		return $this->_connection;
94
	}
95
 
96
	/**
97
	 * @return boolean whether this transaction is active
98
	 */
99
	public function getActive()
100
	{
101
		return $this->_active;
102
	}
103
 
104
	/**
105
	 * @param boolean whether this transaction is active
106
	 */
107
	protected function setActive($value)
108
	{
109
		$this->_active=TPropertyValue::ensureBoolean($value);
110
	}
111
}
112