|
From: Kopylenko, D. <dko...@ac...> - 2003-08-13 23:34:17
|
Thomas, yes, that is the way to go :-)
-----Original Message-----
From: tri...@tr...
To: Kopylenko, Dmitry
Cc: 'springframework-developer'
Sent: 8/13/2003 3:02 PM
Subject: RE: [Springframework-developer] JdbcTemplate.doWithResultSetFromS
taticQuery cleanups
Dmitry,
I think that would work, but I don't know if we need all the extra
exceptions
thrown - if everything went OK, you could end up with
CleanupFailureDataAccessExceptions when you try to close the resultset a
second
time in the finally block.
I was thinking of adding the resultset/statement closing in the catch
block -
usually they are closed inside of the try block. In the case of an
exception
beeing thrown we can assume that if the resource is not null, then it
has been
created, but most likely it has not been closed -- so we should try to
close it.
Doing it this way, we don't incurr any extra processing in normal
processing or
risk any extra exceptions beeing thrown.
Something like this:
public void doWithResultSetFromStaticQuery(... ) throws
DataAccessException {
//
//
//
try {
con = DataSourceUtils.getConnection(this.dataSource);
s = con.createStatement();
rs = s.executeQuery(sql);
//
//
//
rs.close();
s.close();
}
catch (SQLException ex) {
if (rs != null) try {rs.close();} catch(SQLException ignore) {}
if (s != null) try {s.close();} catch(SQLException ignore) {}
throw getExceptionTranslater().translate(... );
}
finally {
DataSourceUtils.closeConnectionIfNecessary(con, this.dataSource);
}
}
What do you think?
Thomas
Quoting "Kopylenko, Dmitry" <dko...@ac...>:
> So the "finally block" for this method would look like this:
>
> finally {
> try {
> rs.close();
> }
> catch (SQLException ex) {
> DataSourceUtils.closeConnectionIfNecessary(con,
> this.dataSource);
> throw new CleanupFailureDataAccessException("Failed to
close
> ResultSet", ex);
> }
> try {
> s.close();
> }
> catch (SQLException ex) {
> DataSourceUtils.closeConnectionIfNecessary(con,
> this.dataSource);
> throw new CleanupFailureDataAccessException("Failed to
close
> Statement", ex);
> }
> DataSourceUtils.closeConnectionIfNecessary(con,
this.dataSource);
> }
>
> I could make a change if no one objects.
>
> Dmitriy.
>
> -----Original Message-----
> From: Kopylenko, Dmitry [mailto:dko...@ac...]
> Sent: Wednesday, August 13, 2003 1:13 PM
> To: 'springframework-developer'
> Subject: FW: [Springframework-developer]
JdbcTemplate.doWithResultSetFromS
> taticQuery cleanups
>
>
> To the list...
>
> -----Original Message-----
> From: Levkovitch, Lidia
> Sent: Wednesday, August 13, 2003 1:12 PM
> To: Kopylenko, Dmitry
> Subject: RE: [Springframework-developer]
JdbcTemplate.doWithResultSetFromS
> taticQuery cleanups
>
>
> Well, IMHO, one can't rely on that. I believe I've seen instances when
a
> connection was closed, leaving behind open cursors (i.e. result sets).
>
> I would add rs.close() and s.close() to the finally block, and put
each of
> them into try {} catch (everything) {//do nothing...}
>
> Lidia
>
> -----Original Message-----
> From: Kopylenko, Dmitry [mailto:dko...@ac...]
> Sent: Wednesday, August 13, 2003 12:51 PM
> To: 'Justinus Menzel'; 'springframework-developer'
> Subject: RE: [Springframework-developer]
JdbcTemplate.doWithResultSetFromS
> taticQuery cleanups
>
> Would closing connection automatically take care of closing Statement
and
> ResultSet? I guess it all depends on the JDBC driver.
>
> Dmitriy.
>
> -----Original Message-----
> From: Justinus Menzel [mailto:jus...@lb...]
> Sent: Wednesday, August 13, 2003 12:09 PM
> To: springframework-developer
> Subject: [Springframework-developer]
> JdbcTemplate.doWithResultSetFromStaticQuery cleanups
>
>
> Hi,
>
> first let me tell you that Spring is great, you guys rock!
>
> Now to the question: if you look into
> JdbcTemplate.doWithResultSetFromStaticQuery():
> ...
> try {
> con = DataSourceUtils.getConnection(this.dataSource);
> s = con.createStatement();
> rs = s.executeQuery(sql);
>
> if (logger.isInfoEnabled())
> logger.info("Executing static SQL query '" + sql + "'
> using a java.sql.Statement");
>
> rse.extractData(rs);
>
> SQLWarning warning = s.getWarnings();
> rs.close();
> s.close();
>
> throwExceptionOnWarningIfNotIgnoringWarnings(warning);
> }
> catch (SQLException ex) {
> throw
> getExceptionTranslater().translate("JdbcTemplate.query(sql)", sql,
ex);
> }
> finally {
> DataSourceUtils.closeConnectionIfNecessary(con,
> this.dataSource);
> }
> ...
>
> it seems like rs.close() and s.close() will be executed only if there
> was no exception thrown from
> rse.extractData(). s.close will only be executed if rs =
> s.executeQuery(sql); did OK.
> So my question is: why don't both close() go into the finally block?
The
> reason why I'm asking this is that we had a few problems with
> running out of cursors
> in our application server and we also had a few
> examples of queries not working correctly with certain input
parameters.
>
> any comments are much appreciated
> thanks
>
> Justinus
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data
> Reports, E-commerce, Portals, and Forums are available now. Download
today
> and enter to win an XBOX or Visual Studio .NET.
>
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data
> Reports, E-commerce, Portals, and Forums are available now. Download
today
> and enter to win an XBOX or Visual Studio .NET.
>
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data
> Reports, E-commerce, Portals, and Forums are available now. Download
today
> and enter to win an XBOX or Visual Studio .NET.
>
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
>
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
|