How do you handle Java Singletons when a network connection fails? -


i have created singleton class contains instance of memcachedclient (this object requires making connection server providing concrete example). initialize memcachedclient in static init block. if memcachedclient cannot create instance due connection error, means class worthless because has null memcachedclient instance.

my question: best solution type of situation? how other people handling situation?

the question (as stated in title) broad, depends of singleton for.

in case, obvious question: if initialization fails, can expect issue solved in near future? cost of keep trying (redesign app not crashes downward in weird ways) instead of rebooting app?

if not expect error solved easily, crash application (with proper logs/alerts/notifications). if want keep running until issue resolves, steps be:

1) pass initialization constructor of singleton (why put in static block if can control access constructor).

2a) while constructor fails, calls getinstance return null. modify application able handle it.

2b) if issue present, create singleton without proper initialization. calls methods of objects nothing until initialization issue has been solved / object has been initialized. modify application able handle these dummy methods. method swifter way more complicated.

3) when new instance requested (if 2a) or method called (if 2b), check again if object can initialized.

compare cost of previous cost of restarting app after issue has been solved.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -