473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

rolling back a database

Hello,

Say I have a function with 5 MySQL queries that all insert or update the
database. Now let's assume that during execution, the first 3 queries
are executed fine but then the 4th one dies for some reason - problem
with the database, connection, whatever.

Obviously I can test the $result variable to see if it returns true or
false, but given the case above, the database is now left in a "corrupt"
state (assuming all 5 queries have to succeed in order for the database
to be correct).

So, if I receive false for any of the results, is there an efficient way
to "roll-back" the database to its original state? Specifically in the
above example, is there a way to undo the first 3 queries?

I thought of setting flags in an array so that, for example, if query 4
dies, I can automatically undo queries 1 through 3 with new queries that
would return the database to its original form, but if there is a
problem that prevented query 4 from executing, I don't see how I can
guarantee that I would be able to successfully execute new queries to
undo the first 3.

Thanks in advance.
Jul 17 '05 #1
9 1702
Marcus (Ju********@aol.com) wrote:
: Hello,

: Say I have a function with 5 MySQL queries that all insert or update the
: database. Now let's assume that during execution, the first 3 queries
: are executed fine but then the 4th one dies for some reason - problem
: with the database, connection, whatever.

: Obviously I can test the $result variable to see if it returns true or
: false, but given the case above, the database is now left in a "corrupt"
: state (assuming all 5 queries have to succeed in order for the database
: to be correct).

: So, if I receive false for any of the results, is there an efficient way
: to "roll-back" the database to its original state? Specifically in the
: above example, is there a way to undo the first 3 queries?

: I thought of setting flags in an array so that, for example, if query 4
: dies, I can automatically undo queries 1 through 3 with new queries that
: would return the database to its original form, but if there is a
: problem that prevented query 4 from executing, I don't see how I can
: guarantee that I would be able to successfully execute new queries to
: undo the first 3.

