aspose file tools*
The moose likes JDBC and the fly likes questions in jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "questions in jdbc" Watch "questions in jdbc" New topic
Author

questions in jdbc

sridhar gandikota
Ranch Hand

Joined: Jul 09, 2008
Posts: 31
Hi Ranchers,

I have two question in JDBC

1) When a connection is created like in below program , if we close connection object wont it be enough why should we close resultset,statement and connection all three.
2)Can we know which class file has kept the connection open(if there are any open connections).Can we write a small program for that?




Regards,
Sridhar
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

I updated your post to add code taggs

1)
We close all these objects to release resources.
In a normal scenario, the connection is a long living object. If you would leave all resources open until the connection closes, you would run out of resources.

2)
don't know.


OCUP UML fundamental and ITIL foundation
youtube channel
Anton Shaykin
Ranch Hand

Joined: Dec 13, 2009
Posts: 57

To partially answer your second question, I might advise you to query your database metadata. For example in Oracle you can query v$session data dictionary (assuming you have sufficient privileges) to list all sessions opened by all users. From there you can find out process id of process that opened a connection. But I doubt that you can obtain such a fine-grained information (such as what class initiated connection).
sridhar gandikota
Ranch Hand

Joined: Jul 09, 2008
Posts: 31
Thanks Jan and Anton for your prompt reply.

Anton,

Any possibility that web server or app server which can do this job?

Regards,
Sridhar
Anton Shaykin
Ranch Hand

Joined: Dec 13, 2009
Posts: 57

Sridhar,
It might help if you elaborate on that. I'm kind of confused over what you're trying to do.
sridhar gandikota
Ranch Hand

Joined: Jul 09, 2008
Posts: 31
If we have class A has opened Connection through connection object and has not closed and similarly with Class B,Class C etc

What I want is

Class Name Connections Open
Class A 1
Class B 2
Class C 3

Something like this.

Please let me know if this is possible through program or is it possible through with the Admin module of server?

With Regards,
Sridhar
Anton Shaykin
Ranch Hand

Joined: Dec 13, 2009
Posts: 57

Still I don't get what you mean by "Connections opened by class". All your classes eventually will be compiled into single program, which when executed will create new operating system process, and you can look up how many connections were opened by this process by querying database metadata. So there is no sense in tracking how many connections were created by particular class. Normally, you would have some sort of connection factory or pool, which would allow to reuse existing connections rather than creating new ones. Most of the application servers support connection pooling out of the box.
sridhar gandikota
Ranch Hand

Joined: Jul 09, 2008
Posts: 31
Hi Anton,

Sorry for my "explaining words" .

I wanted to know is there any way where I could get to know there number of open connections open by a program and so that I can go to that class and put code like connection.close() so that it will solve the open connection issue.

Regards,
Sridhar
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

It depends on the driver, but there probably isn't any way to know (from the outside) how many connections are opened by a program. And even if there were, what good would that be? You still wouldn't know which of the 200 classes which make up the program had been written badly. You're just going to have to go through the code and find classes which use connections.

On the other hand, that shouldn't be too hard if you use a tool which allows you to search the whole code base for particular strings.
sridhar gandikota
Ranch Hand

Joined: Jul 09, 2008
Posts: 31
Thanks Paul,

I guess I have to use tool to search the whole code .


Regards,
Sridhar
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

Well, yeah. That seems a lot more straightforward to me than your idea of looking at the server and trying to trace connections back to programs and classes, anyway.
Anton Shaykin
Ranch Hand

Joined: Dec 13, 2009
Posts: 57

Paul Clapham wrote:there probably isn't any way to know (from the outside) how many connections are opened by a program

Actually, there is. Like I said, just query database metadata. Definitely it's vendor-specific, but at least Oracle allows you to query such stuff.

But for sure, managing connections opened by your program is totally your responsibility, unless you rely on some third-party connection factory mechanism.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: questions in jdbc