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)))
>

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

Reply via email to