aspose file tools*
The moose likes JDBC and the fly likes JDBC-connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC-connection" Watch "JDBC-connection" New topic
Author

JDBC-connection

mahavir swami
Greenhorn

Joined: Sep 18, 2009
Posts: 13
suppose we have a connection class ConnectionHelper, it contains methods for obtaining a connection and closing a connection


now we have method where connection from above class is used


now my question is when we say
getConnection of ConnectionHelper will be called,
now the drivermanager in connectionHelper will create a connection with db and the object that represents the
connection is stored at memory location say 0x1234
and the object "con1" of connectionHelper points to that location 0x1234
now in method userConnection()

hence con has a reference to con1 which actully refers to 0x1234 where actual connection information is stored.


now after closing the connection by calling


if we call userConnection() again
then since con1 is closed, it will create a new connection.
now this connection object must be somewhere in JVM heap memory , but this time when new object is created
will it again be created at same memory location or different???
if its stored at different location then what about the connection information at 0x1234?
and if every time new memory is used to store objects , won't the Heap memory get overflow?


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

soul reaver please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
This is not really a JDBC question. While your example uses a JDBC-created object, the same theory applies to all created objects.

Java is a "garbage collecting" language; the memory space of any object that no longer has any "live" references may at any time be freed. This is managed by one or more threads running concurrently with your application and happens automatically from time to time. The garbage collector also automatically performs some compaction.

When a new object is being created and there isn't sufficient free memory on the heap, the garbage collector will first attempt to reap any dead objects, and if that fails will attempt to expand the heap - up to the maximum specified by the startup parameters on the java process (which may be a default value), and if that fails, will throw a fatal OutOfMemory exception.

Here's a nice introduction:
http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html

While there's lots and lots of things for Java gurus to know about garbage collection, at your current level I'd say:
1. don't keep unnecessary references to objects
2. make sure you explicitly close the JDBC resources you open or use a resource pool; in particular, if you expect to use JDBC on a real-world application in a concurrent use environment (such as a web application) you MUST know about connection pooling, whether you use it or not...
3. don't try to outsmart the garbage collector; that's a great way to really screw things up, until you're a real uber-guru
4. if you have any more questions about Java memory management, please move to a more appropriate forum

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JDBC-connection