With try catch, do I no longer need to use mysql transactions?
三叔2017-06-07 09:25:24
See if your try catch operates on the database. Of course it involves updating and inserting multiple tables. In order to ensure the integrity of the data, transaction operations must be enabled
//開啟事務
try {
//完成提交事務
} catch(throw $e) {
//rollback
}
给我你的怀抱2017-06-07 09:25:24
It depends on what is in your catch. If there is only one sentence after try and an exception is thrown, there will be no commit, OK.
If doOne is executed and you don’t use the transaction, it will autocommit. Do you want to catch doOne and roll it back separately or what to do?
The following are two sentences. If you have 10 sentences, how will you catch them?
try {
doOne
doTwo
} catch(throw $e) {
//rollback
}
天蓬老师2017-06-07 09:25:24
Transaction and try catch
are two different things. The reason why try catch
is used when starting a transaction is because if you use extensions such as pdo
mysqli
, the database error will return an error to php. , let the program executed by PHP terminate, so that the function command of the rollback
step cannot be executed (the program is interrupted when the input is executed), and it is much safer to use try catch
to perform transaction operations
It is necessary to distinguish clearly try catch
is something that belongs to php
java
and other logical operations, while transation
is something that belongs to the database. The two can cooperate but cannot replace each other