Thanks for the reply Alex,
I thought of 2 try blocks, but seems over engineering doesn't it ? I
actually need to open 2 connections simultaneously, which would amount
to 3 nested try blocks. The whole code gets cluttered with all these
try finally (and one catch) statements.
(try
(let [conn1 (API1/getConnection ..)]
(try
(let [conn2 (API2/getConnection ..)]
(try
( ........... Do something with conn1 conn2 ............)
(finally
(API2/closeConnection conn2))))
(finally
(API1/closeConnection conn1))))
(catch Exception ex (.printStackTrace ex)))
The macro solution looks good. But with 2 different APIs for 2
connections, I would need to write 2 macros right?
(defmacro with-api1-connection [conn-sym arg1 arg2 & body]
`(let [~conn-sym (API1/getConnection ~arg1 ~arg2)]
(try
~...@body
(finally (API1/closeConnection ~conn-sym)))))
(defmacro with-api2-connection [conn-sym arg1 arg2 arg3 & body]
`(let [~conn-sym (API2/getConnection ~arg1 ~arg2 ~arg3)]
(try
~...@body
(finally (API2/closeConnection ~conn-sym)))))
Also coming back to the original requirement, wanted to know what are
the problems with providing support (somehow) for something like -
(try
(let [conn1 (API1/getConnection ..)
conn2 (API2/getConnection ..)]
(....))
(catch ..)
(finally
(if conn1 (API1/closeConnection conn1))
(if conn2 (API2/closeConnection conn2))))
Coming from Java, this would be implemented as -
Connection1 conn1 = null;
Connection2 conn2 = null;
try {
conn1 = API1.getConnection ..;
conn2 = API2.getConnection ..;
...
}
catch (){}
finally {
if (conn1 != null)
API1.closeConnection(conn1);
if (conn2 != null)
API2.closeConnection(conn2);
}
I agree that this code doesn't look good from a purist pov, but any
issues besides that?
- Thanks!
On Apr 21, 4:58 pm, Alex Osborne <[email protected]> wrote:
> ka <[email protected]> writes:
> > How can I do something like ...
>
> > (try
> > (let [conn (API/getConnection ..)]
> > (....))
> > (catch ..)
> > (finally (if conn (API/closeConnection conn))))
>
> > Problem is that conn is unavailable in the finally context.
>
> Why not just have two try blocks?
>
> (try
> (let [conn (API/getConnection ..)]
> (try
> (....)
> (finally (API/closeConnection conn))))
> (catch ...))
>
> For these sorts of things I usually try to use with-open, or if
> with-open is not applicable do my own similar macro:
>
> (defmacro with-api-connection [conn-sym arg1 arg2 & body]
> `(let [~conn-sym (API/getConnection ~arg1 ~arg2)]
> (try
> ~...@body
> (finally (API/closeConnection ~conn-sym)))))
>
> So then you never forget to close the connection:
>
> (try
> (with-api-connection conn ...
> (... do something with conn ...))
> (catch ...))
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group
> athttp://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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en