<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(164) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    My Links

    News

    留言簿(18)

    隨筆檔案

    相冊

    搜索

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 免费在线观影网站| 国产免费福利体检区久久| 亚洲精品国产免费| 亚洲精品456在线播放| 久久久久久久久久国产精品免费| 中文字幕精品无码亚洲字 | 成全高清在线观看免费| 亚洲精品你懂的在线观看| a级片在线免费看| 亚洲国产成人私人影院| 久久精品免费一区二区| 亚洲香蕉在线观看| 卡一卡二卡三在线入口免费| 亚洲AV无码国产剧情| 亚洲国产电影av在线网址| aa午夜免费剧场| 亚洲av鲁丝一区二区三区| 亚洲成人在线免费观看| 亚洲最大天堂无码精品区| 免费人妻无码不卡中文字幕18禁| 五月天婷婷免费视频| 久久亚洲精品成人| 国产福利在线观看免费第一福利| 亚洲AV日韩AV无码污污网站 | 久久精品无码专区免费青青| 亚洲国产精品综合福利专区| 日本牲交大片免费观看| 在线播放免费人成毛片乱码| 91亚洲自偷手机在线观看| 黄瓜视频高清在线看免费下载| 免费一级毛片在线播放放视频| 久久久久久亚洲av成人无码国产| 和日本免费不卡在线v| 搜日本一区二区三区免费高清视频| 亚洲AV无码国产丝袜在线观看| 亚洲av午夜成人片精品网站| 国产青草亚洲香蕉精品久久| 我要看免费的毛片| 18勿入网站免费永久| 亚洲AV无码资源在线观看| 国产在线观看免费视频播放器|