Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Alex Evans   on Sep 18 In Java Category.

  
Question Answered By: Mark R   on Sep 18

I used static  connections to make sure there is
only one connection  to the database  in the whole
application, shared between the client classes. But
never in a static initializer, I use instead a static
wrapper method to access it (getConnection() or
something), so this method takes the work of, in the
first call, make the connection, handle  connection
errors, etc and in the following calls to check if the
connection is still valid and make again the
connection if it's not alive.
Remember that static initializers are not executed
when the application  starts. They're executed when the
class is loaded. So if the class with the static
initializer is not used, it will not be loaded,
neither initialized. Therefore, your static
initializer will be executed in the first reference to
the class (somebody tell me if I'm wrong).

Share: