Try this. The initial reflection is certainly expensive, but if you're going to use it many many times, which I think you will, this is most certainly a better solution what what you're proposing. I don't like using reflection, but I find myself using it when I don't like the alternative to reflection. I do think that this will save your team a lot of headache, but you must pass the name of the method (in lowercase).
In other words, rather than pass "name", you would pass "fullname" because the name of the get method is "getFullName()".
Map<String, Method> methodMapping = null;
public Object getNode(String name) {
Map<String, Method> methods = getMethodMapping(contact.getClass());
return methods.get(name).invoke(contact);
}
public Map<String, Method> getMethodMapping(Class<?> contact) {
if(methodMapping == null) {
Map<String, Method> mapping = new HashMap<String, Method>();
Method[] methods = contact.getDeclaredMethods();
for(Method method : methods) {
if(method.getParameterTypes().length() == 0) {
if(method.getName().startsWith("get")) {
mapping.put(method.getName().substring(3).toLower(), method);
} else if (method.getName().startsWith("is"))) {
mapping.put(method.getName().substring(2).toLower(), method);
}
}
}
methodMapping = mapping;
}
return methodMapping;
}
If you need to access data contained within members of contact, you might consider building a wrapper class for contact which has all methods to access any information required. This would also be useful for guaranteeing that the names of the access fields will always remain the same (I.e. if wrapper class has getFullName() and you call with fullname, it will always work even if contact's getFullName() has been renamed -- it would cause compilation error before it would let you do that).
public class ContactWrapper {
private Contact contact;
public ContactWrapper(Contact contact) {
this.contact = contact;
}
public String getFullName() {
return contact.getFullName();
}
...
}
This solution has saved me several times, namely when I wanted to have a single data representation to use in jsf datatables and when that data needed to be exported into a report using jasper (which doesn't handle complicated object accessors well in my experience).
First off, you should be closing connections once you are done using them. That will help take care of the "too many connections" errors you're seeing. At an application level, you need to be releasing any shared resources you have as soon as you're done using them.
Second, it sounds like you're trying to duplicate the work that the underlying connection pool manager can provide for you. Specifically, at the application level, don't worry about the cost involved with opening / closing connections - that's what the pool manager handles for you. Because you haven't been closing off the connections once done, you're making it harder for the pool manager to be of use to you.
It's not clear if you have multiple, separate applications using separate pool managers or separate applications using the same pool manager. Regardless, using a HashMap with the connection string is a bad idea. At a minimum, you'll create inter-application communication issues. In the worst case, you're going to have to try and coordinate resources (connections) across applications in a web-server which is going to be problematic at best.
Finally, if you need to have multiple pools connecting to the same database server and the total number of connections across those pools is exceeding what the DB server can handle then you need to change that configuration on the DB server. If you truly need 11 connections and the DB server is only giving you 10, then you need to change the DB server.
To Summarize:
Best Answer
First let's clarify something: You mention that after assigning null to the variable you could forget to initialize it, but by assigning null to it you are in effect initializing it.
vs
So after you do
String s=null;
there's is no way that you could forget to initialize because you did initialize it.That being clear, I would recommend you to use a "smart default". In your case perhaps the empty string
""
would be a good default value if you want to avoidNullPointerException
. In the other hand, sometimes it is desirable that the program produce an exception because it indicates something wrong happened under the hood that should not have happened.