You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Preetam Palwe <pr...@aftek.com> on 2006/04/18 16:59:32 UTC

[Commons DBCP] Cannot get a connection, pool exhausted

Hi All,
	I am using DBCP with Hibernate, when I deploy my server application it
works fine for some hours and after that for any successive request it
throws

<stacktrace>
org.hibernate.exception.GenericJDBCException: Cannot open connection
        at
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLSta
teConverter.java:82)
        at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
        at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
:43)
        at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
:29)
        at
org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:427)
        at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:168)
        at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:103)
        at
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
        at
org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransa
ctionFactory.java:24)
        at
org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:231)
        at
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1073)
	  ...........
        at java.util.TimerThread.mainLoop(Timer.java:432)
        at java.util.TimerThread.run(Timer.java:382)
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a
connection, pool exhausted
        at
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.ja
va:103)
        at
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:5
40)
        at
com.xxx.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:243
)
        at
org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:424)
        ... 11 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
        at
org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPoo
l.java:756)
        at
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.ja
va:95)
        ... 14 more
</stacktrace>

Can anyone please tell me whets going wrong ?
here is my configuration
	j2sdk 1.4.2_6
	hibernate 3.0
	commons-pool 1.2
	commons-dbcp 1.2.1
	fedora core 3
and
	hibernate config file looks like

	<property
name="hibernate.connection.provider_class">com.xxx.DBCPConnectionProvider</p
roperty>
	<property name="hibernate.connection.pool_size">20</property>
	<property name="hibernate.dbcp.initialSize">10</property>
	<property name="hibernate.dbcp.maxWait">10000</property>
	<property name="hibernate.dbcp.validationQuery">select 1</property>

I have taken com.xxx.DBCPConnectionProvider from
http://wiki.apache.org/jakarta-commons/DBCP/Hibernate


Thanks&Regards
~Preetam


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


Re: [Commons DBCP] Cannot get a connection, pool exhausted

Posted by Sandy McArthur <sa...@apache.org>.
Okay, so maybe Hibernate happens to be kind enough to clean up after
you even when you forget to clean up after yourself. I don't think
that behavior is guaranteed.

It's also not going to be reliable as it would depend on when the
garbage collector runs and then when the finalizer thread happens to
run after that. Since no promises are made when those happen, your
hoping for the best.

Also, just because Hibernate cleans up some when it's garbage
collected, doesn't mean it's smart enough to return connections to the
Dbcp pool where that can return them to Pool.

Until the forthcoming Pool 2, the tracking of "active" objects (jdbc
connections in your case) is done by simply incrementing or
decrementing a counter. The means that counter doesn't get decremented
if "active" objects aren't returned. With your settings, all it takes
are 20 of these connections to get lost and the Pool will think there
are too many connections out there right now, I'll wait for one to be
returned. That never happens, and   eventually it throws a
NoSuchElementException.

Use those log messages are proof that you aren't closing/returning
sessions like you should be. Don't think of them as a warning that
there was a problem but now everything is gonna be okay.

