jdbc驱动版本mysql-connector-java-5.1.21.jar
mysql> show engine innodb status
    -> ;
| InnoDB |      |
=====================================
140616 12:01:10 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 17 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 469 1_second, 469 sleeps, 40 10_second, 90 background, 90 flush
srv_master_thread log flush and writes: 475
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 69, signal count 69
Mutex spin waits 69, rounds 665, OS waits 16
RW-shared spins 50, rounds 1500, OS waits 50
RW-excl spins 0, rounds 90, OS waits 3
Spin rounds per wait: 9.64 mutex, 30.00 RW-shared, 90.00 RW-excl
------------------------
LATEST DETECTED DEADLOCK
------------------------
140616 11:00:15
*** (1) TRANSACTION:
TRANSACTION 447C5, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 1248, 3 row lock(s), undo log entries 2
MySQL thread id 153, OS thread handle 0x17dc, query id 15476 localhost 127.0.0.1 root Updating
update prince_city_info
    set station_time = '2014-06-16 11:00:15',
      role_id = null,
      npc_id = 45,
      total_output = 0
    where city_id = 37
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 743 n bits 408 index `PRIMARY` of table `test_ll`.`prince_city_info` trx id 447C5 lock_mode X locks rec but not gap waiting
Record lock, heap no 334 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000025; asc    %;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141c48; asc       H;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000021; asc    !;;
 6: len 4; hex 80000000; asc     ;;*** (2) TRANSACTION:
TRANSACTION 447C4, ACTIVE 0 sec starting index read, thread declared inside InnoDB 500
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1248, 15 row lock(s), undo log entries 14
MySQL thread id 141, OS thread handle 0x1b08, query id 15486 localhost 127.0.0.1 root Updating
update prince_city_info
    set station_time = '2014-06-16 11:00:15',
      role_id = null,
      npc_id = 5,
      total_output = 0
    where city_id = 29
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 743 n bits 400 index `PRIMARY` of table `test_ll`.`prince_city_info` trx id 447C4 lock_mode X locks rec but not gap
Record lock, heap no 330 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000026; asc    &;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141bbe; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000009; asc     ;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 331 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 8000001b; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141bec; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 8000002f; asc    /;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 332 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 8000000b; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141c1a; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000025; asc    %;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 334 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000025; asc    %;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141c48; asc       H;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000021; asc    !;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 335 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000027; asc    ';;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141c76; asc       v;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 8000002a; asc    *;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 337 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000009; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141ca4; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 8000000c; asc     ;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 338 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000017; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141cd2; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000002; asc     ;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 339 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000014; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141d00; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 8000000b; asc     ;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 340 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000008; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141d2d; asc       -;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000001; asc     ;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 341 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000015; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141d5a; asc       Z;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000024; asc    $;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 342 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 8000000e; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141d88; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 8000000d; asc     ;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 343 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 80000007; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141db5; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 8000000f; asc     ;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 344 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 8000001f; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141de3; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 8000002c; asc    ,;;
 6: len 4; hex 80000000; asc     ;;Record lock, heap no 345 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 8000001c; asc     ;;
 1: len 6; hex 0000000447c4; asc     G ;;
 2: len 7; hex 1b000002141e11; asc        ;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000010; asc     ;;
 6: len 4; hex 80000000; asc     ;;*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 743 n bits 416 index `PRIMARY` of table `test_ll`.`prince_city_info` trx id 447C4 lock_mode X locks rec but not gap waiting
Record lock, heap no 333 PHYSICAL RECORD: n_fields 7; compact format; info bits 0
 0: len 4; hex 8000001d; asc     ;;
 1: len 6; hex 0000000447c5; asc     G ;;
 2: len 7; hex 1c000001fb152e; asc       .;;
 3: len 8; hex 800012515a44cfbf; asc    QZD  ;;
 4: SQL NULL;
 5: len 4; hex 80000025; asc    %;;
 6: len 4; hex 80000000; asc     ;;*** WE ROLL BACK TRANSACTION (1)

