Forum OpenACS Q&A: passing db handle to proc versus opening handle inside proc...

Sometimes I really ask too many questions 😊, but....

What are the pros and cons (I really like these two words 😉) of
passing on an existing db handle to a proc versus opening a new one
inside the proc.

It's usually better to pass the db handle to the proc because you can
only have one db handle per pool per thread.
You can design the proc to work with or without a handle passed to it (something we resort to a lot where I work):

proc foo { some_variable { db "" } } {

    if {[string compare $db ""] == 0} {
        set db [ns_db gethandle subquery]
        set release 1
    } else {
        set release 0
    }

    some more code

    if {$release} {
        ns_db releasehandle $db
    }
}

This is useful because sometimes you already have a handle when you call the proc, but if you don't it's nice to have the proc handle that. I believe ACS 4 makes this irrelevant, because you use a utility that gets and releases the handle for all calls to the database.

<i>I believe ACS 4 makes this irrelevant, because you use a utility that gets and releases the handle for all calls to the database</i><br>
<br>
Correct, and if you don't want to wait, you can use the <a href="/forums/message-view?message_id=18418">backport of the ACS4 api</a> right now. :)
Thanks a lot Jonathan. This will be a real help for us.