Tuesday, February 14, 2012

ThreadLocal Best Practices

  • Be careful to initialize the ThreadLocal object whenever the thread starts working, or you might end up connecting a logged-off user's data to another user's thread. When you're using ThreadLocals in a situation in which a filter initializes the object with user data, make sure there are no other entries to your servlet application—except through the filter.
  • Always remember that the ThreadLocal data is connected to the thread, not the user or anything else. A good idea is to delete the ThreadLocal data when it's no longer used.
  • Use InheritableThreadLocal to pass parent ThreadLocal data to child threads.


References

Monday, February 6, 2012

Performance : Parameterized Constructor in Grails for Domain Objects

Don't use parametereized constructor in Grails for domain objects. This is slower than empty constructor.

This performance issue become significant when creating large number of domain objects in loop.

Wednesday, February 1, 2012

Overriding equals in Groovy

I was overriding equals method in for a Groovy class and got into StackOverflow error.




class ObjKey {
long id
String value

public boolean equals(Object obj) {
if (!(obj instanceof ObjKey)) {
return false;
}
if (this == obj) {
return true;
}
ObjKey rhs = (ObjKey) obj;
return new EqualsBuilder()
.append(id, rhs?.id)
.append(value, rhs?.value)
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(id)
.append(value)
.toHashCode();
}
}



I was getting stackoverflow at line: if (this == obj). Groovy's equals calls the equals method instead of comparing the reference.

I fixed it using bellow syntax: if (this.is(obj)). The corrected code looks like:


class ObjKey {
long id
String value

public boolean equals(Object obj) {
if (!(obj instanceof ObjKey)) {
return false;
}
if (this.is(obj)) {
return true;
}
ObjKey rhs = (ObjKey) obj;
return new EqualsBuilder()
.append(id, rhs?.id)
.append(value, rhs?.value)
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(id)
.append(value)
.toHashCode();
}
}

Thursday, January 20, 2011

List all tables in HSQLDB

SELECT * FROM INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_TYPE='TABLE'

Thursday, November 11, 2010

Setting up quick log4j for testing

Just put the following code on top of your code:


BasicConfigurator.configure();
log.setLevel(Level.DEBUG);