Hi all,
I have a doubt in transactions. Please see the code below.
begin
Project.transaction do
p = Project.new
p.name = “test”
p.user_id = 10
p.save!
if nil.projects
puts “User is having projects”
end
end
rescue Exception => e
puts e.to_s
end
In the above case definitely it will fail at “nil.projects” and throws
an exception. But the project is being saved and is not rolled back and
when I see the log it’s showing rolled back and inthe DB the row is not
getting deleted.
I dont want the row to be created in the DB if there is any error even
after the save. So what should be done??
The below is the log:
SQL (0.001000) INSERT INTO projects (‘name’, ‘user_id’)
VALUES(‘test’, 10)
app/models/project.rb:305:in confirm_project' app/models/project.rb:288:in
confirm_project’
app/models/project.rb:274:in confirm_project' app/controllers/test_controller.rb:19:in
confirm_project’
app/controllers/test_controller.rb:12:in `select_user’
SQL (0.000000) ROLLBACK
What is the problem in the above code? Do I need to handle it in another
way ??
Please reply me regarding this error.
On 22 Sep 2008, at 14:19, Naga harish Kanegolla wrote:
app/controllers/test_controller.rb:12:in `select_user’
SQL (0.000000) ROLLBACK
That’s normal. Were you expecting to see DELETE FROM projects … ?
Fred
Hi Fred,
Yes I am expecting to see DELETE FROM projects…
Is it not possible in the transaction block??
Frederick C. wrote:
On 22 Sep 2008, at 14:19, Naga harish Kanegolla wrote:
app/controllers/test_controller.rb:12:in `select_user’
SQL (0.000000) ROLLBACK
That’s normal. Were you expecting to see DELETE FROM projects … ?
Fred
On 22 Sep 2008, at 14:43, Naga harish Kanegolla wrote:
Hi Fred,
Yes I am expecting to see DELETE FROM projects…
Is it not possible in the transaction block??
That’s just not how transactions work. The ROLLBACK statement does the
work of unwinding all the changes since the previous BEGIN.
Fred
On Sep 22, 9:53 pm, Frederick C. [email protected]
wrote:
That’s just not how transactions work. The ROLLBACK statement does the
work of unwinding all the changes since the previous BEGIN.
I’m a little out of date here. Does MySQL support nested transactions,
and if it does, does AR support it?
On 22 Sep 2008, at 15:00, Erol F. wrote:
On Sep 22, 9:53 pm, Frederick C. [email protected]
wrote:
That’s just not how transactions work. The ROLLBACK statement does
the
work of unwinding all the changes since the previous BEGIN.
I’m a little out of date here. Does MySQL support nested transactions,
and if it does, does AR support it?
Mysql 5 has savepoints. AR doesn’t do anything with them.
Fred
But a roll back should come to the previous state. so in this case how i
can get back to the previous state with out inserting into DB…
What should I do??
Frederick C. wrote:
On 22 Sep 2008, at 14:43, Naga harish Kanegolla wrote:
Hi Fred,
Yes I am expecting to see DELETE FROM projects…
Is it not possible in the transaction block??
That’s just not how transactions work. The ROLLBACK statement does the
work of unwinding all the changes since the previous BEGIN.
Fred
On 22 Sep 2008, at 15:11, Naga harish Kanegolla wrote:
But a roll back should come to the previous state. so in this case
how i
can get back to the previous state with out inserting into DB…
What should I do??
The ROLLBACK statement magically unwinds all changes made. The
database (ie not your app or activerecord) takes care of that.
Hi,
Thanks a lot. Now its working fine. Right now my table type is MyISAM
and I changed the type into InnoDB Storage. In this type the
transactions are safe.
Now all the transactions are getting rolled back.
Frederick C. wrote:
On 22 Sep 2008, at 15:11, Naga harish Kanegolla wrote:
But a roll back should come to the previous state. so in this case
how i
can get back to the previous state with out inserting into DB…
What should I do??
The ROLLBACK statement magically unwinds all changes made. The
database (ie not your app or activerecord) takes care of that.
On 23 Sep 2008, at 05:52, Naga harish Kanegolla
<[email protected]
wrote:
Hi,
Thanks a lot. Now its working fine. Right now my table type is MyISAM
and I changed the type into InnoDB Storage. In this type the
transactions are safe.
Now all the transactions are getting rolled back.
Ah yes. As you have found out, myisam doesn’t do transactions at all
Fred