2010/4/21 Mark J. Reed <markjr...@gmail.com>:
> On Wed, Apr 21, 2010 at 9:37 AM, Laurent PETIT <laurent.pe...@gmail.com>
> wrote:
>>
>> Hi,
>>
>> Something I don't understand: if the call to (API/getConnection ...)
>> fails, there is nothing to close, right ?
>>
>> So for the problem of ensuring that any open connection is always
>> closed, the following pattern seems enough:
>>
>> (try
>>  (let [conn (API/getConnection ..)]
>>     XXX)
>>  (finally (API/closeConnection conn)))

Of course, My code was wrong, and I meant


  (let [conn (API/getConnection ..)]
    (try
      XXX
      (finally (API/closeConnection conn)))

And really, catching exceptions thrown by API/getConnection is not in
the problem domain of ensuring balanced pairs of opened/closed
connections, e.g. should be outside the abstraction you put in place
for handling this (e.g. with-open).

Ensuring that opened connections are closed is a resource (I/O)
management problem.
Handling the case where you were not able to open a connection is an
application problem. You might place the try/catch right around the
let, have a global handling very high in the call stack, or even have
no try/catch at all.

HTH,

-- 
Laurent

>
> That would be great, but it doesn't work, because "conn" doesn't exist any
> more by the time you get to the finally block. That's the problem being
> addressed in this thread.
> So you can do this:
> (let [conn (API/getConnection ...)]
>    (try
>       XXX
>    (finally (API/closeConnection conn))))
> But then any exception thrown by API/getConnection is not caught.
>
> So you have to do this:
> (try
>     (let [conn (API/getConnection ...)]
>       (try
>         XXX
>       (finally (API/closeConnection conn))))
>    (catch ...))
> But that still won't work if the catch block also needs access to "conn".
>  So you wind up with something like this:
> (try
>     (let [conn (API/getConnection ...)]
>       (try
>         XXX
>       (catch ExceptionAfterConnect x (do-something-with conn))
>       (finally (API/closeConnection conn))))
>    (catch .ExceptionDuringConnect x (do-something-without-conn))))
> --
> Mark J. Reed <markjr...@gmail.com>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to