死锁分析(二)

 

死锁日志:

*** (1) TRANSACTION:
TRANSACTION 367592, ACTIVE 2 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 4 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 70124, OS thread handle 140131815679744, query id 1676129 113.132.9.30 saas-titanteam updating
update order_item set bar_code='0868585627', child_order_id=3449, count=2, item_id=239, item_uid='7daaa612458fddd22160cc019ecd7a96f23131e0', procurement_item_id=null, real_count=1, real_price=1000, real_weight=10.0, rejected_weight=0.0, sale_price=1000, item_specification_id=394 where id=18650
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2845 page no 474 n bits 176 index PRIMARY of table `saturn_titan_test`.`order_item` trx id 367592 lock_mode X locks rec but not gap waiting
Record lock, heap no 27 PHYSICAL RECORD: n_fields 26; compact format; info bits 0

*** (2) TRANSACTION:
TRANSACTION 367593, ACTIVE 2 sec starting index read
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 70123, OS thread handle 140132596733696, query id 1676130 113.132.9.30 saas-titanteam updating
update order_item set bar_code='0868585627', child_order_id=3449, count=2, item_id=239, item_uid='7daaa612458fddd22160cc019ecd7a96f23131e0', procurement_item_id=null, real_count=1, real_price=1000, real_weight=10.0, rejected_weight=0.0, sale_price=1000, item_specification_id=394 where id=18650
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 2845 page no 474 n bits 176 index PRIMARY of table `saturn_titan_test`.`order_item` trx id 367593 lock mode S locks rec but not gap
Record lock, heap no 27 PHYSICAL RECORD: n_fields 26; compact format; info bits 0

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2845 page no 474 n bits 176 index PRIMARY of table `saturn_titan_test`.`order_item` trx id 367593 lock_mode X locks rec but not gap waiting
Record lock, heap no 27 PHYSICAL RECORD: n_fields 26; compact format; info bits 0

 

以下为死锁代码, 数据库隔离级别RC

两个事务互相持有对方的共享锁, 相互阻塞对方的互斥锁

@Test
    public void contextLoads() {
        new Thread(new InnerThread()).start();
        new Thread(new InnerThread()).start();

        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    class InnerThread implements  Runnable{

        @Override
        public void run() {
            TenantContext.set("demo");
            purchaseOrderService.test();
        }
    }
public void test(){
        TenantContext.set("demo");
        OrderItemDto orderItemDtos = orderItemDao.getByIdWithLock(18650L);
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        orderItemDtos.setCount(2);
    }
public OrderItemDto getByIdWithLock(Long id){
        String hql = "select o from OrderItemDto o where id = :id";
        return entityManager.createQuery(hql, OrderItemDto.class).setParameter("id",id).setLockMode(LockModeType.PESSIMISTIC_READ).getSingleResult();
    }

 

posted @ 2018-08-20 17:09  車輪の唄  阅读(15)  评论(0)    收藏  举报  来源