Java Hibernate存储过程调用在多次调用时挂起

Java Hibernate存储过程调用在多次调用时挂起,java,mysql,hibernate,jpa,stored-procedures,Java,Mysql,Hibernate,Jpa,Stored Procedures,基本上我们有一堆数据库,然后是一个“主”数据库,它收集和计算一些统计数据 然后我可以调用一个存储过程,从给定的日期范围(然后保存在数据库中)获取统计信息 问题是,当我运行单元测试时,一切正常。对存储过程的单个调用将返回预期的数据。虽然当我多次尝试调用它时,fx使用以下小技巧: @Test public void multipleRequests() { StatisticManager statisticManager = new StatisticManager(); Li

基本上我们有一堆数据库,然后是一个“主”数据库,它收集和计算一些统计数据

然后我可以调用一个存储过程,从给定的日期范围(然后保存在数据库中)获取统计信息

问题是,当我运行单元测试时,一切正常。对存储过程的单个调用将返回预期的数据。虽然当我多次尝试调用它时,fx使用以下小技巧:

@Test
public void multipleRequests()  {

    StatisticManager statisticManager = new StatisticManager();
    List<Errorstatistic> allStats = new ArrayList<>();
    DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);

    for(int i = 1; i <= 31; i++){
        String string = "01/" + i + "/2017";

        Date date = null;
        try {
            date = format.parse(string);
        } catch (ParseException e) {
            System.out.println("Couldn't parse date.");
        }
        allStats.addAll(statisticManager.getNewStatistics(date));
    }
    statisticManager.persistNewStatistics(allStats);
}
我只是不知道发生了什么。我尝试过移除锁,插入长达8秒的睡眠时间,在循环中创建所有新的东西,但没有任何效果

有人能帮我吗

StatisticManager

public class StatisticManager {
    ErrorStatisticProcedureDAO obiDao = new ErrorStatisticProcedureDAO();
    ErrorStatisticDAO localDao = new ErrorStatisticDAO();

    public void persistNewStatistics(List<Errorstatistic> errorstatistics) {
        localDao.saveList(errorstatistics);
    }

    public List<Errorstatistic>  getNewStatistics(Date from) {
        List<Errorstatistic> originalStatistics = obiDao.getNewStatistics(setFrom(from), setTo(from));
        System.out.println("Received data.");
        return originalStatistics;
    }


    public List<Errorstatistic> getNewStatistics(Date from, Date to) {
        List<Errorstatistic> originalStatistics = obiDao.getNewStatistics(setFrom(from), setTo(to));
        System.out.println("Received data.");
        return originalStatistics;
    }



