« Return to Thread: The constant web design wall I keep hitting [long]
Well guys, since you're the savviest developers I've known,
I'm going to ask you this question.
Consider the following typical tiered stack:
public class SomeDao {
public
SomeDao(org.hibernate.Session session) {
/**
Stuff here **/
}
}
public class
SomeService {
public
SomeService(SomeDao dao) {
/**
More stuff :) */
}
}
public class
SomeController {
public
SomeController(SomeService service) {
/**
Seeing the pattern? :) **/
}
}
Now Consider the following assembly in NanoWAR:
MutablePicoContainer
mpc = new PicoBuilder()
.withBehaviors(new NanoWarStuff())
.withLifecycle().build();
mpc.as(REQUEST_SCOPE).addComponent(o.p.persistence.hibernate.ScopedSession.class)
.addComponent(SomeController.class)
.addComponent(SomeService.class)
.addComponent(SomeController.class);
So here’s my problem:
Since Hibernate Session is a request scope, that means:
·
SomeDao depends on Hibernate Session, it must
reside in Request Scope
·
SomeService depends on SomeDao which depends on
Hibernate: it must reside in Request Scope
·
And finally, SomeController depends on SomeService
which depends on SomeDao which depends on Hibernate: therefore it must reside
in the Request Scope.
The end result is that because one dependency is
request-level scope. EVERYTHING get dragged down with it, which equals
poor performance. (Reusable Pico can help here, but only so much). The
only non-threadlocal workaround I’ve come up with is instead of using
ScopedSession, have:
public class SomeDao {
private
final SessionFactory sessionFactory
public
SomeDao(org.hibernate.SessionFactory sessionFactory) {
this.sessionFactory
= sessionFactory;
}
public
void doSomething() {
Session
hibernateSession = sessionFactory.createSession();
try
{
/**
do action **/
}
finally {
hibernateSession.close();
}
}
}
Between my options, in many ways, this is worse
performance than before!
If I include thread local storage for each request object,
I can get it down to:
public class SomeDao {
/**
Request container would put a ThreadLocal session reference in ‘sessionReferenc’
**/
private
final HibernateSessionReference sessionReference;
public
SomeDao(HibernateSessionReference sessionReference) {
this.sessionRerence
= sessionReference;
}
public
void doSomething() {
Session
hibernateSession = sessionReference.get();
/**
Close is handled by the sessionReference factory **/
}
}
But I tend to agree that Threadlocal is the new static
and I try to avoid it whenever possible. But at least the
threadlocal here is limited to one class instead of propagating up the stack.
So. Any thoughts on other solutions to stop/minimize
request-level propagation?
-Mike
« Return to Thread: The constant web design wall I keep hitting [long]
| Free Forum Powered by Nabble | Forum Help |