nerotnt wrote:
baliukas wrote:
nerotnt wrote:
But tell me please, isn't this bad by not separating the persistence tier from the web tier!?
If you close session or/and commit transaction in filter then it does not mean your persistence tier is not separated from the web tier.
Transaction demarcation is not the web tier, this helper just implements interface form servlets API and API is not a tier. Web tier is a parameter parsing, validation, view rendering, ...
Transaction demarcation is the app logic tier, it is not the persistence tier too.
I recommend to separate transaction demarcation and resource management code from persistence tier, It is not persistence in the web tier !
What do mean by "Transaction demarcation"?
Do you mean this by having an intermediary that receives the clients requests and executes the business methods accordingly? This intermediary would be responsible for opening and closing the session?
How would save the clients session's? By putting them in the HttpSession of a user, it could get a little risky if we start forgetting to close the session's, right?
Put session to ThreadLocal or use session as method parameter. It maps transaction to thread, I have never used this way but I think it is not a good idea to map connection to httpsession.
There are a lot of ways to demarcate transaction (base class, interceptor, callback), but transaction per data access operation makes data access code not reusable and it is code dublication is not it ?
User create(String id,String psw){
//code dublication:
Session session = factory.getSession();
try{
Object obj = new User(id,psw);
session.save(obj);
session.flush();
session.getConnection().commit();
}finally{
session.close();
}
}
User create( Session session, String id,String psw){
// it is not very transparent, but "better" than the first way
Object obj = new User(id,psw);
session.save(obj);
}
User create(String id,String psw){
//it is dangerous, but no so bad too
Object obj = new User(id,psw);
this.session.save(obj);
}
User create(String id,String psw){
//current session is ThreadLocal. it "transparent" and "safe"
Session session = factory.currentSession();
Object obj = new User(id,psw);
session.save(obj);
}
abstract class AbstractAction{
abstract void doExecute();
final void execute(){
factory.enter();
try{
doExecute();
}catch(Trowable t){
factory.error(t);
}finally{
factory.exit();
}
}
}
class CreateUserAction{
void doExecute(){
this.user = userFactory.create(this.id,this.psw);
}
}