    private Date setFrom(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 01);
        return cal.getTime();
    }

    private Date setTo(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
}
公共类StatisticManager{
ErrorStatisticProcedureDAO obiDao=新的ErrorStatisticProcedureDAO();
ErrorStatisticDAO localDao=新的ErrorStatisticDAO();
公共无效持久性新闻统计(列表错误统计){
保存列表(errorstatistics);
}
公共列表GetNewsStatistics(日期从){
List originalStatistics=obiDao.getNewStatistics(setFrom(from),setTo(from));
System.out.println(“接收数据”);
回归原始统计;
}
公共列表GetNewsStatistics(日期从,日期到){
List originalStatistics=obiDao.getNewStatistics(setFrom(from),setTo(to));
System.out.println(“接收数据”);
回归原始统计;
}
私人日期设置自(日期){
Calendar cal=Calendar.getInstance();
校准设定时间(日期);
校准设置(日历小时/天,00);
校准设置(日历分钟,00);
校准设置(日历秒,01);
返回cal.getTime();
}
私人日期设置为(日期){
Calendar cal=Calendar.getInstance();
校准设定时间(日期);
校准设置(日历小时/天,23);
校准设置(日历分钟,59);
校准设置(日历秒,59);
返回cal.getTime();
}
}
DAO

    public class ErrorStatisticProcedureDAO {
    protected EntityManager em = ObiPersistenceManager.INSTANCE.getEntityManager();
    private Object lock = new Object();

    protected EntityManager begin() {
        synchronized (lock) {
            System.out.println("Begin called");
            if (!em.getTransaction().isActive()) {
                em.getTransaction().begin();
            }
            System.out.println("Returning em");
        }
        return em;
    }

    protected void close() {
        synchronized (lock) {
            System.out.println("Closing");
            if (em.isOpen()) {
                if (em.getTransaction().isActive()) {
                    em.getTransaction().commit();
                    em.clear();
                }
                System.out.println("Closed");
            }
        }
    }


    public List<Errorstatistic> getNewStatistics(Date from, Date to){
        System.out.println("Get statistics called");
        begin();
        StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("sp_errorratestat");
        storedProcedure.registerStoredProcedureParameter("fromTs", Long.class, ParameterMode.IN);
        storedProcedure.registerStoredProcedureParameter("toTs", Long.class, ParameterMode.IN);

        storedProcedure.setParameter("fromTs", (long) from.getTime() / 1000);
        storedProcedure.setParameter("toTs", (long)to.getTime() / 1000);

        System.out.println("Getting list");
        List data = storedProcedure.getResultList();
        System.out.println("Got list");
        List<Errorstatistic> errorstatistics = new ArrayList<>();
        Iterator itr = data.iterator();
        while (itr.hasNext()) {
            Object[] obj = (Object[]) itr.next();


            Errorstatistic errorstatistic = new Errorstatistic();
            errorstatistic.setDevicestate(obj[0] != null ? ((String) obj[0]) : null);
            errorstatistic.setIftype(obj[1] != null ? (String) obj[1] : null);
            errorstatistic.setServices(obj[2] != null ? (String) obj[2] : null);
            errorstatistic.setHardware(obj[3] != null ? (String) obj[3] : null);
            errorstatistic.setFirmware(obj[4] != null ?  (String) obj[4] : null);
            errorstatistic.setTicketstotal(obj[5] != null ? ((BigDecimal) obj[5]).intValueExact() : null);
            errorstatistic.setBootstotal(obj[6] != null ? ((BigDecimal) obj[6]).intValueExact() : null);
            errorstatistic.setCriticaldevices(obj[7] != null ? ((BigDecimal) obj[7]).intValueExact() : null);
            errorstatistic.setUnstabledevices(obj[8] != null ? ((BigDecimal) obj[8]).intValueExact() : null);
            errorstatistic.setDate(obj[9] != null ? (Date) obj[9] : null);
            errorstatistic.setDevicetime(obj[11] != null ? ((BigDecimal) obj[11]).doubleValue() : null);
            errorstatistic.setBootcausecountAdslFailure(obj[12] != null ? ((BigDecimal) obj[12]).intValueExact() : null);
            errorstatistic.setBootcausecountPllLosinglock(obj[13] != null ? ((BigDecimal) obj[13]).intValueExact() : null);
            errorstatistic.setBootcausecountPoweronHwreset(obj[14] != null ? ((BigDecimal) obj[14]).intValueExact() : null);
            errorstatistic.setBootcausecountServerdown(obj[15] != null ? ((BigDecimal) obj[15]).intValueExact() : null);
            errorstatistic.setBootcausecountSoftreset(obj[16] != null ? ((BigDecimal) obj[16]).intValueExact() : null);
            errorstatistic.setBootcausecountUnknownreset(obj[17] != null ? ((BigDecimal) obj[17]).intValueExact() : null);
            errorstatistic.setBootcausecountWatchdogreset(obj[18] != null ? ((BigDecimal) obj[18]).intValueExact() : null);
            errorstatistic.setDevicecount(obj[19] != null ? ((BigInteger) obj[19]).longValue() : null);

            errorstatistics.add(errorstatistic);
        }
        close();
        return errorstatistics;
    }
}
公共类错误统计过程DAO{
受保护的EntityManager em=ObiPersistenceManager.INSTANCE.getEntityManager();
私有对象锁=新对象();
受保护的EntityManager开始(){
已同步(锁定){
System.out.println(“开始调用”);
如果(!em.getTransaction().isActive()){
em.getTransaction().begin();
}
System.out.println(“返回em”);
}
返回em;
}
受保护的无效关闭(){
已同步(锁定){
系统输出打印项次(“关闭”);
如果(em.isOpen()){
if(em.getTransaction().isActive()){
em.getTransaction().commit();
em.clear();
}
系统输出打印项次(“关闭”);
}
}
}
公共列表GetNewsStatistics(日期从,日期到){
System.out.println(“调用获取统计数据”);
begin();
StoredProcedurey storedProcedure=em.createStoredProcedurey(“sp_errorratestat”);
storedProcedure.registerStoredProcedureParameter(“fromTs”,Long.class,ParameterMode.IN);
存储过程注册表存储过程参数(“toTs”,Long.class,ParameterMode.IN);
storedProcedure.setParameter(“fromTs”,(long)from.getTime()/1000);
storedProcedure.setParameter(“toTs”(长)到.getTime()/1000);
System.out.println(“获取列表”);
列表数据=storedProcedure.getResultList();
System.out.println(“获取列表”);
List errorstatistics=new ArrayList();
迭代器itr=data.Iterator();
while(itr.hasNext()){
Object[]obj=(Object[])itr.next();
Errorstatistic Errorstatistic=新的Errorstatistic();
errorstatistic.setDevicestate(对象[0]!=null?((字符串)对象[0]):null);
errorstatistic.SetIftType(obj[1]!=null?(字符串)obj[1]:null);
errorstatistic.setServices(obj[2]!=null?(字符串)obj[2]:null);
errorstatistic.setHardware(obj[3]!=null?(字符串)obj[3]:null);
errorstatistic.setFirmware(obj[4]!=null?(字符串)obj[4]:null);
errorstatistic.setTicketstotal(obj[5]!=null?((BigDecimal)obj[5])。intValueExact():null);
errorstatistic.SetBootTotal(obj[6]!=null?((BigDecimal)obj[6]).intValueExact():null);
errorstatistic.setCriticaldevices(obj[7]!=null?((BigDecimal)obj[7])。intValueExact():null);
errorstatistic.setUnstabledevices(obj[8]!=null?((BigDecimal)obj[8])。intValueExact():null);
errorstatistic.setDate(对象[9]!=null?(日期)对象[9]:null);
errorstatistic.setDevicetime(obj[11]!=null?((BigDecimal)obj[11]).doubleValue():null);
errorstatistic.SetBootCaseCountAdslFailure(obj[12]!=null?((BigDecimal)obj[12])。intValueExact():null);
errorstatistic.SetBootCaseCountPlllosingLock(obj[13]!=null?((BigDecimal)obj[13])。intValueExact():null);
errorstatistic.SetbootCaseCountPowerOnHWreset(obj[14]!=null?((BigDecimal)obj[14])。intValueExact():null);
errorstatistic.SetBootCaseCountServerDown(obj[15]!=null?((BigDecimal)obj[15])。intValueExact():null);
errorstatistic.SetBootCaseCountSoftReset(obj[16]!=null?((BigDecimal)obj[16])。intValueExact():null);
errorstatistic.SetBootCaseCountUnknownReset(obj[17]!=null?((BigDecimal)obj[17])。intValueExact():null);
errorstatistic.SetBootCaseCountWatchDogReset(obj[18]!=null?((BigDecimal)obj[18])。intValueExact():null);
errorstatistic.setDevicecount(obj[19]!=null?((BigInteger)obj[19]).longValue():null);
errorstatistics.add(errorstatistic);
}
close();
返回错误统计;
}
}
从本sni开始
    public class ErrorStatisticProcedureDAO {
    protected EntityManager em = ObiPersistenceManager.INSTANCE.getEntityManager();
    private Object lock = new Object();

    protected EntityManager begin() {
        synchronized (lock) {
            System.out.println("Begin called");
            if (!em.getTransaction().isActive()) {
                em.getTransaction().begin();
            }
            System.out.println("Returning em");
        }
        return em;
    }

    protected void close() {
        synchronized (lock) {
            System.out.println("Closing");
            if (em.isOpen()) {
                if (em.getTransaction().isActive()) {
                    em.getTransaction().commit();
                    em.clear();
                }
                System.out.println("Closed");
            }
        }
    }


    public List<Errorstatistic> getNewStatistics(Date from, Date to){
        System.out.println("Get statistics called");
        begin();
        StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("sp_errorratestat");
        storedProcedure.registerStoredProcedureParameter("fromTs", Long.class, ParameterMode.IN);
        storedProcedure.registerStoredProcedureParameter("toTs", Long.class, ParameterMode.IN);

        storedProcedure.setParameter("fromTs", (long) from.getTime() / 1000);
        storedProcedure.setParameter("toTs", (long)to.getTime() / 1000);

        System.out.println("Getting list");
        List data = storedProcedure.getResultList();
        System.out.println("Got list");
        List<Errorstatistic> errorstatistics = new ArrayList<>();
        Iterator itr = data.iterator();
        while (itr.hasNext()) {
            Object[] obj = (Object[]) itr.next();


            Errorstatistic errorstatistic = new Errorstatistic();
            errorstatistic.setDevicestate(obj[0] != null ? ((String) obj[0]) : null);
            errorstatistic.setIftype(obj[1] != null ? (String) obj[1] : null);
            errorstatistic.setServices(obj[2] != null ? (String) obj[2] : null);
            errorstatistic.setHardware(obj[3] != null ? (String) obj[3] : null);
            errorstatistic.setFirmware(obj[4] != null ?  (String) obj[4] : null);
            errorstatistic.setTicketstotal(obj[5] != null ? ((BigDecimal) obj[5]).intValueExact() : null);
            errorstatistic.setBootstotal(obj[6] != null ? ((BigDecimal) obj[6]).intValueExact() : null);
            errorstatistic.setCriticaldevices(obj[7] != null ? ((BigDecimal) obj[7]).intValueExact() : null);
            errorstatistic.setUnstabledevices(obj[8] != null ? ((BigDecimal) obj[8]).intValueExact() : null);
            errorstatistic.setDate(obj[9] != null ? (Date) obj[9] : null);
            errorstatistic.setDevicetime(obj[11] != null ? ((BigDecimal) obj[11]).doubleValue() : null);
            errorstatistic.setBootcausecountAdslFailure(obj[12] != null ? ((BigDecimal) obj[12]).intValueExact() : null);
            errorstatistic.setBootcausecountPllLosinglock(obj[13] != null ? ((BigDecimal) obj[13]).intValueExact() : null);
            errorstatistic.setBootcausecountPoweronHwreset(obj[14] != null ? ((BigDecimal) obj[14]).intValueExact() : null);
            errorstatistic.setBootcausecountServerdown(obj[15] != null ? ((BigDecimal) obj[15]).intValueExact() : null);
            errorstatistic.setBootcausecountSoftreset(obj[16] != null ? ((BigDecimal) obj[16]).intValueExact() : null);
            errorstatistic.setBootcausecountUnknownreset(obj[17] != null ? ((BigDecimal) obj[17]).intValueExact() : null);
            errorstatistic.setBootcausecountWatchdogreset(obj[18] != null ? ((BigDecimal) obj[18]).intValueExact() : null);
            errorstatistic.setDevicecount(obj[19] != null ? ((BigInteger) obj[19]).longValue() : null);

            errorstatistics.add(errorstatistic);
        }
        close();
        return errorstatistics;
    }
}
protected EntityManager begin() {
    synchronized (lock) {
        System.out.println("Begin called");
        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
        }
        System.out.println("Returning em");
    }
    return em;
}
protected EntityManager em = ObiPersistenceManager.INSTANCE.getEntityManager();