Here is a copy of an email that I sent to Jeremy K. about this
plugin. At the end of the day, I cannot get my objects to save, so I
give up. I’m going to stop using object-level transactions, and remove
this plugin from my project.
Perhaps this email will help someone who runs into the same issue.
Wes
======================
Jeremy,
I’ve decided to give up on trying to use the object_transactions plugin,
because I can’t get all of my saves to work. The patch that I already
submitted to you handles my session saving, but I can’t get anything
else to save.
I’ll tell you what seems to be happening, but I can’t spend any more
time on this (it’s been way too many hours already).
Here’s some code from my controller - it’s purpose is to assign an
identifier to an AR::Base descendant (@current_job):
@current_job = Job.new(:contact => Contact.find(session[:user]))
@current_job.TPEMail = 1
Job.transaction(@current_job) do
Job.find_by_sql("SELECT DISTINCT 'x' FROM #{Job.table_name} WITH
(TABLOCK XLOCK)")
@current_job.ABSJobNumber = Job.get_next_job_number.to_s
@current_job.save!
end
What appears to happen here is that when things get down to calling the
save_with_transactions! method inside of active_record/transactions.rb
(shown below), for some reason the { transaction {
save_without_transactions! } } block is not being executed, and the call
to AR::Base.save! never happens.
def save_with_transactions! #:nodoc:
rollback_active_record_state! { transaction {
save_without_transactions! } }
end
The same behavior applies if you just call the “regular” save (sans the
!), which in turn, ultimately causes the save_with_transactions method
to be called. Again, the call to AR::Base.save never happens.
Note that if I simply call “save_without_transactions!” or
“save_without_transactions” outside of that transaction block, then they
work fine and dutifully execute save! and save, respectfully. But these
calls do not work when they are inside the block that is passed to
transaction().
What I was unable to figure out was which method is being called when
“transaction” is called when { transaction { save_without_transactions!
}} is executed. Wouldn’t that be the
“transactions_with_object_transactions” method again (which, BTW is
where this code is getting called from)? There’s some weird “too much
method chaining thing” going on here I think, but again, I don’t have
the time nor the energy to try and follow the chained logic. Perhaps
there should be some way of knowing that the save calls are already
within the context of a transaction, and thus, don’t need to put the
save_without_transaction calls inside of a block to pass to transaction?
Thanks,
Wes G.