Skip to content

[Bioc-devel] Error during wrapup, what does this mean?

7 messages · Dipl.-Ing. Johannes Rainer, James W. MacDonald, Seth Falcon +1 more

#
hi, it's again me, but i found the problem...
as i am working with database connections and if an error occurs in a 
function call i want to roll back the whole stuff that has been written 
into a database, i thought to be smart and defined with
where .rollItBack is a function that sends the command ROLLBACK 
TRANSACTION to the database. well, i did not think, that when the error 
occurs the object Con, which is a local variable in the function does 
not exist, unless the user has called the connection object also 'Con'.

now i want to ask how i can define a global variable from within a function.

cheers, jo

Quoting "Dipl.-Ing. Johannes Rainer" <johannes.rainer@tugraz.at>:
#
Dipl.-Ing. Johannes Rainer wrote:
See ?"<<-" and ?assign

HTH,

Jim

  
    
#
"Dipl.-Ing. Johannes Rainer" <johannes.rainer@tugraz.at> writes:
I thought that was what had happened, but I didn't want to show off my
ESP so early in the morning ;-)
You can use "<<-" and can also use assign.  You could also use
environments to pass information around since they do not get copied.

options(error=expression(.rollItBack(dbParams$con)))
dbParams <- new.env(parent=NULL)

dbParams[["user"]] <- "jo"
dbParams[["url"]] <- "blah"
doSomething(dbparams, query)


HTH,

+ seth
#
James W. MacDonald wrote:
Hi,

   But it is generally not a good idea to do it that way. If users have 
connections that you are using then they should be parameters to the 
functions that you are using and using try, or tryCatch, will generally 
be much better. I would also like to point out that if I understand 
this, then any error the user makes will roll-back a transaction - 
typing the wrong name for a function etc. That is probably not a good 
design decision.

   Robert
#
i agree with this. but what if the user types <ctrl>+c during the call? 
i assume that try and tryCatch will not work in this cases, as i want 
also roll back a transaction when the user breaks the operation by 
himself.
currently i have solved the problem in the following way:
with options(error=expression(.rollItBack()))

and during the function call i copy the connection parameter to a 
global variable called 'ErrorConnection' which is used by the 
rollItBack function. on the 'on.exit' i reset the options(error=NULL) 
and remove the 'ErrorConnection' from the global environment...

jo

Quoting Robert Gentleman <rgentlem@fhcrc.org>:
#
Dipl.-Ing. Johannes Rainer wrote:
Hi,
  First - why assume, why not just test? Second - pass a connection in 
and use on.exit? Third, from the man page for tryCatch:
    User interrupts signal a condition of class 'interrupt' that
      inherits directly from class 'condition' before executing the
      default interrupt action.

  Which seems to answer your question.
And as I said - any and every error rolls back a transaction - not 
really what you would like to have happen. Using global variables is, in 
my experience, never a good idea. Explicity parameter passing, and 
encapsulation via lexical scope are much easier to work with.

  And what if I, as many other do, use options(error=recover) for 
debugging purposes. Your code seems to have a rather unfelicitous effect.

  Robert
2 days later
#
thanks for all your help!
i am now using the tryCatch for all my error (and interrupt) catching :)

thanks!

Quoting Robert Gentleman <rgentlem@fhcrc.org>: