java - 简单更新已锁定

标签 java mysql spring hibernate innodb

我们有一个非常简单的 InnoDB 数据库。只是一堆带有外键的表,没有触发器或类似的东西。

我们正在使用 Tomcat、Spring 和 Hibernate 编写 Java 应用程序。

当我们执行非常简单的操作导致对该表进行简单更新时,更新被卡住(锁定),并且所有其他请求都因为这些锁而超时。来自 MySQL 的确切错误是: Lock wait timeout exceeded;尝试重新启动事务

在MySQL中,每次事务中总有2个进程涉及这个锁:一个是Updating,一个是Sleeping。我们发现这有点奇怪,也许他们陷入了僵局?为什么?

当我们停止我们的应用程序并手动运行更新时,它始终有效。所以 table 可能没问题。

Hibernate 配置为自动提交事务,因此我们的应用程序中没有事务处理,所有事务都由 Hibernate 处理。

我们已经尝试过:

  • 更新 hibernate
  • 更新 MySQL(5.1 到 5.5)
  • 将 MySQL 配置更改为 this .

显示 INNODB 状态:

=====================================
150113 16:01:57 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 42 seconds
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 439, signal count 426
Mutex spin waits 0, rounds 5145, OS waits 236
RW-shared spins 368, OS waits 184; RW-excl spins 18, OS waits 16
------------
TRANSACTIONS
------------
Trx id counter 0 15578322
Purge done for trx's n:o < 0 15577923 undo n:o < 0 0
History list length 9
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, process no 1599, OS thread id 140351479547648
MySQL thread id 1150, query id 15431 localhost 127.0.0.1 root
SHOW ENGINE INNODB STATUS
---TRANSACTION 0 15578321, not started, process no 1599, OS thread id 140351478748928
MySQL thread id 1149, query id 15343 localhost redmine_admin
---TRANSACTION 0 15578063, not started, process no 1599, OS thread id 140349944846080
MySQL thread id 879, query id 15207 localhost 127.0.0.1 root
---TRANSACTION 0 0, not started, process no 1599, OS thread id 140349945112320
MySQL thread id 878, query id 15207 localhost 127.0.0.1 root
---TRANSACTION 0 15578218, ACTIVE 318 sec, process no 1599, OS thread id 140351479281408 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1216, 1 row lock(s)
MySQL thread id 1121, query id 15351 localhost 127.0.0.1 root Updating
update anna.device set form_factor_id=null, device_level_id=1, connectivity_type_id=1, locale_id=211, os_id=21, app_version_id=1, family_account_id=68, device_role_id=3, device_type_id=14, platform_id=2, language_id=26, unit_system_id=1, timezone_id=7, screen_resolution_id=6, country_id=193, name='Martin\'s iPhone', active=1, running=0, time_created='2015-01-12 17:33:35', time_deactivated=null, device_uuid='4D420BD6-478F-47D0-A652-E746A912AE6F', ip4_wan=_binary'Q\'�', ip4_lan=_binary'\n\0�', ip6_wan=null, ip6_lan=null, has_camera=1, on_battery=null, battery_level=null, light_level=null, temp
Trx read view will not see trx with id >= 0 15578219, sees < 0 15577724
------- TRX HAS BEEN WAITING 2 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 32770 n bits 224 index `PRIMARY` of table `anna`.`device` trx id 0 15578218 lock_mode X locks rec but not gap waiting
Record lock, heap no 156 PHYSICAL RECORD: n_fields 41; compact format; info bits 0
 0: len 4; hex 000001cb; asc     ;; 1: len 6; hex 000000edb09b; asc       ;; 2: len 7; hex 00000080191c23; asc       #;; 3: len 15; hex 4d617274696e2773206950686f6e65; asc Martin's iPhone;; 4: len 1; hex 81; asc  ;; 5: len 1; hex 80; asc  ;; 6: len 4; hex 54b3f75f; asc T  _;; 7: SQL NULL; 8: len 30; hex 34443432304244362d343738462d343744302d413635322d453734364139; asc 4D420BD6-478F-47D0-A652-E746A9;...(truncated); 9: len 4; hex 511f27b9; asc Q ' ;; 10: len 4; hex 0a0200b1; asc     ;; 11: SQL NULL; 12: SQL NULL; 13: len 4; hex 00000044; asc    D;; 14: len 1; hex 02; asc  ;; 15: len 2; hex 0001; asc   ;; 16: len 1; hex 01; asc  ;; 17: len 2; hex 0015; asc   ;; 18: SQL NULL; 19: len 2; hex 000e; asc   ;; 20: len 4; hex 00000003; asc     ;; 21: len 2; hex 001a; asc   ;; 22: len 1; hex 07; asc  ;; 23: len 2; hex 00c1; asc   ;; 24: len 2; hex 00d3; asc   ;; 25: len 1; hex 01; asc  ;; 26: len 2; hex 0006; asc   ;; 27: len 1; hex 81; asc  ;; 28: SQL NULL; 29: SQL NULL; 30: len 2; hex 0001; asc   ;; 31: SQL NULL; 32: SQL NULL; 33: SQL NULL; 34: SQL NULL; 35: SQL NULL; 36: SQL NULL; 37: len 1; hex 80; asc  ;; 38: len 4; hex 54b52644; asc T &D;; 39: len 4; hex 0000e306; asc     ;; 40: len 1; hex 81; asc  ;;

