Since Kohana 2.x, the development team added nice transaction methods to the database class that are comparable to those of Propel, ADODB, PDO, etc.
To start, commit, and rollback transactions in Kohana 3.x, just call the following functions as needed.
$db->begin(); $db->commit(); $db->rollback();
A common use case with database transactions is to use a try-catch block where you start your transaction at the before the try block, commit it at the end of the try block, and then catch and rollback if an exception is thrown. The code below demonstrates how you would do that in Kohana.
$db = Database::instance(); $db->begin(); try { // insert, update, delete things here // you can also use ORM models, etc. $db->commit(); } catch(Database_Exception $e) { $db->rollback(); }
Back in Kohana 2.x, the database class did not ship with these methods, so you had to execute transaction calls manually. See example below:
$this->db->query("START TRANSACTION"); $this->db->query("COMMIT"); $this->db->query("ROLLBACK");
The manual example above shows how to do it with MySQL. In PostgreSQL, instead of “START TRANSACTION” you would have to do “BEGIN”