Forums Register Login

Connection Object as Static in a J2EE Application

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks to both of you ...
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1198 times.
Similar Threads
Covert Excel Worksheet to PDF
Which one is the best ?
JMS connection factory
connection pooling
Database Connection
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:31:38.