• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Object as Static in a J2EE Application

 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Is it a good practice to create DB connection objects as static member variables in a class, My opinion is we should not create them .


Need your help and assistance.


Thanks in advance
-Neha
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neha,

if you run a real Java enterprise application you will usually use an application server, too. Right? It's one of the features of an application server to manage database connections. And in my opinion it will do this job better because you have tested and stable connection pool management and so on already built-in. I'd recommend to use this!

If you want to use this you have to configure your database connections in your application server and the server will give you connections on demand with dependency injections. It's much simpler and better than to manage connections manually.

Marco
[ May 19, 2008: Message edited by: Marco Ehrentreich ]
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is poor practice to reference connections using instance variables, even worse to use class variables. Think about the consequences of some other thread closing your connection whilst you are using it!

Always be thread-safe and use local variables for your connections.
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic