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
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?
Joined: Dec 03, 2008
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?
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: if you have a problem or question at its own please start a new topic for your own instead of hijacking other's topic.
Joined: Oct 08, 2006
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 ]