dao scenario 2

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;        
  }  
 }

你可能感兴趣的:(dao scenario 2)