------------------
---TRANSACTION 0 15578064, ACTIVE 566 sec, process no 1599, OS thread id 140351558203136
2 lock struct(s), heap size 368, 1 row lock(s), undo log entries 46
MySQL thread id 1091, query id 15391 localhost 127.0.0.1 root
Trx read view will not see trx with id >= 0 15578065, sees < 0 15577724
---TRANSACTION 0 15577880, ACTIVE 1057 sec, process no 1599, OS thread id 140351558469376 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 11 lock struct(s), heap size 3024, 7 row lock(s), undo log entries 6
MySQL thread id 927, query id 15347 localhost 127.0.0.1 root Updating
update anna.server set server_type_id=2, region_id=2, timezone_id=1, country_id=56, name='rdev', cpu_load=4, ip4=_binary'M]�', ip6=null, relay_countries='SK,CZ', cpu_score=862, tcp_port=0, rest_port=8443, udp_port=10002, config=null, tokens_active=0, memory=1822208, memory_free=464904, stack_trace_enabled=0, tokens_free=null, tokens_limit=500 where server_id=9
Trx read view will not see trx with id >= 0 15577881, sees < 0 15577724
------- TRX HAS BEEN WAITING 7 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 29430 n bits 168 index `PRIMARY` of table `anna`.`server` trx id 0 15577880 lock_mode X locks rec but not gap waiting
Record lock, heap no 101 PHYSICAL RECORD: n_fields 23; compact format; info bits 0
 0: len 2; hex 0009; asc   ;; 1: len 6; hex 000000edb317; asc       ;; 2: len 7; hex 00000080192bd3; asc      + ;; 3: len 1; hex 82; asc  ;; 4: len 1; hex 81; asc  ;; 5: len 4; hex 72646576; asc rdev;; 6: len 4; hex 80000000; asc     ;; 7: len 4; hex 4d5dc81f; asc M]  ;; 8: SQL NULL; 9: len 1; hex 82; asc  ;; 10: len 5; hex 534b2c435a; asc SK,CZ;; 11: len 4; hex 8000035e; asc    ^;; 12: len 8; hex 8000000000000000; asc         ;; 13: len 8; hex 80000000000020fb; asc         ;; 14: len 8; hex 8000000000002712; asc       ' ;; 15: SQL NULL; 16: len 4; hex 80000000; asc     ;; 17: len 8; hex 80000000001bce00; asc         ;; 18: len 8; hex 800000000002783e; asc       x>;; 19: len 2; hex 0038; asc  8;; 20: len 1; hex 80; asc  ;; 21: SQL NULL; 22: len 4; hex 800001f4; asc     ;;

