You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Antony Paul <an...@hotmail.com> on 2004/04/19 11:34:48 UTC

[DBCP] PreparedStatements throwing Already closed Exception.

Hi all,
    I was using DBCP quite well without any problem. Yesterday I had an idea
to reap the benefit of PreparedStatement pools because 95% of our
application uses PreparedStatements. But to my surprise it is throwing
SQLException("Already closed") in PoolablePreparedStatement when
PreparedStatement pooling is turned on. What is the need of throwing an
exception here against the default behaviour of ordinary PreparedStatements
?. I think this is unwanted as it forces to set a PreparedStatement to null
on closing and in the finally block again check it for null when the same
prepared statement is used. Or is there anything as not use same
PreparedStatement for another sql statement ?. If so please document it in
the configuration section and API of DBCP.

Antony Paul


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DBCP] PreparedStatements throwing Already closed Exception.

Posted by Dirk Verbeeck <di...@pandora.be>.
The idea is that each connection has a keyed pool of prepared 
statements. With the statement string as key.
When you do
    stmt1 = conn.prepareStatement("select ...");
you actually borrow the statement from the pool and it's returned on 
stmt1.close()

The normal use is
    stmt1 = conn.prepareStatement("select ...");
    use stmt1
    stmt1.close()

    stmt2 = conn.prepareStatement("select ...");
    use stmt2
    stmt2.close()

If the statement "select ..." is the same then the same undelying 
statement will be used.

This is also possible:
    stmt1 = conn.prepareStatement("select ...");
    stmt2 = conn.prepareStatement("select ...");
    use stmt1
    use stmt2
    stmt1.close()
    stmt2.close()
The pool will create a second database statement because stmt1 is 
still "in use". The close() will free the underlying statement for reuse.

-- Dirk


Antony Paul wrote:
> I am using DBCP 1.1 stable.
> I shall give details later but a quick question. In
> PoolablePreparedStatement.close() method it is checking isClosed() and
> throws SQLException if it is already closed. What is its intention ?.
> 
> Antony Paul
> 
> ----- Original Message -----
> From: "Dirk Verbeeck" <di...@pandora.be>
> To: "Jakarta Commons Users List" <co...@jakarta.apache.org>
> Sent: Wednesday, April 21, 2004 1:45 AM
> Subject: Re: [DBCP] PreparedStatements throwing Already closed Exception.
> 
> 
> 
>>What version are you using? (v1.1 or nightly build)
>>Can you give a small code example of the problem?
>>
>>-- Dirk
>>
>>Antony Paul wrote:
>>
>>
>>>Hi all,
>>>    I was using DBCP quite well without any problem. Yesterday I had an
> 
> idea
> 
>>>to reap the benefit of PreparedStatement pools because 95% of our
>>>application uses PreparedStatements. But to my surprise it is throwing
>>>SQLException("Already closed") in PoolablePreparedStatement when
>>>PreparedStatement pooling is turned on. What is the need of throwing an
>>>exception here against the default behaviour of ordinary
> 
> PreparedStatements
> 
>>>?. I think this is unwanted as it forces to set a PreparedStatement to
> 
> null
> 
>>>on closing and in the finally block again check it for null when the
> 
> same
> 
>>>prepared statement is used. Or is there anything as not use same
>>>PreparedStatement for another sql statement ?. If so please document it
> 
> in
> 
>>>the configuration section and API of DBCP.
>>>
>>>Antony Paul
>>
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DBCP] PreparedStatements throwing Already closed Exception.

Posted by Antony Paul <an...@hotmail.com>.
I am using DBCP 1.1 stable.
I shall give details later but a quick question. In
PoolablePreparedStatement.close() method it is checking isClosed() and
throws SQLException if it is already closed. What is its intention ?.

Antony Paul

----- Original Message -----
From: "Dirk Verbeeck" <di...@pandora.be>
To: "Jakarta Commons Users List" <co...@jakarta.apache.org>
Sent: Wednesday, April 21, 2004 1:45 AM
Subject: Re: [DBCP] PreparedStatements throwing Already closed Exception.


> What version are you using? (v1.1 or nightly build)
> Can you give a small code example of the problem?
>
> -- Dirk
>
> Antony Paul wrote:
>
> > Hi all,
> >     I was using DBCP quite well without any problem. Yesterday I had an
idea
> > to reap the benefit of PreparedStatement pools because 95% of our
> > application uses PreparedStatements. But to my surprise it is throwing
> > SQLException("Already closed") in PoolablePreparedStatement when
> > PreparedStatement pooling is turned on. What is the need of throwing an
> > exception here against the default behaviour of ordinary
PreparedStatements
> > ?. I think this is unwanted as it forces to set a PreparedStatement to
null
> > on closing and in the finally block again check it for null when the
same
> > prepared statement is used. Or is there anything as not use same
> > PreparedStatement for another sql statement ?. If so please document it
in
> > the configuration section and API of DBCP.
> >
> > Antony Paul
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DBCP] PreparedStatements throwing Already closed Exception.

Posted by Dirk Verbeeck <di...@pandora.be>.
What version are you using? (v1.1 or nightly build)
Can you give a small code example of the problem?

-- Dirk

Antony Paul wrote:

> Hi all,
>     I was using DBCP quite well without any problem. Yesterday I had an idea
> to reap the benefit of PreparedStatement pools because 95% of our
> application uses PreparedStatements. But to my surprise it is throwing
> SQLException("Already closed") in PoolablePreparedStatement when
> PreparedStatement pooling is turned on. What is the need of throwing an
> exception here against the default behaviour of ordinary PreparedStatements
> ?. I think this is unwanted as it forces to set a PreparedStatement to null
> on closing and in the finally block again check it for null when the same
> prepared statement is used. Or is there anything as not use same
> PreparedStatement for another sql statement ?. If so please document it in
> the configuration section and API of DBCP.
> 
> Antony Paul




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org