项目中的抛异常注意点

 1、如果增删改抛异常,则需要rollback(),在try块中commit();

     /**
     * description: 刪除數據
     * 
     * 
@param  iBaseVO
     *            IBaseVO
     * 
@param  locale
     *            String
     * 
@throws  BusiException
     *             業務異常
     * 
@throws  DbAccessException
     *             數據庫訪問異常
     
*/
    
private   void  del(IBaseVO iBaseVO, String locale)  throws  BusiException {
        
if  (LOG.isDebugEnabled()) {
            LOG.debug(
" [accountYmService] "   +   " [FUNCTION:delAccountYmTemp] "
                    
+   " [Param iBaseVO: "   +  iBaseVO  +   " ][begin] " );
        }
        AccountYmVO accountYmVO 
=  (AccountYmVO) iBaseVO;

        EntityManager mgr 
=  initiate();

        AccountYmTempDao ayTempDao 
=  AccountYmTempDao.getInstance(mgr);
        ParaMenuDataDao pmdDao 
=  ParaMenuDataDao.getInstance(mgr);
        EntityTransaction tx 
=   null ;
        tx 
=  mgr.beginTransaction();
        
try  {

            
//  插入一筆數據到參數設定表
            ParaMenuData pmData  =   new  ParaMenuData();
            pmData.setOperationCode(Constant.PROCESS_STATUS_DEL);
            pmData.setCrtDate(accountYmVO.getCrtDate());
            pmData.setCrtId(accountYmVO.getCrtId());
            pmData.setCrtPgm(accountYmVO.getCrtPgm());
            pmData.setCrtTime(accountYmVO.getCrtTime());
            pmData.setHandleStatus(Constant.PROCESS_STATUS_NOPROCESS);
            pmData.setMemo(
"" );
            pmData.setParaCode(Constant.PARACODE_ACCOUNT_YM);
            pmData.setSendDate(accountYmVO.getCrtDate());
            pmData.setSendId(accountYmVO.getCrtId());
            pmdDao.create(pmData);

            
//  插入一筆數據到臨時表
             this .createTemp(ayTempDao, accountYmVO, pmData.getSeqno(),
                    accountYmVO.getEndDate());
            tx.commit();

        } 
catch  (DbAccessException e) {
            LOG.error(
" [FUNCTION:del()] "
                    
+   " [DbAccessException] " , e);
            tx.rollback();
            
throw   new  BusiException(ExceptionConstant.DATABASE_ADD_EXCEPTION,
                    
"" , locale, ExceptionConstant.INFO_TYPE);
        }
    }

2、如果是查询,则不需要rollback(),只需要在finally块中commit()。

/**
 * 根據seqno查詢臨時表的待審核記錄
 * 
 * 
@param  seqno
 *            序號
 * 
@param  locale
 *            String
 * 
@return  List
 * 
@throws  BusiException
 
*/
public  List qryBySeqno( long  seqno, String locale)  throws  BusiException {

    ArrayList al 
=   null ;
    
if  (LOG.isDebugEnabled()) {
        LOG.debug(
" [AccountYmService] "
                
+   " [FUNCTION:qryAccountYmData][Param seqno: "   +  seqno
                
+   " ][begin] " );
    }

    EntityManager mgr 
=  initiate();
    AccountYmTempDao ayTempDao 
=  AccountYmTempDao.getInstance(mgr);

    EntityTransaction tx 
=   null ;
    AccountYmVO accountYmVO;
    tx 
=  mgr.beginTransaction();

    List list;
    
try  {
        list 
=  ayTempDao.selectBySeqno(seqno);
        tx.commit();
    } 
catch  (DbAccessException e) {
        LOG.error(
" [FUNCTION:qryBySeqno()] "   +   " [DbAccessException] " , e);
        tx.rollback();
        
throw   new  BusiException(
                ExceptionConstant.DATABASE_ACCESS_EXCEPTION, 
"" , locale,
                ExceptionConstant.INFO_TYPE);
    } 

    
if  (list  !=   null   &&  list.size()  >   0 ) {
        
if  (LOG.isDebugEnabled()) {
            LOG.debug(
" [FUNCTION:qryBySeqno()] "   +   " select list success! " );
        }

        al 
=   new  ArrayList();
        
try  {
            AccountYmTemp accountYmTemp;
            
for  ( int  i  =   0 ; i  <  list.size(); i ++ ) {
                accountYmVO 
=   new  AccountYmVO();
                accountYmTemp 
=  (AccountYmTemp) list.get(i);
                BeanUtils.copyProperties(accountYmVO, accountYmTemp);
                BeanUtils
                        .copyProperties(accountYmVO, accountYmTemp.getId());

                al.add(accountYmVO);
            }
        } 
catch  (InvocationTargetException e) {
            LOG.error(
" [FUNCTION:qryBySeqno()] "
                    
+   " [InvocationTargetException] " , e);
            tx.rollback();
            
throw   new  BusiException(
                    ExceptionConstant.INVOCATION_TARGET_EXCEPTION, 
"" ,
                    locale, ExceptionConstant.INFO_TYPE);
        } 
catch  (IllegalAccessException e) {
            LOG.error(
" [FUNCTION:qryBySeqno()] "
                    
+   " [IllegalAccessException] " , e);
            tx.rollback();
            
throw   new  BusiException(
                    ExceptionConstant.ILLEGAL_ACCESS_EXCEPTION, 
"" , locale,
                    ExceptionConstant.INFO_TYPE);
        } 
finally  {
            tx.commit();
        }
    } 
else  {
        tx.commit();
        
throw   new  BusiException(ExceptionConstant.NOT_FOUND_SEQNO_IN_TEMP,
                
"" , locale, ExceptionConstant.INFO_TYPE);
    }
    
    
return  al;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值