wood burning stoves 2.0*
The moose likes JDBC and the fly likes static connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "static connection" Watch "static connection" New topic
Author

static connection

Rohitash sharma
Greenhorn

Joined: Dec 03, 2008
Posts: 19
I am working on project and i have established connection.But the problem arised when i declared connection like
static connection con =null
My TL told me that we should not use this type of connection
I want to know why should we not declare connection to be static

Thanks in advance
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What do you hope to gain by declaring the Connection as a static variable?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rohitash sharma
Greenhorn

Joined: Dec 03, 2008
Posts: 19
I am completely blank on this concept. I usually use this concept specially on variables when I want to fix the value of any variable.
I tried this concept on connection as well
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Well, static will not "fix" the value of a variable. Its final you would use for this (though that doesn't stop the state of the object the variable is fixed to changing).

A static variable is usually used as a shared variable. For example:


Here the Connection varible is available to both methods in the class and any other class in your application. Why is this bad? A Connection has state that must be managed carefully. It is what all the database resources hang off. What if one class using the Connection closes it?

OK, so the reason other classes can do this in the first place is because it is public as well as static. So you could change your class to do this:

What does static give you in this instance? Well, here this looks dangerously like an early optimisation. One side effect of declaring something like this is it becomes statically bound, i.e. it is bound when the class loads and all instances of your MyDatabaseClass share it. Sharing something sounds like a good thing, however it actually normally gains you nothing programatically and can introduce problems. Because the Connection is shared all methods than interact with it need to be threadsafe, you can't have on thread updating the state of the connection while another is using it.

I'm one of those people who would question ever using static variables (with the possible exception of with imutable objects that are also declared final), given it gains you so little. If you flip my original question around and ask yourself what harm not using a static Connection causes can you come up with anything?
Rohitash sharma
Greenhorn

Joined: Dec 03, 2008
Posts: 19
Thanks Sir for your guidance But I want to ask one question If we use coding like this

private Connection con=null;

I read in a book that if we use this type of connection then we can only insert value in database but can't retrieve value .It is true? Why?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Rohitash sharma:
Thanks Sir for your guidance But I want to ask one question If we use coding like this

private Connection con=null;

I read in a book that if we use this type of connection then we can only insert value in database but can't retrieve value .It is true? Why?


No its not true. That is not a different "type" of connection, its just declared differently from a class variable. Where did you read this would be a problem?
Rohitash sharma
Greenhorn

Joined: Dec 03, 2008
Posts: 19
Actually if we use like this

private static Connection con=null;
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Again, not a different type of Connection, just a different way of declaring a variable. Like I asked earlier, what advantage does a static private Connection have over an instance variable?
[ December 23, 2008: Message edited by: Paul Sturrock ]
han smile
Greenhorn

Joined: Dec 24, 2008
Posts: 2
I do not know how this


Love java, love life
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30595
    
154

Originally posted by han smile:
I do not know how this

How to do what? Make an instance variable? What do you have so far?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Have you noticed that he's not the topicstarter?

Han Smile: if you have a problem or question at its own please start a new topic for your own instead of hijacking other's topic.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Ontopic: another disadvantage of declaring a connection static without closing it is that it will certainly timeout sooner or later depending on the DB configuration used. An average DB closes the connection after 10 ~ 60 mins of inactivity. If your application is for example running continuously in the background or in some container and the connection isn't been used for a while, then your application will completely break when the connection times out.

You should be acquiring and closing the connection in the shortest possible scope. Preferably already in the same method block. If you want to improve performance -connecting is a fairly expensive task- then consider using connection pooling. You still need to code your JDBC code the right way. The connection pooling implementation will worry about actually closing the connection or releasing back to the pool when you call Connection#close().
[ December 24, 2008: Message edited by: Bauke Scholtz ]
 
GeeCON Prague 2014
 
subject: static connection