aspose file tools*
The moose likes JDBC and the fly likes Reason for not keeping Connection instance as static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Reason for not keeping Connection instance as static" Watch "Reason for not keeping Connection instance as static" New topic
Author

Reason for not keeping Connection instance as static

Steve James
Ranch Hand

Joined: Apr 12, 2009
Posts: 44
Why is it fine to have static getConnection() method but not advisable to have a Connection instance as static



It would be great if someone helps with this .


I am just a brick in the wall of Java
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



first point is ,you cannot declare static variables inside a method.
Second , if you want only connection, declare as static connection variable. and open and close as application demands.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Can you open and close Connections as required? Please confirm that you can reopen a previously closed Connection.

And it's static not Static, Connection not connection. Remember case-sensitivity.
Steve James
Ranch Hand

Joined: Apr 12, 2009
Posts: 44
Can you open and close Connections as required? Please confirm that you can reopen a previously closed Connection.


Campbell ,

Please tell what do you mean by " that you can reopen a previously closed Connection"? please tell me what you want to indicate actually ?
Steve James
Ranch Hand

Joined: Apr 12, 2009
Posts: 44
Ritchie , not interested to reply back ?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Please tell what do you mean by " that you can reopen a previously closed Connection"? please tell me what you want to indicate actually ?


My guess is Campbell is asking Balu whether they know a way to reopen a closed connection (because its not normally possible).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


and open and close as application demands


I meant to get Connection when required and close it , and again get a new Connection as and when required by the application. ( in different methods)
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ah, that makes more sense!
Steve James
Ranch Hand

Joined: Apr 12, 2009
Posts: 44
This discussion added more confusion to me
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What is confusing you? What did you not understand? If you can ask some more direct questions perhaps we can help.

Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
It makes sense to have a getConnection to be static, because since it is a method, you can have centralized management for connection creation there, do some house-keeping if required [like, the number of connections issued, number of requests etc.... for management purpose, maybe].

But the problem with making the connection object itself static is that, then you will have only one connection instance [the one which you can access from classname.connection].... You'll be handicapped if your app is huge and requires a lot of connections to deal with databases [i mean, thats why we have database connection pools etc, right? how good is a db connection pool if all it has is one connection ?]

Does that help you ?

Dawn.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Steve James wrote:Ritchie , not interested to reply back ?
Some of us have other things to do. Read this FAQ.
Steve James
Ranch Hand

Joined: Apr 12, 2009
Posts: 44
It makes sense to have a getConnection to be static, because since it is a method, you can have centralized management for connection creation there, do some house-keeping if required [like, the number of connections issued, number of requests etc.... for management purpose, maybe



This was clear to me and also a great explanation .

But the problem with making the connection object itself static You'll be handicapped if your app is huge and requires a lot of connections to deal with databases


Confused here ,
Assume if i had implemented static Connection con ; in a Web Application where there are 100 Requests/Threads accessing the Application simultaneosly . If one Thread calls Connection.close , will all the other 99 request Connections get closed ??


Steve James
Ranch Hand

Joined: Apr 12, 2009
Posts: 44
oh i forgot to inform

class Mine
{
static Connection con ; // Thanks to Balu
}
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
If you defined a connection as static, then there will be only ONE connection available.ie

Connection con = ConnectionProvider.getConnection() // ConnectionProvider being an imaginary class which will return you an instance of connection.

and when a new request comes in your webapp, you'll again run the same command as above to get the connection... right?
Now, if you declare the Connection object as static, then the same "con" will be returned to you [ie, the Connection object will act like a singleton]

So, even if you have 100 requests coming, there will be only one Connection available to service them all.
Steve James
Ranch Hand

Joined: Apr 12, 2009
Posts: 44
hey Dawn , that was simply superb .

Thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reason for not keeping Connection instance as static
 
Similar Threads
ResultSet closed all the time
Singleton Instance
Singleton class for Database Connection
Here connectionFactory is an interface. how am i directly being able to access the method getConnec
Single tone Vs Static