Question

I am setting category and subcategory of an entity so the subcategory depends on category. While setting categories, I need to ensure that either the complete transaction is successful or complete transaction is failed. If I use 2 queries together, it will still execute one by one and thus this is possible that one insert is successful and other is not. Is there any good way of maintaining atomicity in php or mysqli?

Was it helpful?

Solution

No, because those databases are completely unrelated and there's no such thing as a cross-database transaction. At least, not that I know of. :)

But you are a little bit safer if you use transactions for your inserts. Start a transaction on both databases, do all modifications on both databases, and only afterwards commit both transactions. The insert/update statements will already have run (and probably have failed if there were problems like constraint errors), so at the point where you're about to commit, there's not much that's likely to go wrong.

Make sure your tables are in a storage type that supports transactions.

[edit]

You can use

START TRANSACTION;

-- Your queries go here

COMMIT

To execute statements within a transaction.

For more info, read: http://dev.mysql.com/doc/refman/5.0/en/commit.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top