On 4/19/06, Preetam Palwe <pr...@aftek.com> wrote:
> thanks !
> but
> even if i dont close hibernate session, hibernate will close those for me
> right ?
> the finalize() of org.hibernate.jdbc.JDBCContext will do this job for me
> right ?
> i checked the hibernate log and i have seen ....
> 2006 04 19 11:20:53 ADC [Finalizer] [DEBUG] JDBCContext.finalize() - running
> Session.finalize()
> 2006 04 19 11:20:53 ADC [Finalizer] [WARN ] JDBCContext.finalize() -
> unclosed connection, forgot to call close() on your session?
>
> it means when the session object will get garbage colleced at that time it
> will close the corresponding connection ....
> any other thoughts ?
>
> -----Original Message-----
> From: sandymac@gmail.com [mailto:sandymac@gmail.com]On Behalf Of Sandy
> McArthur
> Sent: Wednesday, April 19, 2006 2:35 AM
> To: Jakarta Commons Users List
> Subject: Re: [Commons DBCP] Cannot get a connection, pool exhausted
>
>
> There are two probable reasons for this.
>
> 1. You are experiencing high enough load that you've run out of
> concurrent connections.  If this is the case just bump the pool size,
> but this isn't as likely as #2 if it happens after a while.
>
> 2. You are forgetting to return/close/cleanup any sessions you create.
> It's been a while since I've used hibernate but there is a start/end
> transaction aspect. If you start a transaction, and never end it
> (maybe because of an exception), the transaction will never be freed.
>
> Since Hibernate uses a Dbcp connection which uses Pool to
> borrow/return objects if Pool never gets borrowed objects back then
> eventually the max number of concurrent active connections will be
> reached and pool won't allow any more objects to be borrowed and
> you'll get the NoSuchElementException you see that caused all those
> exceptions.
>
> You really need to return sessions/connections/objects in a finally
> block. With web programing you could get an exception at almost any
> time when the user clicks the stop button. Be very defensive.
>
> On 4/18/06, Preetam Palwe <pr...@aftek.com> wrote:
> > Hi All,
> >         I am using DBCP with Hibernate, when I deploy my server
> application it
> > works fine for some hours and after that for any successive request it
> > throws
> >
> > <stacktrace>
> > org.hibernate.exception.GenericJDBCException: Cannot open connection
> >         at
> >
> org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLSta
> > teConverter.java:82)
> >         at
> >
> org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
> >         at
> >
> org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
> > :43)
> >         at
> >
> org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
> > :29)
> >         at
> >
> org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:427)
> >         at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:168)
> >         at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:103)
> >         at
> > org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
> >         at
> >
> org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransa
> > ctionFactory.java:24)
> >         at
> > org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:231)
> >         at
> > org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1073)
> >           ...........
> >         at java.util.TimerThread.mainLoop(Timer.java:432)
> >         at java.util.TimerThread.run(Timer.java:382)
> > Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a
> > connection, pool exhausted
> >         at
> >
> org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.ja
> > va:103)
> >         at
> >
> org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:5
> > 40)
> >         at
> >
> com.xxx.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:243
> > )
> >         at
> >
> org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:424)
> >         ... 11 more
> > Caused by: java.util.NoSuchElementException: Timeout waiting for idle
> object
> >         at
> >
> org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPoo
> > l.java:756)
> >         at
> >
> org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.ja
> > va:95)
> >         ... 14 more
> > </stacktrace>
> >
> > Can anyone please tell me whets going wrong ?
> > here is my configuration
> >         j2sdk 1.4.2_6
> >         hibernate 3.0
> >         commons-pool 1.2
> >         commons-dbcp 1.2.1
> >         fedora core 3
> > and
> >         hibernate config file looks like
> >
> >         <property
> >
> name="hibernate.connection.provider_class">com.xxx.DBCPConnectionProvider</p
> > roperty>
> >         <property name="hibernate.connection.pool_size">20</property>
> >         <property name="hibernate.dbcp.initialSize">10</property>
> >         <property name="hibernate.dbcp.maxWait">10000</property>
> >         <property name="hibernate.dbcp.validationQuery">select
> 1</property>
> >
> > I have taken com.xxx.DBCPConnectionProvider from
> > http://wiki.apache.org/jakarta-commons/DBCP/Hibernate
> >
> >
> > Thanks&Regards
> > ~Preetam
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
>
>
> --
> Sandy McArthur
>
> "He who dares not offend cannot be honest."
> - Thomas Paine
>
> ---------------------------------------------------------------------
> 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
>
>


--
Sandy McArthur

"He who dares not offend cannot be honest."
- Thomas Paine

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


RE: [Commons DBCP] Cannot get a connection, pool exhausted

Posted by Preetam Palwe <pr...@aftek.com>.
thanks !
but
even if i dont close hibernate session, hibernate will close those for me
right ?
the finalize() of org.hibernate.jdbc.JDBCContext will do this job for me
right ?
i checked the hibernate log and i have seen ....
2006 04 19 11:20:53 ADC [Finalizer] [DEBUG] JDBCContext.finalize() - running
Session.finalize()
2006 04 19 11:20:53 ADC [Finalizer] [WARN ] JDBCContext.finalize() -
unclosed connection, forgot to call close() on your session?

it means when the session object will get garbage colleced at that time it
will close the corresponding connection ....
any other thoughts ?

-----Original Message-----
From: sandymac@gmail.com [mailto:sandymac@gmail.com]On Behalf Of Sandy
McArthur
Sent: Wednesday, April 19, 2006 2:35 AM
To: Jakarta Commons Users List
Subject: Re: [Commons DBCP] Cannot get a connection, pool exhausted


There are two probable reasons for this.

1. You are experiencing high enough load that you've run out of
concurrent connections.  If this is the case just bump the pool size,
but this isn't as likely as #2 if it happens after a while.

2. You are forgetting to return/close/cleanup any sessions you create.
It's been a while since I've used hibernate but there is a start/end
transaction aspect. If you start a transaction, and never end it
(maybe because of an exception), the transaction will never be freed.

Since Hibernate uses a Dbcp connection which uses Pool to
borrow/return objects if Pool never gets borrowed objects back then
eventually the max number of concurrent active connections will be
reached and pool won't allow any more objects to be borrowed and
you'll get the NoSuchElementException you see that caused all those
exceptions.

You really need to return sessions/connections/objects in a finally
block. With web programing you could get an exception at almost any
time when the user clicks the stop button. Be very defensive.