------------------
---TRANSACTION 0 15577879, ACTIVE 1058 sec, process no 1599, OS thread id 140351480346368
40 lock struct(s), heap size 6752, 19 row lock(s), undo log entries 26
MySQL thread id 926, query id 15217 localhost 127.0.0.1 root
Trx read view will not see trx with id >= 0 15577880, sees < 0 15577724
---TRANSACTION 0 15577750, ACTIVE 1374 sec, process no 1599, OS thread id 140351482476288 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 15 lock struct(s), heap size 3024, 9 row lock(s), undo log entries 6
MySQL thread id 915, query id 15345 localhost 127.0.0.1 root Updating
update anna.server set server_type_id=2, region_id=2, timezone_id=1, country_id=56, name='rdev', cpu_load=2, ip4=_binary'M]�', ip6=null, relay_countries='SK,CZ', cpu_score=862, tcp_port=0, rest_port=8443, udp_port=10002, config=null, tokens_active=0, memory=1822208, memory_free=601266, stack_trace_enabled=0, tokens_free=null, tokens_limit=500 where server_id=9
Trx read view will not see trx with id >= 0 15577751, sees < 0 15577415
------- TRX HAS BEEN WAITING 17 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 29430 n bits 168 index `PRIMARY` of table `anna`.`server` trx id 0 15577750 lock_mode X locks rec but not gap waiting
Record lock, heap no 101 PHYSICAL RECORD: n_fields 23; compact format; info bits 0
 0: len 2; hex 0009; asc   ;; 1: len 6; hex 000000edb317; asc       ;; 2: len 7; hex 00000080192bd3; asc      + ;; 3: len 1; hex 82; asc  ;; 4: len 1; hex 81; asc  ;; 5: len 4; hex 72646576; asc rdev;; 6: len 4; hex 80000000; asc     ;; 7: len 4; hex 4d5dc81f; asc M]  ;; 8: SQL NULL; 9: len 1; hex 82; asc  ;; 10: len 5; hex 534b2c435a; asc SK,CZ;; 11: len 4; hex 8000035e; asc    ^;; 12: len 8; hex 8000000000000000; asc         ;; 13: len 8; hex 80000000000020fb; asc         ;; 14: len 8; hex 8000000000002712; asc       ' ;; 15: SQL NULL; 16: len 4; hex 80000000; asc     ;; 17: len 8; hex 80000000001bce00; asc         ;; 18: len 8; hex 800000000002783e; asc       x>;; 19: len 2; hex 0038; asc  8;; 20: len 1; hex 80; asc  ;; 21: SQL NULL; 22: len 4; hex 800001f4; asc     ;;

------------------
---TRANSACTION 0 15577748, ACTIVE 1383 sec, process no 1599, OS thread id 140351479015168 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 35 lock struct(s), heap size 6752, 21 row lock(s), undo log entries 24
MySQL thread id 913, query id 15320 localhost 127.0.0.1 root Updating
update anna.server set server_type_id=2, region_id=2, timezone_id=1, country_id=56, name='rdev', cpu_load=2, ip4=_binary'M]�', ip6=null, relay_countries='SK,CZ', cpu_score=862, tcp_port=0, rest_port=8443, udp_port=10002, config=null, tokens_active=0, memory=1822208, memory_free=164184, stack_trace_enabled=0, tokens_free=null, tokens_limit=500 where server_id=9
Trx read view will not see trx with id >= 0 15577749, sees < 0 15577415
------- TRX HAS BEEN WAITING 27 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 29430 n bits 168 index `PRIMARY` of table `anna`.`server` trx id 0 15577748 lock_mode X locks rec but not gap waiting
Record lock, heap no 101 PHYSICAL RECORD: n_fields 23; compact format; info bits 0
 0: len 2; hex 0009; asc   ;; 1: len 6; hex 000000edb317; asc       ;; 2: len 7; hex 00000080192bd3; asc      + ;; 3: len 1; hex 82; asc  ;; 4: len 1; hex 81; asc  ;; 5: len 4; hex 72646576; asc rdev;; 6: len 4; hex 80000000; asc     ;; 7: len 4; hex 4d5dc81f; asc M]  ;; 8: SQL NULL; 9: len 1; hex 82; asc  ;; 10: len 5; hex 534b2c435a; asc SK,CZ;; 11: len 4; hex 8000035e; asc    ^;; 12: len 8; hex 8000000000000000; asc         ;; 13: len 8; hex 80000000000020fb; asc         ;; 14: len 8; hex 8000000000002712; asc       ' ;; 15: SQL NULL; 16: len 4; hex 80000000; asc     ;; 17: len 8; hex 80000000001bce00; asc         ;; 18: len 8; hex 800000000002783e; asc       x>;; 19: len 2; hex 0038; asc  8;; 20: len 1; hex 80; asc  ;; 21: SQL NULL; 22: len 4; hex 800001f4; asc     ;;