(*) The "easiest" solution is to use a database like Oracle ($) or
PostgreSQL (free), or MSSQL (if you're on a windows server that has it).
This sort of thing is handled automatically by simply defining a
transaction for the set of related queries.

The mysql web pages had (probably still has) suggestions for how to best
do this sort of thing in mysql, though it's all kludgey.

I suppose you could do something like write all your steps to a process
specific file ahead of time, and then log each step as it runs, then the
last step would be to remove the file. If any files are left around then
you would know something went wrong, and also know what was supposed to
have happened. The processes could also query the original data ahead of
time and save that in the file too which might be useful. If that sounds
inefficient or kludgey then goto (*) above.

--

This space not for rent.
Jul 17 '05 #2
yf***@vtn1.victoria.tc.ca (Malcolm Dew-Jones) wrote in
news:42******@news.victoria.tc.ca:
The mysql web pages had (probably still has) suggestions for how to
best do this sort of thing in mysql, though it's all kludgey.


Kludgey? Not at all.

To the original poster, yes you can do this in MySQL. It is called using
"transactions", and has been available from MySQL 4 onwards. All that is
involved is using InnoDB tables instead of MyISAM tables.

http://dev.mysql.com/doc/mysql/en/an...nsactions.html

http://www.devarticles.com/c/a/MySQL...MySQL-4.0-and-
PHP/

http://www.databasejournal.com/featu...le.php/3382171
Jul 17 '05 #3
>Say I have a function with 5 MySQL queries that all insert or update the
database. Now let's assume that during execution, the first 3 queries
are executed fine but then the 4th one dies for some reason - problem
with the database, connection, whatever.
Use MySQL transactions, however, that is unlikely to help with
problems with the database CONNECTION. It does help with
errors such as the user name the user wanted is already taken
(and inserting the record fails due to a unique index), or
detected problems like a bank balance going negative.

You need to be using a high enough MySQL version and appropriate table
type to use transactions (e.g. MySQL 4.1 and InnoDB table type
works, although I believe there are some versions earlier than 4.1
which also work).
Obviously I can test the $result variable to see if it returns true or
false, but given the case above, the database is now left in a "corrupt"
state (assuming all 5 queries have to succeed in order for the database
to be correct).

So, if I receive false for any of the results, is there an efficient way
to "roll-back" the database to its original state? Specifically in the
above example, is there a way to undo the first 3 queries?
Start a transaction, proceed with the queries. If you are satisfied
with the result, commit the result. If you are not, roll it back.
Assuming you have autocommit turned off (normal when using transactions),
if the connection breaks, the last (partial) transaction is rolled back.
I thought of setting flags in an array so that, for example, if query 4
dies, I can automatically undo queries 1 through 3 with new queries that
would return the database to its original form, but if there is a
problem that prevented query 4 from executing, I don't see how I can
guarantee that I would be able to successfully execute new queries to
undo the first 3.


Transactions will not protect you against things like bad disk sectors
on the MySQL server, network interruptions, or system crashes. It
does allow you to roll back partially-completed transactions due to
things like bad data entered, unique key violations, etc.

Gordon L. Burditt
Jul 17 '05 #4
Good Man wrote:
yf***@vtn1.victoria.tc.ca (Malcolm Dew-Jones) wrote in
news:42******@news.victoria.tc.ca:

The mysql web pages had (probably still has) suggestions for how to
best do this sort of thing in mysql, though it's all kludgey.

Kludgey? Not at all.

To the original poster, yes you can do this in MySQL. It is called using
"transactions", and has been available from MySQL 4 onwards. All that is
involved is using InnoDB tables instead of MyISAM tables.

http://dev.mysql.com/doc/mysql/en/an...nsactions.html

http://www.devarticles.com/c/a/MySQL...MySQL-4.0-and-
PHP/

http://www.databasejournal.com/featu...le.php/3382171


Hello,

Thank you all for the replies. I have been reading up on InnoDB tables
per your suggestions, and it looks like this is exactly what I was
looking for.

I was wondering if anyone has had any experience with InnoDB tables and
if anyone knows if they are more efficient in terms of
storage/speed/security or any other major aspects than MyISAM. I
remember reading somewhere awhile ago that MyISAM was the most efficient
table type available to MySQL, but that was obviously before 4.1 onwards.

Lastly, I am running 4.1 and was just wondering if changing existing
table types to InnoDB would have any adverse affects, or if the DB will
rearrange all the data on disk without any complications. I just want
to be sure I don't mess up data on my live server by changing the structure.

Thank you again in advance.
Jul 17 '05 #5
Good Man (he***@letsgo.com) wrote:
: yf***@vtn1.victoria.tc.ca (Malcolm Dew-Jones) wrote in
: news:42******@news.victoria.tc.ca:

: > The mysql web pages had (probably still has) suggestions for how to
: > best do this sort of thing in mysql, though it's all kludgey.

: Kludgey? Not at all.

: To the original poster, yes you can do this in MySQL. It is called using
: "transactions", and has been available from MySQL 4 onwards. All that is
: involved is using InnoDB tables instead of MyISAM tables.

: http://dev.mysql.com/doc/mysql/en/an...nsactions.html

: http://www.devarticles.com/c/a/MySQL...MySQL-4.0-and-
: PHP/

: http://www.databasejournal.com/featu...le.php/3382171
Well, by bad.

A welcome addition to mysql.
--

This space not for rent.
Jul 17 '05 #6
On Thu, 21 Apr 2005 02:59:49 +0000, Marcus wrote:
Good Man wrote:
yf***@vtn1.victoria.tc.ca (Malcolm Dew-Jones) wrote in
news:42******@news.victoria.tc.ca:

The mysql web pages had (probably still has) suggestions for how to
best do this sort of thing in mysql, though it's all kludgey.

Kludgey? Not at all.

To the original poster, yes you can do this in MySQL. It is called using
"transactions", and has been available from MySQL 4 onwards. All that is
involved is using InnoDB tables instead of MyISAM tables.

http://dev.mysql.com/doc/mysql/en/an...nsactions.html

http://www.devarticles.com/c/a/MySQL...MySQL-4.0-and-
PHP/

http://www.databasejournal.com/featu...le.php/3382171


Hello,

Thank you all for the replies. I have been reading up on InnoDB tables
per your suggestions, and it looks like this is exactly what I was
looking for.

I was wondering if anyone has had any experience with InnoDB tables and
if anyone knows if they are more efficient in terms of
storage/speed/security or any other major aspects than MyISAM. I
remember reading somewhere awhile ago that MyISAM was the most efficient
table type available to MySQL, but that was obviously before 4.1 onwards.

Lastly, I am running 4.1 and was just wondering if changing existing
table types to InnoDB would have any adverse affects, or if the DB will
rearrange all the data on disk without any complications. I just want
to be sure I don't mess up data on my live server by changing the structure.

Thank you again in advance.

They are more efficient in terms of they are capable of recovering from
failed transactions.

This comes at a cost

Table and row locks are held longer.
More potential for race conditions and deadlocking.
More IO/Memory required.
Jul 17 '05 #7
On Wed, 20 Apr 2005 21:24:50 -0500, Good Man <he***@letsgo.com> wrote:
To the original poster, yes you can do this in MySQL. It is called using
"transactions", and has been available from MySQL 4 onwards. All that is
involved is using InnoDB tables instead of MyISAM tables.


It's been available since the later versions of 3.2, as well, in theory
anyway. 3.23.17 according to the manual.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #8
Andy Hassall wrote:
To the original poster, yes you can do this in MySQL. It is called using
"transactions", and has been available from MySQL 4 onwards. All that is
involved is using InnoDB tables instead of MyISAM tables.


Any way of converting a MyISAM tableto an InnoDB?

--
http://www.petezilla.co.uk
Jul 17 '05 #9
>>>To the original poster, yes you can do this in MySQL. It is called using
"transactions", and has been available from MySQL 4 onwards. All that is
involved is using InnoDB tables instead of MyISAM tables.


Any way of converting a MyISAM tableto an InnoDB?


ALTER TABLE foo TYPE=InnoDB;
or
ALTER TABLE foo ENGINE=InnoDB;

Don't convert the tables in the 'mysql' database.

Gordon L. Burditt
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DB2 Convert | last post by:
Hi, Correct me if I am wrong? Why should I specify at the restore statement such as Restore Database ABC ... "WITHOUT ROLLING FOWARD"? My understand is when I am using circular logging,...
3
by: db2group88 | last post by:
we are using db2 udb v8.2 on windows. All our tables are created with "not logged initially" parameter. Our application with auto commit on. I would like to do an online backup and rolling forward...
4
by: david.monaghan | last post by:
I have a table with the following fields - Location, Manager, CostCentre, Month and Headcount. What I am trying to do is a monthly rolling average headcount by Location, Manager, CostCentre. To...
101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
17
by: Jose Durazo | last post by:
Hello, I'm doing an exercise to simulate rolling a pair of dice 36,000 times, then count and display how many times the simulation rolls each possible sum. For some reason each time I run my...
2
by: BurtonBach | last post by:
I have a small database that mostly keeps track of material quantities going into jobs we do. I have been able to make this work well for me. I now want to add the tracking of a rolling balance of...
1
by: cricketweb | last post by:
I have a stored procedure that calls another stored procedure with the first stored procedure opening a transaction: BEGIN SET XACT_ABORT ON BEGIN TRANSACTION does various updates/inserts
4
by: brendan.wong | last post by:
hello. i'm trying to incorporate error handling into my application, but i've run into a dilemma. i've already performed 10 successful INSERTS, but on the 11th INSERT, the application fails for...
8
by: valentin tihomirov | last post by:
Once file is open, it should be closed: Stream fs = new FileStream(filename, FileMode.Create); try { // write, exceptions may raise here } finally { fs.Close(); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.