afunms

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

dao scenario 1

/**
 * First scenario:transaction is often used in the system
 * we control connection outside service classes.
 */

/**
 * -----ConnectionManager.class---------
 */
   public static Connection getConnection()
   {
    return getConnection(ModuleConfig.getDefaultJndi(),true);
   }

   public static Connection getConnection(final boolean auto)
   {
    return getConnection(ModuleConfig.getDefaultJndi(),auto);
   }
  
   public static Connection getConnection(final String jndi,final boolean auto)
   {
    Connection conn = null;
    try
    {       
     DataSource ds = dsMap.get(jndi);
        if(ds==null) return null;
       
        conn = ds.getConnection();       
        conn.setAutoCommit(auto);
    }
    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())
    {

   /**
    * -----get connection and set autoCommit to false---------
    */ 
    
conn = ConnectionManager.getConnection(false);
     bs.setConnection(conn);
     bs.setDao(BeanFactory.newDao(beanId,conn));
    }
    if(method!=null)
    {
        try
        {         
            targetJsp = (String)method.invoke(ba);
    /**
     * -----if method is executed successfully,commit connection---------
     */       

      if(ann.isNeedDB()) conn.commit();
        }
        catch(Exception e)
        {
         SysLogger.error("Error:" + bs.getClass().getName() + "." + method.getName(),e);
      /**
    * connection rollback when run into exception
    */
        
if(ann.isNeedDB()) ConnectionManager.rollback(conn);
      targetJsp = null;
        }
       }
    if(ann.isNeedDB())
     ConnectionManager.close(conn);
       forward(request,response,targetJsp);
   }  
      
      /**
     * example:method in service
  * there has not code for operating conncetion
     */
 
public void addTop(MenuDto dto) throws Exception
 {
  Connection conn = getConnection();
  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());
 }

posted on 2007-05-13 14:09 afunms 阅读(183) 评论(0)  编辑  收藏


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


网站导航:
 

My Links

News

留言簿(18)

随笔档案

相册

搜索

最新评论

阅读排行榜