【发布时间】:2015-10-17 17:04:30
【问题描述】:
我在下面有这个过程:
CREATE PROCEDURE updatepath()
BEGIN
declare cnt, n int;
update foo a set a.path=a.name where a.parent_id is null;
select count(*) into cnt from foo where path is null;
while cnt > 0 do
update foo a, foo b set a.path = concat(b.path, '/', a.name) where b.path is not null and a.parent_id = b.id;
select row_count() into cnt;
end while;
END;
;;
当我从 mysql 工作台调用这个过程时,如下所示:
调用 updatePath(); 它在几毫秒内成功执行。 但是,当我从 java 程序运行相同的代码时,会花费大量时间,最终我不得不终止 java 进程。 调用proc的代码如下:
{
jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con) throws SQLException {
CallableStatement cs = con.prepareCall("{call updatePath()}");
return cs;
}
},
new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs) throws SQLException {
cs.execute();
return null;
}
}
);
}
上述方法在单独的事务中运行。当我在一段时间后终止事务时,我看到它正在等待锁定。 在执行 show engine innodb status 时,我得到以下信息:
---TRANSACTION 4186138, ACTIVE 532 sec fetching rows mysql tables in use 2, locked 2 37 lock struct(s), heap size 6544, 2437 row lock(s), 撤消日志条目 307157 MySQL 线程 id 14,OS 线程句柄 0x2b38, 查询id 461758 localhost 127.0.0.1 root 发送数据 更新 foo a, foo b set a.path = concat(b.path, '/', a.name) 其中 b.path 不为空且 a.parent_id = b.id;
【问题讨论】:
标签: java mysql spring transactions locking