Prasanna
Prasanna

Reputation: 3

getConnection() with out assign variable will leak a connection

I am using the jdbc connection pooling and if I write code like

DatabaseMetaData dbMeta = getConnection().getMetaData(); 

where getConnection() results connection object. Is this results connection leak? Here the returned connection is not explicitly closing.

Upvotes: 0

Views: 164

Answers (1)

Rishal
Rishal

Reputation: 1538

As per JavaDocs https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html

Connection
A connection (session) with a specific database. SQL statements are executed and results are returned within the context of a connection.

whereas getMetaData() is for Retrieving DatabaseMetaData object that contains metadata about the database to which this Connection object represents a connection. The metadata includes information about the database's tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on. Returns: a DatabaseMetaData object for this Connection object

So getMetaData() can be used to validate the connection object validity also getConnection() is responsible for creating connection object and getMetaData() can be used to get the information related to above stated points. Also as stated all the points above you have to make sure your connections are closed to avoid any memory leakage after use of each connection.

Upvotes: 1

Related Questions