You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mike Whittaker <mi...@ntlworld.com> on 2003/06/03 16:14:02 UTC
[OT] data layer, try-catch-finally, connections
I guess you've all been here, so can you answer this?
you have do a 'finally' to ensure you close the connection/release it back
to the pool.
In the controller tier this is easy, get the connection in the
Filter/RequestProcessor/Action when whatever returns you can finally close
it.
However this ties it to the controller layer. I want my connections
instigated from the business layer. But because so many processes require a
connection I will have identical try,catch,finally block all over the place.
Any one got a solution to this?
Thankyou
--
Mike W
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: [OT] data layer, try-catch-finally, connections
Posted by Ian Hunter <ih...@hunterweb.net>.
I have two routines in my data layer that close connections; one takes an
exception as a parameter and the other doesn't. My data layer objects open
their own connections and close them when they're done. (Actually, routines
that populate/read DTOs from/to the data layer make the calls)
public static void closeconn (java.sql.Connection conn,
java.sql.Statement s, java.sql.ResultSet rs) throws java.sql.SQLException {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (s != null) {
s.close();
s = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (java.sql.SQLException e) {
closeconn(conn, s, rs, e);
}
}
and if THAT one throws an exception, it calls this:
public static void closeconn (java.sql.Connection conn,
java.sql.Statement s, java.sql.ResultSet rs,
java.sql.SQLException e) throws java.sql.SQLException {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (java.sql.SQLException rse) {
e.setNextException (rse);
rs = null;
}
}
if (s != null) {
try {
s.close();
s = null;
} catch (java.sql.SQLException se) {
e.setNextException (se);
s = null;
}
}
if (conn != null) {
try {
conn.close();
conn = null;
} catch (java.sql.SQLException ce) {
e.setNextException (ce);
conn = null;
}
}
throw new java.sql.SQLException (e.getMessage(), e.getSQLState(),
e.getErrorCode());
}
Crazy, perhaps, but it works quite well.
----- Original Message -----
From: "Mike Whittaker" <mi...@ntlworld.com>
To: "Struts List" <st...@jakarta.apache.org>
Sent: Tuesday, June 03, 2003 10:14 AM
Subject: [OT] data layer, try-catch-finally, connections
> I guess you've all been here, so can you answer this?
>
> you have do a 'finally' to ensure you close the connection/release it back
> to the pool.
>
> In the controller tier this is easy, get the connection in the
> Filter/RequestProcessor/Action when whatever returns you can finally close
> it.
>
> However this ties it to the controller layer. I want my connections
> instigated from the business layer. But because so many processes require
a
> connection I will have identical try,catch,finally block all over the
place.
>
> Any one got a solution to this?
>
> Thankyou
> --
> Mike W
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org