Linux Systems Administrator
Jupitermedia
US-AZ-Tucson

Justtechjobs.com Post A Job | Post A Resume

Best Practices: Database Transactions
Last week I talked about how to best use indices in your database queries. Now let's talk about transactions. Transactions may sound reasonably straightforward, as indexing does, but there are a lot of things you can do wrong which will haunt you in certain circumstances.
The general idea behind a transaction is that several steps can be performed in series, with the capability to undo all of the steps at once if needed. In addition, the transaction should happen inside an isolated world where other transactions cannot change data while your transaction is running.
Transactions
If you're one of the 6 or 7 people who didn't buy Linux Journal just to read my article ;-), then you may not know how to properly use transactions or why you should use them.
When you are used to building sites and not using transactions, you work around their absence in creative ways. For instance, I have a lot of random cron jobs that run on SourceForge to munge data in interesting ways. Some of that munging can take quite a while, and you don't want your site to be out of business while it happens. So with MySQL, I would create temp tables to munge data in, then swap the temp table for the permanent one when I'm done. This did the trick - mostly. But for the brief instance while I was swapping tables, the site could be hosed.
If you live in a world where you have transactions, you can wrap the entire process in a begin...commit block and your site won't be out of business for even one microsecond.
So rather than munging data in a temp table, I start a transaction, delete all the data from the live table, recreate it, then commit my changes. The entire process happens in its own little universe right up until I commit them, and then the rest of the world can see the changes.
Like proper indexing, proper transaction use is one or two degrees more complex than you may have bargained for. To do things 100% correctly, you may have to wrap entire blocks of selects and updates inside the same transaction.
[ Next Page ]


Comments:
full theory about database transactions ??Girish Agarwal01/27/05 00:36
RE: excellent articleLance Edusei07/14/04 08:30
RE: MySQL TransactionsRob Limbrey01/21/03 08:45
MySQL TransactionsImtiaz10/16/02 06:22
MULTIPLE TABLES OR HUGE TABLES-BETTER OPTION?Kapil10/06/02 10:48
please found out my code mistakesMary Pei10/04/02 05:44
project work please help usmike smith09/11/02 06:51
RE: Transaction in MySQLPedro Salgado08/13/02 07:12
PostgreSQL nested transactionsLonny L GRanstrom08/09/02 22:36
RE: How to trigger RollbackVinco07/07/02 01:55
How to trigger RollbackIgor04/25/02 23:46
What about batch updates?pedro03/10/02 20:41
LDAP "Rollback"Fraser02/06/02 20:57
nested transactions - my solutionShivers08/17/01 12:57
PHP, MSSQL7 and TransactionsAndrew Prior04/25/01 05:47
XML and pgSQLmoses03/06/01 14:09
RE: transactions...george02/07/01 06:29
transactions...michael kristopeit02/05/01 11:35
RE: MySQL TransactionsRyan Ayers01/30/01 22:41
Clean solution to nested transactionsJeffrey Greer01/28/01 16:43
RE: sessions and DB transactionsKai Meder01/27/01 18:37
RE: MySQL TransactionsKai Meder01/27/01 18:35
sessions and DB transactionsJohn Harnett01/26/01 23:17
nextval/currval in postgresDon Baccus01/26/01 20:54
Transactions in functions vs. PostgresDon Baccus01/26/01 20:46
Re: I hate duplicate functions.Kirk Parker01/24/01 19:20
RE: nested xactionsPeter Moulding01/24/01 17:24
RE: excellent articlePeter Moulding01/24/01 17:18
RE: Transaction in MySQLMattias01/23/01 05:28
RE: nested xactionsJBW01/22/01 23:37
Transaction in MySQLHabibi01/22/01 18:22
MySQL TransactionsRyan Ayers01/22/01 14:30
excellent articleRyan McGeary01/22/01 09:35
nested xactionsJim G01/22/01 08:48
IdeasAndrew Coldham01/22/01 03:49
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.