Help! Could not retrieve a database connection

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Help! Could not retrieve a database connection

Post by jamieyg3 »

Hey

I made a custom login, and it seems to work 10 times but on the 11th time I get this msg:

Code: Select all

jvm 1    | 23:34:31.413 - [ WARNING ] > Could not retrieve a database connection: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
jvm 1    | org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
jvm 1    |      at org.apache.commons.dbcp.PoolingDriver.connect(PoolingDriver.java:183)
jvm 1    |      at java.sql.DriverManager.getConnection(Unknown Source)
jvm 1    |      at java.sql.DriverManager.getConnection(Unknown Source)
jvm 1    |      at it.gotoandplay.smartfoxserver.db.DbManager.getConnection(DbManager.java:218)
jvm 1    |      at sacredseasons.Database.tryLogin(Database.java:28)
jvm 1    |      at sacredseasons.Login.loginRequest(Login.java:93)
jvm 1    |      at sacredseasons.Login.handleInternalEvent(Login.java:75)
jvm 1    |      at it.gotoandplay.smartfoxserver.controllers.MessageHandler.dispatchEvent(MessageHandler.java:141)
jvm 1    |      at it.gotoandplay.smartfoxserver.controllers.SystemHandler.handleLoginRequest(SystemHandler.java:439)
jvm 1    |      at it.gotoandplay.smartfoxserver.controllers.SystemHandler.processEvent(SystemHandler.java:194)
jvm 1    |      at it.gotoandplay.smartfoxserver.controllers.SystemHandler.run(SystemHandler.java:143)
jvm 1    |      at java.lang.Thread.run(Unknown Source)
jvm 1    | Caused by: java.util.NoSuchElementException
jvm 1    |      at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:744)
jvm 1    |      at org.apache.commons.dbcp.PoolingDriver.connect(PoolingDriver.java:175)
jvm 1    |      ... 11 more
jvm 1    | java.lang.NullPointerException
jvm 1    |      at sacredseasons.Database.tryLogin(Database.java:32)
jvm 1    |      at sacredseasons.Login.loginRequest(Login.java:93)
jvm 1    |      at sacredseasons.Login.handleInternalEvent(Login.java:75)
jvm 1    |      at it.gotoandplay.smartfoxserver.controllers.MessageHandler.dispatchEvent(MessageHandler.java:141)
jvm 1    |      at it.gotoandplay.smartfoxserver.controllers.SystemHandler.handleLoginRequest(SystemHandler.java:439)
jvm 1    |      at it.gotoandplay.smartfoxserver.controllers.SystemHandler.processEvent(SystemHandler.java:194)
jvm 1    |      at it.gotoandplay.smartfoxserver.controllers.SystemHandler.run(SystemHandler.java:143)
jvm 1    |      at java.lang.Thread.run(Unknown Source)
jvm 1    | 23:34:31.414 - [ WARNING ] > SystemHandler: Invalid login request > java.lang.NullPointerException
now I already did a search on these forums for this and found something interesting, other people who are getting this error are also doing this:

Code: Select all

Connection con=db.getConnection();
which I am also doing, because I wanted to use PreparedStatements to avoid injection attacks. All I have to do to trigger this problem is try to login with the same user with a bad password 11 times.

now... if I don't use db.getConnection() I do not get this problem at all!!! but then I can't use the PreparedStatements...

so I don't mind not using db.getConnection() as long as there is a good way to clean (escape) strings before I put them into an sql statement!

Please advise me what to do!
shvilam
Posts: 118
Joined: 16 Mar 2008, 12:36
Location: Israel
Contact:

what is your code in finally part

Post by shvilam »

do you close the connection ?
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: what is your code in finally part

Post by jamieyg3 »

shvilam wrote:do you close the connection ?
thanks for the quick reply.

Yes the connection is being closed.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

You can change the policy of the pool to GROW in order to allow the pool of connection to become larger than the size specified in the config.
Check chapter 6.3 of the docs for all the details
Lapo
--
gotoAndPlay()
...addicted to flash games
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Post by jamieyg3 »

Lapo wrote:You can change the policy of the pool to GROW in order to allow the pool of connection to become larger than the size specified in the config.
Check chapter 6.3 of the docs for all the details
i saw that but something is not right. it works fine without having to grow as long as i dont use db.getConnection().
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

if you use getConnection() then you have to manually close that connection or it won't be returned to the pool
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply