I'm load testing the application and get deadlock error. The scenario is inserting and updating database concurrently by 10 different users. I looked up online and still couldn't find the way to solve it. Here attached my sample code involved in deadlock.
Can anyone give me some advice to solve deadlock? Thank you in advance.
SampleController:
onSubmit(userAccount)
{
sampleBO.testDeadLock(userAccount.getUserAccountId());
}
SampleBO:
public void testInsert(Long id)
{
sampleDAO.testInsert4(id);
}
public void testDeadLock(Long id)
{
testInsert(id);
sampleDAO.testUpdate4(id);
}
SampleDAO:
public void testInsert4(Long id)
{
StringBuffer sbSql = new StringBuffer();
sbSql.append(" INSERT INTO Test ");
sbSql.append(" ( ");
sbSql.append(" id, ");
sbSql.append(" note ");
sbSql.append(" ) ");
sbSql.append(" VALUES ");
sbSql.append(" (");
sbSql.append(""+id+",");
sbSql.append(" 'test' ");
sbSql.append(" )");
//Execute SQL using Spring's JDBC Templates
this.getSimpleJdbcTemplate().update(sbSql.toString());
}
public void testUpdate4(Long id)
{
StringBuffer sbSql = new StringBuffer();
sbSql.append(" UPDATE Test WITH(ROWLOCK) SET ");
sbSql.append(" note = 'test1111'");
sbSql.append(" WHERE id="+id);
//Execute SQL using Spring's JDBC Templates
this.getSimpleJdbcTemplate().update(sbSql.toString());
}