我要编写一个较长的sql事务,并考虑编写可重用的sql函数,所以我的问题是我可以这种方式编写事务吗?
try {
// First of all, let's begin a transaction
$db->beginTransaction();
// A set of queries; if one fails, an exception should be thrown
$db->query('first query');
UpdateEmp();
//anothre sql query function, i put him in another function so that i
//can use it seperately
$db->query('second query');
UpdateProject();
$db->query('third query');
// If we arrive here, it means that no exception was thrown
// i.e. no query has failed, and we can commit the transaction
$db->commit();
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$db->rollback();
}
其他sql函数就像
function UpdateEmp(){
//separate sql queries for emplyee update
UpdateEmpProject();//nested sql query function
//Do any db error will stop and rollback the trasaction?
}
function UpdateEmpProject(){
//sepeate sql queries for emplyee update
}
我的问题是,如果UpdateEmp函数中发生任何错误,事务是否会回滚?假设UpdateEmp()还包含另一个带有sql查询的函数,那么嵌套查询也会影响事务结果吗?