Forum OpenACS Development: Re: Can we use namespace variables to avoid upvar?

Collapse
Posted by Tom Jackson on

In AOLserver there is a convenient proc ns_atclose which might be used to do what you want. Here is an example of it being used with namespace vars:


namespace eval ::twt::db::transaction {

    variable initialized 0
    variable ids
 
    namespace import ::twt::log::*
}


proc ::twt::db::transaction::init { } {

    ns_atclose ::twt::db::transaction::reset
    
    variable initialized 1
    variable ids
    array unset ids

}

proc ::twt::db::transaction::reset { } {

    # This runs after the connection closes

    variable initialized
    variable ids

    # Need to rollback any open transactions
    foreach id [array names ids] {
	
	set transactionInfo $ids($id)
	set provider [lindex $transactionInfo 0]
	set transactionRollbackProc [set ::twt::db::${provider}::transactionRollbackProc]
	$transactionRollbackProc $id
    }
    
    array unset ids
    set initialized 0
}

The namespace vars are reset for each connection. Ns_atclose is guaranteed to run even if the connection returns an error, it also runs at the end of a scheduled procedure, if the code were to be used there.