Hi Martin,
thanks for the help/education :-)
It all makes perfect sense.
Brian


----- Original Message -----
From: "Martin Vilcans" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, June 12, 2003 12:52 PM
Subject: RE: [JBoss-user] Closing database connections


> > Glad to see that I was on the right track. Just one little
> > question. Why do
> > you have
> > two finally blocks in the DAO - can't you close both the statement and
the
> > result set
> > in the same finally block?
>
> This is because when you close the statement, the result set might not
> necessarily exists. See comments below:
>
> > >     public Page findByKey(Connection connection, String key, int
start,
> > int
> > > count )
> > >         throws SQLException
> > >     {
> > >         PreparedStatement stmt = connection.prepareStatement (
> > > SQL_FIND_BY_KEY,
> > >                 ResultSet.TYPE_SCROLL_INSENSITIVE,
> > > ResultSet.CONCUR_READ_ONLY );
> > >         try {
> > >
> > >           stmt.setString ( 1, key );
>
> If the above line throws an exception, the ResultSet doesn't exist yet.
> Execution will resume at stmt.close() in the second finally. You can't
have
> rs.close() in the second finally as rs is out of scope.
>
> > >           stmt.setString ( 2, key );
> > >           stmt.setString ( 3, key );
> > >
> > >           ResultSet rs = stmt.executeQuery (  );
> > >           try {
> > >             return toPage ( rs, start, count );
> > >           }
> > >           finally {
> > >             rs.close();
> > >           }
> > >         }
> > >         finally {
> > >           stmt.close();
> > >         }
> > >     }
>
> If you allocate a lot of stuff, e.g. a Connection, a PreparedStatement and
a
> ResultSet in a method, all the finally clauses can clutter the code, and
in
> that case I usually do something like this instead:
>
> void method() {
>   Connection connection = null;
>   PreparedStatement statement = null;
>   ResultSet results = null;
>
>   try {
>     connection = getConnection();
>     statement = connection.prepareStatement(...);
>     statement.setString(...);
>     results = statement.executeQuery();
>     ...
>   }
>   finally {
>     if(results != null) results.close();
>     if(statement != null) statement.close();
>     if(connection != null) connection.close();
>   }
> }
>
> What I don't like about this is the C-ish convention of declaring all
> variables in the beginning of the method. It works fine, though. In
reality
> I'd also encapsulate if(xxx != null) xxx.close() into a method to avoid
code
> duplication.
>
> Unfortunately, both of these solutions clutter the code more or less.
> Paradoxally, garbage collected languages such as Java force you to do a
lot
> of resource handling using finally clauses. In C++, code like the above
> would be much cleaner, thanks to the RAII pattern.
>
> Martin
>
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by: eBay
> Great deals on office technology -- on eBay now! Click here:
> http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
> _______________________________________________
> JBoss-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/jboss-user
>



-------------------------------------------------------
This SF.NET email is sponsored by: eBay
Great deals on office technology -- on eBay now! Click here:
http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to