数据库插入的多线程问题

时间:2015-08-12 09:38:33

标签: java multithreading spring

我有一个JAVA代码,可以被多个线程访问。

synchronized (this.getClass()) 
{
    System.out.println("stsrt");
    certRequest.setRequestNbr(
        generateRequestNumber(
            certInsuranceRequestAddRq.getAccountInfo().getAccountNumberId()));
    System.out.println("outside funcvtion"+certRequest.getRequestNbr());
    reqId = Utils.getUniqueId();
    certRequest.setRequestId(reqId);
    System.out.println(reqId);
    ItemIdInfo itemIdInfo = new ItemIdInfo();
    itemIdInfo.setInsurerId(certRequest.getRequestId());
    certRequest.setItemIdInfo(itemIdInfo);
    dao.insert(certRequest);
    addAccountRel();
    System.out.println("end");
}

函数generateRequestNumber()根据从两个数据库表中获取的数据生成请求编号。

public String generateRequestNumber(String accNumber) throws Exception 
{
    String requestNumber = null;
    if (accNumber != null) 
    {
        String SQL_QUERY = "select CERTREQUEST.requestNbr from CertRequest as CERTREQUEST, "
                + "CertActObjRel  as certActObjRel where  certActObjRel.certificateObjkeyId=CERTREQUEST.requestId "
                + " and certActObjRel.certObjTypeCd=:certObjTypeCd "
                + " and certActObjRel.certAccountId=:accNumber ";

        String[] parameterNames = { "certObjTypeCd", "accNumber" };
        Object[] parameterVaues = new Object[] 
        {
            Constants.REQUEST_RELATION_CODE, accNumber 
        };
        List<?> resultSet = dao.executeNamedQuery(SQL_QUERY,
                parameterNames, parameterVaues);

        // List<?> resultSet = dao.retrieveTableData(SQL_QUERY);
        if (resultSet != null && resultSet.size() > 0) {
            requestNumber = (String) resultSet.get(0);
        }
        int maxRequestNumber = -1;
        if (requestNumber != null && requestNumber.length() > 0) {
            maxRequestNumber = maxValue(resultSet.toArray());
            requestNumber = Integer.toString(maxRequestNumber + 1);
        } else {
            requestNumber = Integer.toString(1);
        }
        System.out.println("inside function request number"+requestNumber);
        return requestNumber;
    }
    return null;
}

CertRequest中使用的表CertActObjRelgenerateRequestNumber()分别由我的初始代码中使用的函数"dao.insert(certRequest);""addAccountRel();"更新。此外,我的初始代码中使用的System.out.println()语句也有以下输出。

stsrt
inside function request number73
outside funcvtion73
A1664886-5F84-45A9-AB5F-C69768B83EAD
end
stsrt
inside function request number73
outside funcvtion73
44DAD872-6A1D-4524-8A32-15741FAC0CA9
end

如果您注意到两个线程都以同步方式运行,但是生成请求号时,它们是相同的。我的假设是CertRequestCertActObjRel的数据库更新在两个线程完成执行时完成。

有人可以帮我解决这个问题吗?

0 个答案:

没有答案