------------------
---TRANSACTION 0 15577746, ACTIVE 1437 sec, process no 1599, OS thread id 140351482210048
10 lock struct(s), heap size 3024, 13 row lock(s), undo log entries 16
MySQL thread id 911, query id 15219 localhost 127.0.0.1 root
Trx read view will not see trx with id >= 0 15577747, sees < 0 15577415
---TRANSACTION 0 15577744, ACTIVE 1547 sec, process no 1599, OS thread id 140349945378560 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 15 lock struct(s), heap size 6752, 5 row lock(s), undo log entries 2
MySQL thread id 909, query id 15226 localhost 127.0.0.1 root Updating
update anna.server set server_type_id=2, region_id=2, timezone_id=1, country_id=56, name='rdev', cpu_load=1, ip4=_binary'M]�', ip6=null, relay_countries='SK,CZ', cpu_score=862, tcp_port=0, rest_port=8443, udp_port=10002, config=null, tokens_active=0, memory=1822208, memory_free=481920, stack_trace_enabled=0, tokens_free=null, tokens_limit=500 where server_id=9
Trx read view will not see trx with id >= 0 15577745, sees < 0 15577415
------- TRX HAS BEEN WAITING 37 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 29430 n bits 168 index `PRIMARY` of table `anna`.`server` trx id 0 15577744 lock_mode X locks rec but not gap waiting
Record lock, heap no 101 PHYSICAL RECORD: n_fields 23; compact format; info bits 0
 0: len 2; hex 0009; asc   ;; 1: len 6; hex 000000edb317; asc       ;; 2: len 7; hex 00000080192bd3; asc      + ;; 3: len 1; hex 82; asc  ;; 4: len 1; hex 81; asc  ;; 5: len 4; hex 72646576; asc rdev;; 6: len 4; hex 80000000; asc     ;; 7: len 4; hex 4d5dc81f; asc M]  ;; 8: SQL NULL; 9: len 1; hex 82; asc  ;; 10: len 5; hex 534b2c435a; asc SK,CZ;; 11: len 4; hex 8000035e; asc    ^;; 12: len 8; hex 8000000000000000; asc         ;; 13: len 8; hex 80000000000020fb; asc         ;; 14: len 8; hex 8000000000002712; asc       ' ;; 15: SQL NULL; 16: len 4; hex 80000000; asc     ;; 17: len 8; hex 80000000001bce00; asc         ;; 18: len 8; hex 800000000002783e; asc       x>;; 19: len 2; hex 0038; asc  8;; 20: len 1; hex 80; asc  ;; 21: SQL NULL; 22: len 4; hex 800001f4; asc     ;;

------------------
---TRANSACTION 0 15577730, ACTIVE 1557 sec, process no 1599, OS thread id 140351483541248
10 lock struct(s), heap size 3024, 10 row lock(s), undo log entries 13
MySQL thread id 895, query id 15184 localhost 127.0.0.1 root
Trx read view will not see trx with id >= 0 15577731, sees < 0 15577415
---TRANSACTION 0 15577729, ACTIVE 1567 sec, process no 1599, OS thread id 140351559001856 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 11 lock struct(s), heap size 3024, 7 row lock(s), undo log entries 3
MySQL thread id 894, query id 15224 localhost 127.0.0.1 root Updating
update anna.server set server_type_id=2, region_id=2, timezone_id=1, country_id=56, name='rdev', cpu_load=1, ip4=_binary'M]�', ip6=null, relay_countries='SK,CZ', cpu_score=862, tcp_port=0, rest_port=8443, udp_port=10002, config=null, tokens_active=0, memory=1822208, memory_free=492131, stack_trace_enabled=0, tokens_free=null, tokens_limit=500 where server_id=9
Trx read view will not see trx with id >= 0 15577730, sees < 0 15577415
------- TRX HAS BEEN WAITING 47 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 29430 n bits 168 index `PRIMARY` of table `anna`.`server` trx id 0 15577729 lock_mode X locks rec but not gap waiting
Record lock, heap no 101 PHYSICAL RECORD: n_fields 23; compact format; info bits 0
 0: len 2; hex 0009; asc   ;; 1: len 6; hex 000000edb317; asc       ;; 2: len 7; hex 00000080192bd3; asc      + ;; 3: len 1; hex 82; asc  ;; 4: len 1; hex 81; asc  ;; 5: len 4; hex 72646576; asc rdev;; 6: len 4; hex 80000000; asc     ;; 7: len 4; hex 4d5dc81f; asc M]  ;; 8: SQL NULL; 9: len 1; hex 82; asc  ;; 10: len 5; hex 534b2c435a; asc SK,CZ;; 11: len 4; hex 8000035e; asc    ^;; 12: len 8; hex 8000000000000000; asc         ;; 13: len 8; hex 80000000000020fb; asc         ;; 14: len 8; hex 8000000000002712; asc       ' ;; 15: SQL NULL; 16: len 4; hex 80000000; asc     ;; 17: len 8; hex 80000000001bce00; asc         ;; 18: len 8; hex 800000000002783e; asc       x>;; 19: len 2; hex 0038; asc  8;; 20: len 1; hex 80; asc  ;; 21: SQL NULL; 22: len 4; hex 800001f4; asc     ;;

