lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> I am not sure if I get your point. try-catch nor pcall would
> prevent these connections from accumulating, unless it is placed
> INSIDE the code, in which case you should wonder: "Was throwing that
> exception really worth it?". Or, if you call a external library, you
> should wonder: "Is there anything I can do to prevent my program
> from misbehaving on a error?". If you can't, you should switch
> library, switch database, or accept that you made the wrong choices
> or have to deal with a braindead API and just create a pcall.
> Or, alternatively, you could do something like this:
>  local ok, err = pcall(function()
>     local f ='file.txt')
>     f:close()
> end)
> if not ok then
>     collectgarbage() -- force garbage collection, closing open database connections
>     error(err)
> end
>  which is what a sensible database API would want you to do.

There exists several other way in API.
1. Use `acquire` function.
  db.acquire(..., function()
    ... do some work ...
    error('some error')

`acquire` function can close db properly and propagate error further.
But  there problem with original stack trace. I think error object can
have one.

2.  Variant  implemented in LuaSocket library with `protect` and `try`

Also I think about how Lua can extend generic for to be able call some
finalizer  when  loop  breaks.  Its  real  problem  because now it not
possible implement such mechanism.

  for row in db:rows('select * ...') do
    if ... then break end

we  have  no access to cursor directly so we can not close it and have
to rely on GC.

С уважением,

Это сообщение проверено на вирусы антивирусом Avast.