1、public 端新建接口,创建方法并将方法名带后缀_RequiresNew,注意这里的下划线是英文状态的下_,并抛出异常
public interface IPrivateUtilMaintain {
/**
* @Title executeAction__RequiresNew
* @Description 执行单据动作(独立事务),private端使用
* @param actionName
* @param billtype
* @param aggVO
* @return String
* @throws
*/
Object executeAction_RequiresNew(String actionName, String billtype, AggregatedValueObject aggVO) throws BusinessException;
}
2、private 创建该接口的实现类,并实现该方法,编写具体的业务逻辑
public class PrivateUtilMaintainImpl implements IPrivateUtilMaintain {
/*
* 执行单据动作(独立事务)
*/
@Override
public Object executeAction_RequiresNew(String actionName, String billtype, AggregatedValueObject aggVO) throws BusinessException {
return BaseDaoUtil.executeAction(actionName, billtype, aggVO);
}
}
3、通过调用接口方法实现该方法开启独立事务,使该方法与当前类中的事务分离,可以防止该方法出现异常后导致类中所有的数据库操作全部回滚,这样可以使用 try catch 来捕获异常,使正常的数据操作成功,异常操作可以自己记录日志。
try {
// 实例化接口
IPrivateUtilMaintain ipu = NCLocator.getInstance().lookup(IPrivateUtilMaintain.class);
// 保存
ArriveVO[] saveBase = (ArriveVO[]) ipu.executeAction_RequiresNew("SAVEBASE", "23", avo[0]);
// 审批
ipu.executeAction_RequiresNew("APPROVE", "23", saveBase[0]);
} catch (Exception e) {
// 记录异常日志
log.append(e.getMessage() + "\n");
}
备注:
1)因为是新建接口,所以要新建 upm 配置文件 xxxx.upm,并放在 META-INF 文件夹下
<?xml version='1.0' encoding='UTF-8'?>
<module name="xxxx">
<public>
<component remote="true" singleton="true" tx="CMT">
<interface>nc.itf.dao.IPrivateUtilMaintain</interface>
<implementation>nc.impl.dao.PrivateUtilMaintainImpl</implementation>
</component>
</public>
</module>
2)在打包补丁并部署在服务器时,要用 sysconfig 对该补丁模块进行 EJB 部署
3)因为 client 端是没有事务的,所以独立事务只应用于 private 端