------------------
---TRANSACTION 0 15577727, ACTIVE 1617 sec, process no 1599, OS thread id 140349944579840
10 lock struct(s), heap size 3024, 4 row lock(s), undo log entries 2
MySQL thread id 892, query id 15221 localhost 127.0.0.1 root
Trx read view will not see trx with id >= 0 15577728, sees < 0 15577415
---TRANSACTION 0 15577724, ACTIVE 1648 sec, process no 1599, OS thread id 140351483275008
14 lock struct(s), heap size 6752, 11 row lock(s), undo log entries 34
MySQL thread id 889, query id 15430 localhost 127.0.0.1 root
Trx read view will not see trx with id >= 0 15577725, sees < 0 15577415
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (write thread)
Pending normal aio reads: 0, aio writes: 0,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
786 OS file reads, 2712 OS file writes, 1137 OS fsyncs
0.07 reads/s, 16384 avg bytes/read, 0.05 writes/s, 0.05 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 9, seg size 11,
428 inserts, 428 merged recs, 69 merges
Hash table size 2212699, node heap has 9 buffer(s)
5.00 hash searches/s, 15.43 non-hash searches/s
---
LOG
---
Log sequence number 0 1679880718
Log flushed up to   0 1679878430
Last checkpoint at  0 1679871205
0 pending log writes, 0 pending chkp writes
758 log i/o's done, 0.05 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 1216262384; in additional pool allocated 1048576
Dictionary memory allocated 786512
Buffer pool size   65536
Free buffers       64458
Database pages     1069
Modified db pages  25
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages read 1062, created 7, written 2508
0.07 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
13 read views open inside InnoDB
Main thread process no. 1599, id 140349909063424, state: sleeping
Number of rows inserted 438, updated 282, deleted 0, read 335130
0.48 inserts/s, 0.50 updates/s, 0.00 deletes/s, 337.68 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

保存对象(导致那些更新)的方法只是下面的代码,剩下的就是 Hibernate 魔法

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
@Repository("dbHome")
public class DbHome {
    @Autowired(required=true)
    private SessionFactory sessionFactory;

    // Other CRUD methods are here

    public void saveObject(final Object object) {
        final Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(object);
    }
}

hibernate 配置:

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/anna?zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="hibernate.connection.username">xxxxx</property>
        <property name="hibernate.connection.password">xxxxx</property>
        <property name="hibernate.connection.schema">anna</property>
        <property name="hibernate.connection.autocommit">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.connection.CharSet">utf8</property>
        <property name="hibernate.connection.characterEncoding">utf8</property>
        <property name="hibernate.connection.useUnicode">true</property>
        <!-- Mappings -->
    </session-factory>
</hibernate-configuration>

最佳答案

好的,感谢@M.Deinum,我找到了解决方案。

首先,从 Hibernate 配置中删除/注释以下行。

<property name="hibernate.current_session_context_class">thread</property>

然后,将以下行添加到您的 Spring 上下文配置中。不要忘记添加适当的命名空间,更多信息/示例 here .

<tx:annotation-driven/>

还要确保附近有 transactionManager bean。

最后一步是在 @Repository 注释之后添加 @Transactional 注释(我读到它是 import to place it after)。

关于java - 简单更新已锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27960118/

相关文章:

Java加载带有变量的库

java - AsyncTask 完成后执行操作

java - 在构造函数中调用像 Swing 的 add() 这样的可覆盖方法

java - 如何用 Spring + Hibernate + MySQL 实现搜索过滤器?

javascript - 如何优化循环以插入数据库?

java - 如何为 Spring Boot 应用程序执行命令行任务(如 Rake 任务)?

java - Mysql 需要很长时间才能检索结果

php - 从存储在mysql中的路径显示图像

Java : Cannot send a form with Angular 5 that contain an input of type file to a REST backend (Spring Boot)

java - 我想将 MultipartFile 转换为所需类型 byte[] 但出现错误