多个请求从数据库中获得相同的价值

时间:2018-09-27 10:25:56

标签: java mysql synchronization thread-safety

我正在从数据库中获取预先创建的WorkItem,有时会同时获取两个请求,这些请求获得与WorkItem相同的值,这会引起问题。 我的代码如下:-

private synchronized Long FetchPreCreatedWI(String planName,String queueName) {
    Long TrailID = null;
    try{
        List<Object> preCreatedWI = null;
        if("OrderManagement".equalsIgnoreCase(planName)){
            preCreatedWI = beanDataManager.executeSQLQuerybyObject("select tec.ENACTMENT_TRAIL_ID from TRAIL_EXECUTION_CONTEXT tec where "
                    + "tec.CURRENT_JOB_STATE_NAME ='PREACQUIRED' and tec.CURRENT_QUEUE_NAME = 'PreAcquired' and LAST_USER_COMMENTS is null order by ENACTMENT_TRAIL_ID limit 1");
        }else{
            preCreatedWI = beanDataManager.executeSQLQuerybyObject("select tec.ENACTMENT_TRAIL_ID from TRAIL_EXECUTION_CONTEXT tec where "
                    + "tec.CURRENT_JOB_STATE_NAME ='PREACQUIRED' and tec.CURRENT_QUEUE_NAME ='" + queueName + "' and tec.PLAN_NAME ='" + planName + "' and LAST_USER_COMMENTS is null order by ENACTMENT_TRAIL_ID limit 1");
        }
        if (!ListUtils.isEmptyList(preCreatedWI)) {
            TrailID = Long.parseLong(preCreatedWI.get(0).toString());
            if("OrderManagement".equalsIgnoreCase(planName)){
                beanDataManager.executeUpdate("update TRAIL_EXECUTION_CONTEXT "
                        +"inner join SECURITY_SECRESOBJECT sso on sso.ENACTMENT_JOB_ID=TRAIL_EXECUTION_CONTEXT.CURRENT_JOB_ID "
                        +"inner join SECURITY_ACL4QL saq on saq.SECRESOBJECT_ID = sso.ID "
                        +"inner join SECURE_JOB_HISTORY sjh on sso.ID = sjh.SECURE_JOB_ID "
                        +"inner join BEM_FIELDVALUESTRING bfv on TRAIL_EXECUTION_CONTEXT.BEM_ENTITYINSTANCE_ID = bfv.BEM_ENTITYINSTANCE_ID "
                        +"set TRAIL_EXECUTION_CONTEXT.CURRENT_QUEUE_ID = (select ID from ENACTMENT_QUEUE where queue_name = '"+queueName+"'), "
                        +"TRAIL_EXECUTION_CONTEXT.CURRENT_QUEUE_NAME = '"+queueName+"', "
                        +"TRAIL_EXECUTION_CONTEXT.LAST_USER_COMMENTS = 'Job Picked', "
                        +"saq.QU_ID = (select QU_ID from ENACTMENT_QUEUE where queue_name = '"+queueName+"'), "
                        +"sjh.OLD_QUEUE_NAME = '"+queueName+"', "
                        +"bfv.bpi_factValueString005 = '"+queueName+"' "
                        +"where TRAIL_EXECUTION_CONTEXT.ENACTMENT_TRAIL_ID ="+TrailID);
            }else{
                beanDataManager.executeUpdate("update TRAIL_EXECUTION_CONTEXT set LAST_USER_COMMENTS = 'Job Picked' where ENACTMENT_TRAIL_ID = "+TrailID);
            }
        }
    }catch(Exception e){
        if (logEnabled && logger.isLogLevel(LogLevel.ERROR)) {
            logger.log(LogLevel.ERROR, e.getMessage(), e);
        }
        return TrailID;
    }

如何控制请求的流,以便如果执行了一个查询,则必须执行第二个查询而没有上下文切换。就像在更新查询中一样,我只是在更改获取的WI的queueName。

0 个答案:

没有答案