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


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


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 祁阳县| 乌海市| 神池县| 太保市| 临江市| 景泰县| 闽侯县| 渝中区| 金秀| 北宁市| 乌拉特前旗| 伊通| 赤峰市| 舒兰市| 南昌市| 新田县| 濮阳市| 成武县| 龙岩市| 阿鲁科尔沁旗| 高安市| 交城县| 子洲县| 松阳县| 北辰区| 桂东县| 清原| 淮南市| 贵州省| 河西区| 普定县| 安义县| 井研县| 含山县| 忻城县| 韩城市| 博客| 丰原市| 合阳县| 台中市| 白河县|