• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reason for not keeping Connection instance as static

 
Steve James
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritchie , not interested to reply back ?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that makes more sense!
 
Steve James
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This discussion added more confusion to me
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve James wrote:Ritchie , not interested to reply back ?
Some of us have other things to do. Read this FAQ.
 
Steve James
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh i forgot to inform

class Mine
{
static Connection con ; // Thanks to Balu
}
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Dawn , that was simply superb .

Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic