OutOfMemoryError: Java heap space

Ask around here!

OutOfMemoryError: Java heap space

Postby Patric » Tue May 31, 2011 2:34 pm

Hi Wallace,

there seems to be another serious problem with BoneCP that it fails with an OutOfMemoryError: Out of heap space.
It seems to leak some resources (My guess is that's related to the statement cache)

Just execute the application attached, it should fail within seconds.

- Patric
Attachments
BoneCP_out_of_heap_space_test.java
I forgot to remove the log4j dependency. Just remove this import line, sorry.
(4.05 KiB) Downloaded 181 times
Patric
 
Posts: 13
Joined: Fri May 27, 2011 3:43 pm

Re: OutOfMemoryError: Java heap space

Postby wwadge » Tue May 31, 2011 2:58 pm

You forgot to close off the prepared statement:

Try with:
Code: Select all
    PreparedStatement ps = lastConn.prepareStatement("SELECT 1 from DUAL");
                  ps.executeQuery();
                  ps.close();


instead of lastConn.prepareStatement("SELECT 1 from DUAL").executeQuery()
wwadge
Site Admin
 
Posts: 728
Joined: Mon Oct 19, 2009 7:50 pm

Re: OutOfMemoryError: Java heap space

Postby Patric » Tue May 31, 2011 3:16 pm

I am closing the statement (have a look at line 89):

Code: Select all
                  prepareStatement.close();


EDIT: You're right, there's another statement - I am sorry, I got confused.


But:
A connection pool should be immune to unclosed statements, especially when its connection is closed
Patric
 
Posts: 13
Joined: Fri May 27, 2011 3:43 pm

Re: OutOfMemoryError: Java heap space

Postby wwadge » Tue May 31, 2011 3:25 pm

I'm referring to line 61:
if (Math.random() < 0.5d && lastConn != null)
{
lastConn.prepareStatement("SELECT 1 from DUAL")
.executeQuery();

}


there you prepare a new statement on that connection, execute it right away but fail to close it.

I disagree with the pool having to close off your statements -- that would mean it has to keep track of each statement slowing down performance. You should always close off your resources when you're done with it.
wwadge
Site Admin
 
Posts: 728
Joined: Mon Oct 19, 2009 7:50 pm

Re: OutOfMemoryError: Java heap space

Postby Patric » Tue May 31, 2011 3:27 pm

I've updated my previous post in the meanwhile.
Sorry for confusing.

From the jdbc spec:

Code: Select all
9.4.4          Closing Connection Objects
An application calls the method Connection.close to indicate that it has finished
using a connection. All Statement objects created from a given Connection object
will be closed when the close method for the object is called.
Patric
 
Posts: 13
Joined: Fri May 27, 2011 3:43 pm

Re: OutOfMemoryError: Java heap space

Postby wwadge » Tue May 31, 2011 3:35 pm

Hmmm in that case I'll add that as an option (it doesn't make sense for people using managed connections such as spring jdbctemplate/hibernate). Would be nice if you could open a feature request here: https://bugs.launchpad.net/bonecp

Incidentally the pool already tracks statements and warns you if you forget to close properly (but this only works if you've got closeConnectionWatch + statement caching enabled). It also gives you a nice stack trace indicating where the statement was opened, the idea being: go fix your code.

Wallace
wwadge
Site Admin
 
Posts: 728
Joined: Mon Oct 19, 2009 7:50 pm

Re: OutOfMemoryError: Java heap space

Postby Patric » Tue May 31, 2011 3:50 pm

Done:
https://bugs.launchpad.net/bonecp/+bug/790759

I suggest that this option should be the default to not violate the jdbc spec.

Code: Select all
go fix your code.

There's nothing wrong with a code which doesn't close statements but their connection.
(although it's a questionable style)
Patric
 
Posts: 13
Joined: Fri May 27, 2011 3:43 pm


Return to BoneCP - Help, Q&A, Whatever!

Who is online

Users browsing this forum: No registered users and 2 guests