afunms

My Software,My Dream—Forge a more perfect NMS product.

dao scenario 2

/**
 * Second scenario:transaction is rare
 * we control connection inside service classes.
 */

/**
 * -----ConnectionManager.class---------
 */

   public static Connection getConnection()
   {
    return getConnection(ModuleConfig.getDefaultJndi());
   }
  
   public static Connection getConnection(final String jndi)
   {
    Connection conn = null;
    try
    {       
     DataSource ds = dsMap.get(jndi);
        if(ds==null) return null;
       
        conn = ds.getConnection();       
        conn.setAutoCommit(true);
    }
    catch(SQLException sqle)
    {
     SysLogger.error("Database fail to get connection 1",sqle);
    }
    catch(Exception sqle)
    {
     SysLogger.error("Database fail to get connection 2",sqle);
    }
    return conn;
   }
     
   public static void rollback(Connection conn)
   {
    try
    {
     if(conn==null || conn.isClosed())
      return;
    
     if(!conn.getAutoCommit())         
           conn.rollback();
    }
    catch(SQLException se)
    {
        SysLogger.error("Can not do rollback operation.",se);        
    }
   }  
}  

/**
 * -----ActionServlet.class---------
 */

   private void processHttpRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
   {   
    response.setContentType("text/html;charset=GB2312");  
    request.setCharacterEncoding("GB2312");  
   
    String beanId = extractBeanID(request.getRequestURI());
    String methodName = request.getParameter("method");   
    String targetJsp = null;
   
    if(beanId==null || methodName==null)
    {
     request.setAttribute(EXCEPTION_MESSAGE,"请求URI错误.");
     forward(request,response,targetJsp);
     return;
    }   
    BaseAction ba = BeanFactory.newAction(beanId);
    BaseService bs = BeanFactory.newService(beanId);
    if(ba==null || bs == null)
    {
     request.setAttribute(EXCEPTION_MESSAGE,"没有Bean为" + beanId + "的action或service");
     forward(request,response,targetJsp);
     return;
    }
     
    ActionAnnotation ann = AnnotationExtractor.getAnnotation(ba.getClass(), methodName);
    ba.setRequest(request);
    ba.setService(bs);              
    Method method = SysUtil.lookupMethod(ba.getClass().getMethods(),methodName);   

    Connection conn = null;
    if(ann.isNeedDB())
    {

     conn = ConnectionManager.getConnection();
     bs.setConnection(conn);
     bs.setDao(BeanFactory.newDao(beanId,conn));
    }
    if(method!=null)
    {
        try
        {         
            targetJsp = (String)method.invoke(ba);
        }
        catch(Exception e)
        {
         SysLogger.error("Error:" + bs.getClass().getName() + "." + method.getName(),e);
     targetJsp = null;
        }
       }
    if(ann.isNeedDB())
     ConnectionManager.close(conn);
       forward(request,response,targetJsp);
   } 
     
   /**
    * example:method in service class
    * operating conncetion in service
    */

 /**
  * 这是两个dao实现一个事处的最好例子
  */
 public void addTop(MenuDto dto) throws Exception
 {
  Connection conn = getConnection();
  try
                {   
   conn.setAutoCommit(false);
   
   MenuDao mDao = new MenuDao(conn);
   MenuRoleDao mrDao = new MenuRoleDao(conn);
   MenuDto menu = mDao.getNextMenu();
   menu.setTitle(dto.getTitle());
   mDao.save(menu);
   mrDao.saveMenu(menu.getId());
   
   conn.commit();
  }
  catch(Exception e)
  {    
   ConnectionManager.rollback(conn);
                        throw e;        
  }  
 }

posted on 2007-05-14 14:18 afunms 阅读(156) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

My Links

News

留言簿(18)

随笔档案

相册

搜索

最新评论

阅读排行榜