Mysql函数没有返回对Hibernate的响应

时间:2014-01-16 09:00:30

标签: java mysql sql hibernate

我有一个mysql函数,它是使用HibernateCallback类的Spring从Web应用程序的服务类调用的。此函数使用游标选择一些行,然后执行一些更新和插入。然后它返回一个varchar。

当光标选择的行数百时,一切都很好。但是当行数为12000时,调用java代码没有收到任何响应,即使是1小时。

当我们检查mysql慢查询日志和错误日志时,它显示mysql函数已在8秒内运行,锁定时间为0.7秒。但java程序没有得到任何响应。此外,无论何时调用此函数,mysql进程列表都会非常高。

什么可能是没有响应的原因,根据mysql日志,函数已运行?

功能如下:

CREATE FUNCTION fun_test(
    param1 varchar(15),
    param2 varchar(20),
    v_startTime timestamp,
    v_endTime timestamp)
RETURNS varchar(50) DETERMINISTIC
begin
    declare v_no_more_rows BOOLEAN;
    declare v_id int ;
    declare v_count int default 0;
    declare v_amount double ;
    declare v_totalAmount double default 0;

    DECLARE cur1 CURSOR FOR
        SELECT id,amount 
            from table1 
            where col1=param1 
                and updatedat>= v_startTime 
                and updatedat< v_endTime 
                and amount>0;

    DECLARE CONTINUE HANDLER FOR NOT FOUND
            SET v_no_more_rows = TRUE;

    OPEN cur1;
    loop_cur1: LOOP

        FETCH cur1
        INTO v_id,v_amount;
        IF v_no_more_rows THEN
            CLOSE cur1;
            LEAVE loop_cur1;
        END IF;      

        insert into table2 (parentid,createdAt) values(v_id,current_timestamp());
        update table1 
            set updatedat = current_timestamp()
            where
            id = v_id;
            set v_totalAmount=v_totalAmount+v_amount;
            set v_count=v_count+1;

    END LOOP loop_cur1;

    return concat("SUCCESS:",v_totalAmount,":",v_count);

end ;

编辑:为此函数添加mysql慢查询日志结果:

 Time: 140116 11:22:39
  User@Host: host @  [x.x.x.x]
  Query_time: 7.868044  Lock_time: 0.852301 Rows_sent: 1  Rows_examined: 0
  SET timestamp=1389851559;
  select fun_test('a','b','2014-01-01 11:00:00','2014-01-01 12:00:00');

0 个答案:

没有答案