diff --git a/uapbd/src/private/nc/bs/uapbd/task/ims/MaterialOutGyImsPlugin.java b/uapbd/src/private/nc/bs/uapbd/task/ims/MaterialOutGyImsPlugin.java index a0d17ab5..afb10340 100644 --- a/uapbd/src/private/nc/bs/uapbd/task/ims/MaterialOutGyImsPlugin.java +++ b/uapbd/src/private/nc/bs/uapbd/task/ims/MaterialOutGyImsPlugin.java @@ -7,12 +7,10 @@ import nc.bs.logging.Logger; import nc.bs.pub.pa.PreAlertObject; import nc.bs.pub.taskcenter.BgWorkingContext; import nc.bs.pub.taskcenter.IBackgroundWorkPlugin; +import nc.bs.uapbd.util.ImsDaoUtil; import nc.bs.uapbd.util.MyHelper; import nc.itf.mmpac.pickm.IPickmQueryService; import nc.itf.uap.pf.busiflow.PfButtonClickContext; -import nc.jdbc.framework.JdbcSession; -import nc.jdbc.framework.PersistenceManager; -import nc.jdbc.framework.exception.DbException; import nc.jdbc.framework.processor.MapListProcessor; import nc.pubitf.ic.m4d.api.IMaterialOutMaintainAPI; import nc.util.mmf.busi.service.PFPubService; @@ -32,8 +30,6 @@ import nc.vo.scmpub.res.billtype.MMBillType; import nccloud.baseapp.core.log.NCCForUAPLogger; import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.SQLException; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.*; @@ -152,7 +148,7 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin { updateSql = updateSql.replace("[err_msg]", e.getMessage()); updateSql = updateSql.replace("[cgeneralhid]", cgeneralhid); NCCForUAPLogger.debug("MaterialOutGyImsPlugin-updateSql = " + updateSql); - int rows = executeUpdate(updateSql, cgeneralhid); + int rows = ImsDaoUtil.executeUpdate(updateSql, cgeneralhid); NCCForUAPLogger.debug("MaterialOutGyImsPlugin-rows = " + rows); } } @@ -160,7 +156,7 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin { // 修改状态为 完成 String inSql = SqlUtils.getInStr("cgeneralhid", imsIdSet.toArray(new String[0]), Boolean.TRUE); String updateSql = "update BIPOutMainTab set status = '2',err_msg = null where " + inSql; - executeUpdate(updateSql, imsIdSet.toString()); + ImsDaoUtil.executeUpdate(updateSql, imsIdSet.toString()); } Logger.error("---end----任务结束运行--"); } catch (Exception e) { @@ -193,66 +189,5 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin { } } - private int executeUpdate(String sql, String targetId) throws BusinessException { - - // 2. 直接通过静态工厂方法获取 PersistenceManager - PersistenceManager pm = null; - JdbcSession jdbcSession = null; - Connection conn = null; - int rows = 0; - try { - // 直接调用 PersistenceManager 的静态方法 getInstance() - pm = PersistenceManager.getInstance("gyims"); - pm.setAddTimeStamp(false);// 不添加时间戳ts - - // 2.1 获取 JdbcSession 和 Connection - jdbcSession = pm.getJdbcSession(); - jdbcSession.setSQLTranslator(true); // 保持 SQL 转换 - conn = jdbcSession.getConnection(); - conn.setAutoCommit(false); - logger.info("直接获取PersistenceManager,手动事务开启,targetId:" + targetId); - - // 4. 执行主表+子表操作 (传入共享的 pm) - logger.info("事务内执行更新,targetId:" + targetId); - rows = pm.getJdbcSession().executeUpdate(sql); - logger.info("事务内执行更新,rows:" + rows); - logger.info("事务内操作全部成功,准备提交,targetId:" + targetId); - - } catch (DbException | SQLException e) { // 捕获 DbException 和 SQLException - logger.error("事务执行失败,触发回滚,targetId:" + targetId); - if (conn != null) { - try { - conn.rollback(); - logger.info("事务回滚完成,targetId:" + targetId); - } catch (SQLException rollbackE) { - logger.error("事务回滚异常,targetId:" + targetId); - } - } - throw new BusinessException("备料计划同步事务失败:" + e.getMessage(), e); - - } finally { - if (conn != null) { - try { - conn.commit(); - logger.info("事务提交成功,targetId:" + targetId); - // 恢复自动提交模式,确保不会影响其他操作 - conn.setAutoCommit(true); - } catch (SQLException commitE) { - logger.error("事务提交异常,targetId:" + targetId); - } - } - // 5. 释放 PersistenceManager (至关重要) - if (pm != null) { - try { - pm.release(); // 必须调用 release() 将连接归还给连接池 - logger.info("PersistenceManager 释放完成,targetId:" + targetId); - } catch (Exception releaseE) { - logger.error("PersistenceManager 释放异常", releaseE); - } - } - } - return rows; - } - } diff --git a/uapbd/src/public/nc/bs/uapbd/util/ImsDaoUtil.java b/uapbd/src/public/nc/bs/uapbd/util/ImsDaoUtil.java new file mode 100644 index 00000000..3b1963c8 --- /dev/null +++ b/uapbd/src/public/nc/bs/uapbd/util/ImsDaoUtil.java @@ -0,0 +1,84 @@ +package nc.bs.uapbd.util; + + +import nc.bs.logging.Log; +import nc.jdbc.framework.JdbcSession; +import nc.jdbc.framework.PersistenceManager; +import nc.jdbc.framework.exception.DbException; +import nc.vo.pub.BusinessException; + +import java.sql.Connection; +import java.sql.SQLException; + +/** + * 高压加工车间-IMS-DAO 工具类 + * + * @author mzr + * @date 2025/10/08 + */ +public class ImsDaoUtil { + + private static final String LOG_INFO_NAME = "gymeslog"; + private static final Log logger = Log.getInstance(LOG_INFO_NAME); + + public static int executeUpdate(String sql, String targetId) throws BusinessException { + // 2. 直接通过静态工厂方法获取 PersistenceManager + PersistenceManager pm = null; + JdbcSession jdbcSession = null; + Connection conn = null; + int rows = 0; + try { + // 直接调用 PersistenceManager 的静态方法 getInstance() + pm = PersistenceManager.getInstance("gyims"); + pm.setAddTimeStamp(false);// 不添加时间戳ts + + // 2.1 获取 JdbcSession 和 Connection + jdbcSession = pm.getJdbcSession(); + jdbcSession.setSQLTranslator(true); // 保持 SQL 转换 + conn = jdbcSession.getConnection(); + conn.setAutoCommit(false); + logger.info("直接获取PersistenceManager,手动事务开启,targetId:" + targetId); + + // 4. 执行主表+子表操作 (传入共享的 pm) + logger.info("事务内执行更新,targetId:" + targetId); + rows = pm.getJdbcSession().executeUpdate(sql); + logger.info("事务内执行更新,rows:" + rows); + logger.info("事务内操作全部成功,准备提交,targetId:" + targetId); + + } catch (DbException | SQLException e) { // 捕获 DbException 和 SQLException + logger.error("事务执行失败,触发回滚,targetId:" + targetId); + if (conn != null) { + try { + conn.rollback(); + logger.info("事务回滚完成,targetId:" + targetId); + } catch (SQLException rollbackE) { + logger.error("事务回滚异常,targetId:" + targetId); + } + } + throw new BusinessException("备料计划同步事务失败:" + e.getMessage(), e); + + } finally { + if (conn != null) { + try { + conn.commit(); + logger.info("事务提交成功,targetId:" + targetId); + // 恢复自动提交模式,确保不会影响其他操作 + conn.setAutoCommit(true); + } catch (SQLException commitE) { + logger.error("事务提交异常,targetId:" + targetId); + } + } + // 5. 释放 PersistenceManager (至关重要) + if (pm != null) { + try { + pm.release(); // 必须调用 release() 将连接归还给连接池 + logger.info("PersistenceManager 释放完成,targetId:" + targetId); + } catch (Exception releaseE) { + logger.error("PersistenceManager 释放异常", releaseE); + } + } + } + return rows; + } + +}