On 4/18/06, Preetam Palwe <pr...@aftek.com> wrote:
> Hi All,
>         I am using DBCP with Hibernate, when I deploy my server
application it
> works fine for some hours and after that for any successive request it
> throws
>
> <stacktrace>
> org.hibernate.exception.GenericJDBCException: Cannot open connection
>         at
>
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLSta
> teConverter.java:82)
>         at
>
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
>         at
>
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
> :43)
>         at
>
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
> :29)
>         at
>
org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:427)
>         at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:168)
>         at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:103)
>         at
> org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
>         at
>
org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransa
> ctionFactory.java:24)
>         at
> org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:231)
>         at
> org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1073)
>           ...........
>         at java.util.TimerThread.mainLoop(Timer.java:432)
>         at java.util.TimerThread.run(Timer.java:382)
> Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a
> connection, pool exhausted
>         at
>
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.ja
> va:103)
>         at
>
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:5
> 40)
>         at
>
com.xxx.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:243
> )
>         at
>
org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:424)
>         ... 11 more
> Caused by: java.util.NoSuchElementException: Timeout waiting for idle
object
>         at
>
org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPoo
> l.java:756)
>         at
>
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.ja
> va:95)
>         ... 14 more
> </stacktrace>
>
> Can anyone please tell me whets going wrong ?
> here is my configuration
>         j2sdk 1.4.2_6
>         hibernate 3.0
>         commons-pool 1.2
>         commons-dbcp 1.2.1
>         fedora core 3
> and
>         hibernate config file looks like
>
>         <property
>
name="hibernate.connection.provider_class">com.xxx.DBCPConnectionProvider</p
> roperty>
>         <property name="hibernate.connection.pool_size">20</property>
>         <property name="hibernate.dbcp.initialSize">10</property>
>         <property name="hibernate.dbcp.maxWait">10000</property>
>         <property name="hibernate.dbcp.validationQuery">select
1</property>
>
> I have taken com.xxx.DBCPConnectionProvider from
> http://wiki.apache.org/jakarta-commons/DBCP/Hibernate
>
>
> Thanks&Regards
> ~Preetam
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


--
Sandy McArthur

"He who dares not offend cannot be honest."
- Thomas Paine

---------------------------------------------------------------------
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: [Commons DBCP] Cannot get a connection, pool exhausted

Posted by Sandy McArthur <sa...@apache.org>.
There are two probable reasons for this.

1. You are experiencing high enough load that you've run out of
concurrent connections.  If this is the case just bump the pool size,
but this isn't as likely as #2 if it happens after a while.

2. You are forgetting to return/close/cleanup any sessions you create.
It's been a while since I've used hibernate but there is a start/end
transaction aspect. If you start a transaction, and never end it
(maybe because of an exception), the transaction will never be freed.

Since Hibernate uses a Dbcp connection which uses Pool to
borrow/return objects if Pool never gets borrowed objects back then
eventually the max number of concurrent active connections will be
reached and pool won't allow any more objects to be borrowed and
you'll get the NoSuchElementException you see that caused all those
exceptions.

You really need to return sessions/connections/objects in a finally
block. With web programing you could get an exception at almost any
time when the user clicks the stop button. Be very defensive.

On 4/18/06, Preetam Palwe <pr...@aftek.com> wrote:
> Hi All,
>         I am using DBCP with Hibernate, when I deploy my server application it
> works fine for some hours and after that for any successive request it
> throws
>
> <stacktrace>
> org.hibernate.exception.GenericJDBCException: Cannot open connection
>         at
> org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLSta
> teConverter.java:82)
>         at
> org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
>         at
> org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
> :43)
>         at
> org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
> :29)
>         at
> org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:427)
>         at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:168)
>         at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:103)
>         at
> org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
>         at
> org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransa
> ctionFactory.java:24)
>         at
> org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:231)
>         at
> org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1073)
>           ...........
>         at java.util.TimerThread.mainLoop(Timer.java:432)
>         at java.util.TimerThread.run(Timer.java:382)
> Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a
> connection, pool exhausted
>         at
> org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.ja
> va:103)
>         at
> org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:5
> 40)
>         at
> com.xxx.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:243
> )
>         at
> org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:424)
>         ... 11 more
> Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
>         at
> org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPoo
> l.java:756)
>         at
> org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.ja
> va:95)
>         ... 14 more
> </stacktrace>
>
> Can anyone please tell me whets going wrong ?
> here is my configuration
>         j2sdk 1.4.2_6
>         hibernate 3.0
>         commons-pool 1.2
>         commons-dbcp 1.2.1
>         fedora core 3
> and
>         hibernate config file looks like
>
>         <property
> name="hibernate.connection.provider_class">com.xxx.DBCPConnectionProvider</p
> roperty>
>         <property name="hibernate.connection.pool_size">20</property>
>         <property name="hibernate.dbcp.initialSize">10</property>
>         <property name="hibernate.dbcp.maxWait">10000</property>
>         <property name="hibernate.dbcp.validationQuery">select 1</property>
>
> I have taken com.xxx.DBCPConnectionProvider from
> http://wiki.apache.org/jakarta-commons/DBCP/Hibernate
>
>
> Thanks&Regards
> ~Preetam
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


--
Sandy McArthur

"He who dares not offend cannot be honest."
- Thomas Paine

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