Quantcast

Forums | MacLife

You are not logged in.

#1 2009-04-17 12:02 pm

Alphacow
Genius without a clue
From: Neurosurgery
Registered: 2001-05-23
Posts: 855

mysql transactions

I'm writing an intranet for a medical company, and the code I'm working on now requires nine separate inserts to different tables in order to execute a single action; register a patient. I'm trying to figure out what transactions do and whether it would be beneficial to wrap all the commands in a "begin... commit". I have two questions:

863) if I have a machine-level problem while a transaction (power outage, etc.) is executing, does it automatically roll back, or do I have to worry about that sort of thing myself?
864) if I have a mysql-level problem within a transaction which would cause mysql to throw a error (syntax error, illegal null values,  etc.), will the transaction automatically roll back, or do I have to do it myself?

Thanks!

Offline

 

#2 2009-04-18 11:34 am

Basseq
Fixxy of F&S fame
From: D.C.
Registered: 2002-12-18
Posts: 3125
Website

Re: mysql transactions

My gut feeling is that nothing would be committed until the entire command is finished, so in either case rollback would occur without your intervention. Don't take my word for it though, as I'm a little rusty on the details of MySQL.


Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)

Offline

 

#3 2009-04-18 3:00 pm

resedit
Chicken Little
Royal Wombat
From: /dev/null
Registered: 1999-11-01
Posts: 50393
Website

Re: mysql transactions

Alphacow wrote:

I'm writing an intranet for a medical company, and the code I'm working on now requires nine separate inserts to different tables in order to execute a single action; register a patient. I'm trying to figure out what transactions do and whether it would be beneficial to wrap all the commands in a "begin... commit". I have two questions:

863) if I have a machine-level problem while a transaction (power outage, etc.) is executing, does it automatically roll back, or do I have to worry about that sort of thing myself?
864) if I have a mysql-level problem within a transaction which would cause mysql to throw a error (syntax error, illegal null values,  etc.), will the transaction automatically roll back, or do I have to do it myself?

Thanks!

If I were you and worried about something going squirely before all inserts were done, I'd create a new temporary table as a staging area (by temporary I mean not for the purpose of long term record storage) and do a single insert. Upon success, then insert into the existing table structure, and upon success, delete the record from the temporary table.

That way if something breaks during the process, your temporary table has all the information needed to do the complete registration of the patient.

You can also insert into multiple tables at the same time, at least with some SQL implementations, but I'm not sure that would be safe - still may end up with some partial data.

Last edited by resedit (2009-04-18 3:01 pm)


In her right hand Jenny held the Bible of her mother
Jenny had a pistol in the other
-- Steve Taylor

Offline

 

#4 2009-04-19 1:47 am

Miles
Now I fight for wisdom!
Administrator
From: Michigan
Registered: 2001-07-21
Posts: 4506
Website

Re: mysql transactions

resedit wrote:

If I were you and worried about something going squirely before all inserts were done, I'd create a new temporary table as a staging area (by temporary I mean not for the purpose of long term record storage) and do a single insert. Upon success, then insert into the existing table structure, and upon success, delete the record from the temporary table.

That way if something breaks during the process, your temporary table has all the information needed to do the complete registration of the patient.

You can also insert into multiple tables at the same time, at least with some SQL implementations, but I'm not sure that would be safe - still may end up with some partial data.

Don't do this.  Use transactions.

Offline

 

#5 2009-04-19 3:50 am

Alien
Forum Czar
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16942
Website

Re: mysql transactions

Basseq wrote:

My gut feeling is that nothing would be committed until the entire command is finished, so in either case rollback would occur without your intervention. Don't take my word for it though, as I'm a little rusty on the details of MySQL.

Your gut feeling is correct. Although it depends on which of the machines unexpectedly shuts down (if the database server loses power, there's really no telling what would happen, really), and I couldn't tell you what MySQL's default commit points are.

So insert explicit commit points into your code at strategic, logical locations.

.tsooJ


http://macstack.net/forums/images/smilies/lol.gif

Offline

 

#6 2009-04-19 8:57 am

Booksley
Zombie Genocidest
From: Toronto, Ontario
Registered: 2001-02-16
Posts: 5037

Re: mysql transactions

1) Use transactions
2) You must use the InnoDB engine
3) Read this and this.

Offline

 

#7 2009-04-24 9:43 am

Alphacow
Genius without a clue
From: Neurosurgery
Registered: 2001-05-23
Posts: 855

Re: mysql transactions

Thanks for the replies. (Sorry for the delay in my response; this is a once-a-week job.) I was hoping to avoid needing to learn how to do "error checking" to make sure the inserts were completed correctly, but I guess that'll be a necessity here.

Offline

 

#8 2009-04-27 10:18 pm

resedit
Chicken Little
Royal Wombat
From: /dev/null
Registered: 1999-11-01
Posts: 50393
Website

Re: mysql transactions

Alphacow wrote:

Thanks for the replies. (Sorry for the delay in my response; this is a once-a-week job.) I was hoping to avoid needing to learn how to do "error checking" to make sure the inserts were completed correctly, but I guess that'll be a necessity here.

Use prepared statements and if necessary, a regex check to make sure the arguments match the data types specified in the prepared statements.

You need MySQL 4.1 or newer for prepared statements, but I believe you need a similar version of MySQL to use transactions anyway.


In her right hand Jenny held the Bible of her mother
Jenny had a pistol in the other
-- Steve Taylor

Offline

 

Board footer

Powered by PunBB 1.2.6
© Copyright 2002–2005 Rickard Andersson