You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Robert Greig <ro...@btopenworld.com> on 2002/06/28 21:44:49 UTC

[dbcp] bug in DelegatingPreparedStatement

Hi,

I was using the latest build of the dbcp classes and I found a bug in 
the DelegatingPreparedStatement.

Basically, using the class will almost guarantee you get a 
ConcurrentModificationException from the trace list.

I think this problem was introduced in the commit:

revision 1.4
date: 2002/06/28 15:28:20;  author: glenn;  state: Exp;  lines: +19 -20
Fix a memory leak when tracing abandoned connections.
Cleanup of passivate and close methods.

The problem is the close() method of DelegatingPreparedStatement (and 
presumably others). Here are the problem lines:

List resultSets = getTrace();
if( resultSets != null) {
     Iterator it = resultSets.iterator();
     while(it.hasNext()) {
        ((ResultSet)it.next()).close();  // Line 166 of the source file
     }
     clearTrace();
}

The problem is that the elements in the iterator are really 
DelegatingResultSets which try to remove themselves from their container 
when close() is called on them. Hence the List which you're iterating 
through is modified during iteration.

The solution is obviously simple - just copy the list first, e.g.

ResultSet[] set = new ResultSet[resultSets.size()];
resultSets.toArray(set);
for (int i = 0; i < set.length; i++) {
     set[i].close();
}

etc.

Best regards,

Robert Greig

Re: [dbcp] bug in DelegatingPreparedStatement

Posted by Glenn Nielsen <gl...@voyager.apg.more.net>.
Thanks for reporting this.  I had fixed this in the
removeAbandoned() code for abandoned connections.
I'll take care of it.

Regards,

Glenn

Robert Greig wrote:
> 
> Hi,
> 
> I was using the latest build of the dbcp classes and I found a bug in
> the DelegatingPreparedStatement.
> 
> Basically, using the class will almost guarantee you get a
> ConcurrentModificationException from the trace list.
> 
> I think this problem was introduced in the commit:
> 
> revision 1.4
> date: 2002/06/28 15:28:20;  author: glenn;  state: Exp;  lines: +19 -20
> Fix a memory leak when tracing abandoned connections.
> Cleanup of passivate and close methods.
> 
> The problem is the close() method of DelegatingPreparedStatement (and
> presumably others). Here are the problem lines:
> 
> List resultSets = getTrace();
> if( resultSets != null) {
>      Iterator it = resultSets.iterator();
>      while(it.hasNext()) {
>         ((ResultSet)it.next()).close();  // Line 166 of the source file
>      }
>      clearTrace();
> }
> 
> The problem is that the elements in the iterator are really
> DelegatingResultSets which try to remove themselves from their container
> when close() is called on them. Hence the List which you're iterating
> through is modified during iteration.
> 
> The solution is obviously simple - just copy the list first, e.g.
> 
> ResultSet[] set = new ResultSet[resultSets.size()];
> resultSets.toArray(set);
> for (int i = 0; i < set.length; i++) {
>      set[i].close();
> }
> 
> etc.
> 
> Best regards,
> 
> Robert Greig

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>