The previous sample code can be pseudo-coded with transactions as such: // Start transaction Perhaps read on about ACID and Concurrency Control. See MySQL Transactional Locking with InnoDB and/or PostgreSQL transaction isolation. Table and/or row locking is important to know about as well, especially on high-traffic sites. No data if affected until the transaction is committed. Committing a transaction - Committing all queries within the transaction, ending the transactional state.Rolling back a transaction - Cancelling all queries within the transaction, ending the transactional state.Creating a transaction - Letting the database know that next queries on a connection should be considered part of a transaction.You can either code around that, and every other possible data disparity edge-case in your application, or you can wrap this in a transaction and be done with it! Our Transactional Toolsetĭatabase transactions consist of three possible "tools": You now have an account with no available users, and there is disparity in the database data. If, however, the account was created, but the user was not, then we run into issues. We just need to handle that situation in code (not shown above). In this scenario, the account and user will fail to be created, so there isn't necessarily disparate data in the database. If the account was not created, there's no id available to pass to the user for its account_id field. Two situations can cause issues: Account was not created. 'accountname' => Input::get('accountname'), If this application creates an account and the first user at the same time, you need to handle what happens when the account was created successfuly, but the user is not. Each account can have one or more users associated with it. This is made safe because you can choose to rollback all queries made within the transaction at any time.įor example, let's pretend we have an application which allows the creation of accounts. However, let's review! A transaction gives you the ability to safely perform a set of data-modifying SQL queries (such as insertions, deletions or updates). You may already know what a transaction is. What if we need more power? Let's dig in to see what's going on behind the scenes, and what tools we have to work with Database Transactions in Laravel. Laravel's documentation on Database Transactions describes wrapping our database calls within a closure.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |