Error: I'm afraid this is the first I've heard of a "writeback" flavoured Blosxom. Try dropping the "/+writeback" bit from the end of the URL.
Neat ThreadLocal Trick
At my current job, at DAD, we try to be as conservative as possible in the technologies we use. So no Spring, no Ruby on Rails and we try to keep the AJAX magic to a minimum. The main reason for this is that we want developers, internal and external, to be as productive as possible as quick as possible and to straighten the learning curve to switch projects.
The framework that we are pushing right now is based on standard Struts, Hibernate and some tag libraries. I also introduced some neat little tricks that gives you some pretty cool cross-cutting features, just using the ThreadLocal class. It's the kind of code I love to write: simple, small and obvious when you have started using it. The other developers seem to like it as well, because it is non-intrusive to their work and, well, only has benefits.
One example: I'll first show the code, and then walk through it:
//omitted the imports
public class HibernateFilter implements Filter {
private static SessionFactory sessionFactory = null;
private static ThreadLocal sessions = new ThreadLocal();
public void init(FilterConfig config){
sessionFactory = //build a new instance...
}
public void doFilter(..., FilterChain chain){
chain.doFilter(...);
closeSession();
}
public static void closeSession(){
Session currentSession = (Session)sessions.get();
if (currentSession != null){
try{
currentSession.close();
} catch (HibernateException ex){
//log the ex
}
}
sessions.set(null);
}
public static Session getSession(){
Session currentSession = (Session)sessions.get();
if (currentSession == null){
currentSession = createSession();
sessions.set(currentSession);
}
return currentSession;
}
public static Session createSession(){
Session session = null;
try{
session = sessionFactory.openSession();
} catch (HibernateException ex){
//do something intelligent
} catch (SQLException ex){
//do something intelligent
}
return session ;
}
public void destroy(){
try{
sessionFactory.close();
} catch (HibernateException ex){
//log
}
sessionFactory = null;
sessions = new ThreadLocal();
}
}
When the filter is initialized the SessionFactory is started and whenever you call getSession() within the scope of a request, the same Session will be returned. When it doesn't exist yet, a new one is created. When the request finishes: the Session 'attached' to it is closed. Note that when you do not call getSession() anywhere in the request, nothing happens, which means that you can map the filter over your entire webapp, and there will be nearly no overhead when accessing static content.
It follows the one-Session-per-Request pattern, but doesn't require you to pass on requests and/or Sessions through all of your code. There is no injection needed from a container, no AOP configuration and nearly no code to do this. For the testing crowd: it is very easy to add a custom init() that would instantiate the static SessionFactory with a special test implementation. #