解决方案 »

  1.   

    接上面:------------
    TRANSACTIONS
    ------------
    Trx id counter 45464
    Purge done for trx's n:o < 45464 undo n:o < 0
    History list length 884
    LIST OF TRANSACTIONS FOR EACH SESSION:
    ---TRANSACTION 0, not started
    MySQL thread id 185, OS thread handle 0x1420, query id 20898 KUEN-PC020 192.168.2.69 root
    show engine innodb status
    ---TRANSACTION 45462, not started
    MySQL thread id 177, OS thread handle 0x1844, query id 20891 localhost 127.0.0.1 root
    ---TRANSACTION 0, not started
    MySQL thread id 173, OS thread handle 0x193c, query id 16430 localhost 127.0.0.1 root
    ---TRANSACTION 4545E, not started
    MySQL thread id 171, OS thread handle 0x1950, query id 20874 localhost 127.0.0.1 root
    ---TRANSACTION 0, not started
    MySQL thread id 175, OS thread handle 0x1c54, query id 16426 localhost 127.0.0.1 root
    ---TRANSACTION 0, not started
    MySQL thread id 174, OS thread handle 0x1c28, query id 16425 localhost 127.0.0.1 root
    ---TRANSACTION 0, not started
    MySQL thread id 168, OS thread handle 0x17dc, query id 16427 localhost 127.0.0.1 root
    ---TRANSACTION 0, not started
    MySQL thread id 169, OS thread handle 0x1700, query id 16424 localhost 127.0.0.1 root
    ---TRANSACTION 0, not started
    MySQL thread id 170, OS thread handle 0x768, query id 20702 localhost 127.0.0.1 root
    ---TRANSACTION 448FF, not started
    MySQL thread id 172, OS thread handle 0x1948, query id 16428 localhost 127.0.0.1 root
    ---TRANSACTION 0, not started
    MySQL thread id 165, OS thread handle 0x15ac, query id 16238 localhost 127.0.0.1 root
    --------
    FILE I/O
    --------
    I/O thread 0 state: wait Windows aio (insert buffer thread)
    I/O thread 1 state: wait Windows aio (log thread)
    I/O thread 2 state: wait Windows aio (read thread)
    I/O thread 3 state: wait Windows aio (read thread)
    I/O thread 4 state: wait Windows aio (read thread)
    I/O thread 5 state: wait Windows aio (read thread)
    I/O thread 6 state: wait Windows aio (write thread)
    I/O thread 7 state: wait Windows aio (write thread)
    I/O thread 8 state: wait Windows aio (write thread)
    I/O thread 9 state: wait Windows aio (write thread)
    Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
     ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
    Pending flushes (fsync) log: 0; buffer pool: 0
    1463 OS file reads, 1783 OS file writes, 480 OS fsyncs
    0.00 reads/s, 0 avg bytes/read, 0.35 writes/s, 0.24 fsyncs/s
    -------------------------------------
    INSERT BUFFER AND ADAPTIVE HASH INDEX
    -------------------------------------
    Ibuf: size 1, free list len 0, seg size 2, 1 merges
    merged operations:
     insert 13, delete  0, delete 0
    discarded operations:
     insert 0, delete  0, delete 0
    Hash table size 188953, node heap has 35 buffer(s)
    0.00 hash searches/s, 0.06 non-hash searches/s
    ---
    LOG
    ---
    Log sequence number 112383478
    Log flushed up to   112383478
    Last checkpoint at  112383478
    0 pending log writes, 0 pending chkp writes
    373 log i/o's done, 0.12 log i/o's/second
    ----------------------
    BUFFER POOL AND MEMORY
    ----------------------
    Total memory allocated 97665024; in additional pool allocated 0
    Dictionary memory allocated 694724
    Buffer pool size   5824
    Free buffers       4260
    Database pages     1529
    Old database pages 584
    Modified db pages  0
    Pending reads 0
    Pending writes: LRU 0, flush list 0, single page 0
    Pages made young 13, not young 0
    0.00 youngs/s, 0.00 non-youngs/s
    Pages read 1452, created 77, written 1345
    0.00 reads/s, 0.00 creates/s, 0.18 writes/s
    Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
    Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
    LRU len: 1529, unzip_LRU len: 0
    I/O sum[0]:cur[0], unzip sum[0]:cur[0]
    --------------
    ROW OPERATIONS
    --------------
    0 queries inside InnoDB, 0 queries in queue
    1 read views open inside InnoDB
    Main thread id 2204, state: waiting for server activity
    Number of rows inserted 14204, updated 233, deleted 0, read 108021
    0.00 inserts/s, 0.06 updates/s, 0.00 deletes/s, 0.06 reads/s
    ----------------------------
    END OF INNODB MONITOR OUTPUT
    ============================CREATE TABLE `prince_city_info` (
      `city_id` int(11) NOT NULL COMMENT '城池id,关联sprince_city表的id',
      `station_time` datetime DEFAULT NULL COMMENT '占领时间',
      `role_id` bigint(20) DEFAULT NULL COMMENT '角色id。null表示没有玩家占领此城',
      `npc_id` int(11) DEFAULT NULL COMMENT 'NPC的id。null表示没有npc占领此城',
      `total_output` int(11) DEFAULT NULL COMMENT '累积产出',
      PRIMARY KEY (`city_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='城池信息表'
      

  2.   

    这是java应用程序的错误日志:
    2014-06-16 11:00:15,333 ERROR [pool-1-thread-1] com.joyway.mobile.module.schedule.controller.ScheduleController.scheduleEveryHour(ScheduleController.java:252) - 
    org.springframework.dao.DeadlockLoserDataAccessException: 
    ### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
    ### The error may involve com.joyway.mobile.mybatisgen.repository.mapper.PrinceCityInfoVoMapper.updateByPrimaryKey-Inline
    ### The error occurred while setting parameters
    ### SQL: update prince_city_info     set station_time = ?,       role_id = ?,       npc_id = ?,       total_output = ?     where city_id = ?
    ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
    ; SQL []; Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:265)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:71)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
    at com.sun.proxy.$Proxy9.update(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:251)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
    at com.sun.proxy.$Proxy150.updateByPrimaryKey(Unknown Source)
    at com.joyway.mobile.module.prince.facade.impl.PrinceFacadeImpl.genNpcPeriodiccally(PrinceFacadeImpl.java:226)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy152.genNpcPeriodiccally(Unknown Source)
    at com.joyway.mobile.module.schedule.controller.ScheduleController.scheduleEveryHour(ScheduleController.java:249)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1065)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.execute(NewProxyPreparedStatement.java:989)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:41)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:66)
    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:45)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:108)
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:145)
    at sun.reflect.GeneratedMethodAccessor417.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:355)
    ... 32 more
      

  3.   

    源代码:
    for (...........条件省略) {
             PrinceCityInfoVo record = new PrinceCityInfoVo();
    record.setCityId(info.getCity().getId());
    record.setNpcId(npcs.get(j).getId());
    record.setRoleId(null);
    record.setStationTime(now);
    record.setTotalOutput(0);
    princeCityInfoDao.updateByPrimaryKey(record);
    }