论坛首页 Java版 DAO

BO,DAO如何来分层清晰以及Session何处关闭的问题

浏览 7709 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2007-07-25
Session session;
ColumnsDAO colDao;
public List getColList1(){ session = SessionFactory.getSession(); Transaction tx = session.beginTransaction();
colDao = new ColumnsDAO(session);
try { List colList = new ArrayList();
colList = colDao.getColList1();
tx.commit();
return colList;
}catch(HibernateException e){
throw e;
}finally{
}
}
   
0 请登录后投票
时间:2007-07-25
public  List getColList1(){    
         session = SessionFactory.getSession();    
         Transaction tx = session.beginTransaction();    
         colDao = new ColumnsDAO(session);    
         try {    
             List colList = new ArrayList();    
             colList = colDao.getColList1();    
             tx.commit();    
             return colList;    
          }catch(HibernateException e){    
           throw e;    
          }finally{    
          }    
     }
   
0 请登录后投票
时间:2007-07-25
<html><head><title>2222222</title></head><body>123123</body></html>
   
0 请登录后投票
时间:2008-03-28
晕掉. 楼上的在做什么

看的让我茫然了. 难道非Spring不行了?
   
0 请登录后投票
时间:2008-03-28
用动态代理 在你的getlist的方法用动态代理包装下,加入事务
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.hibernate.*

import com.strutslet.demo.service.SystemException;

public final class TransactionWrapper {

/**
* 装饰原始的业务代表对象,返回一个与业务代表对象有相同接口的代理对象
*/
public static Object decorate(Object delegate) {
return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(), new XAWrapperHandler(
delegate));
}

//动态代理技术
static final class XAWrapperHandler implements InvocationHandler {
private final Object delegate;

XAWrapperHandler(Object delegate) {
this.delegate = delegate;
}

//简单起见,包装业务代表对象所有的业务方法
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
session = SessionFactory.getSession();
try {
//开始一个事务
Transaction tx = session.beginTransaction();
//调用原始业务对象的业务方法
result = method.invoke(delegate, args);
session.flush();
tx.commit();
} catch (Throwable t) {
//回滚
ta.rollback();
throw new SystemException(t);
}

return result;
}
}
}
bo层事务就不用写了直接写个工厂,return
TransactionWrapper.decorate(new ColumnsDao())
这样就生成把事务封装进去的Dao了,如果不用事务也行,工厂类直接返回不事务处理的dao,我觉的可行
   
0 请登录后投票
时间:2008-03-30
不知道可以不可以用类似与sping的opensessioninview的方式,在过滤器中打开session,把这个session用ThreadLocal保存起来,在本次请求方法内都任何地方都可以使用!具体实现可能大家都会了,这里就只说一下思路,呵呵,不知道可以否!!!!
   
0 请登录后投票
论坛首页 Java版 DAO

跳转论坛:
JavaEye推荐
    快速回复 引用上一条消息 (Alt+S)