Resulting in a database deadlock timeout analysis rpc service

Background of

Service A and the service B at the same time on the same record in a table update operation performed

Flow is shown

The sample code

//服务A
@Transactional
public void serviceAmth{
	System.out.println("服务A事务方法开启");
	//此处更新db_test数据库中table_test表中id=1的记录
	update db_test.table_test set name='hello' where id=1;
	serviceB.serviceBmth()
	System.out.println("服务A事务方法结束");
}

//服务B
@Transactional
public void serviceBmth{
	System.out.println("服务B事务方法开启");
	//此处更新db_test数据库中table_test表中id=1的记录
	update db_test.table_test set name='hello' where id=1;
	System.out.println("服务B事务方法结束");
}

Process Analysis

A method of service when the open transaction, and updates the record id = 1, MySQL (default INNODB) database would lock up the rows (exclusive lock), but this time the service A service call before the transaction B in a transaction method, note that at this time in service a transaction has not been submitted, so the record id = 1 is still locked, after calling the service B, which also records modifications id = 1 but this time lock has been held by the transaction service a, we can only wait for the service a lock is released, the thread into the wait, and the service a releases the lock B calls and other services required to complete, so it is necessary to wait for the end of the service B, As a result, both waiting for each other, a database deadlock, a service call timeout.

Solution

We will update the same row records into a transaction carried out.

Guess you like

Origin blog.csdn.net/ly853602/article/details/84729398