문제

Inside a method annotated with @Transactional, should a LOCK TABLE statement be executed inside the try block or before it?

Option 1:

jdbcTemplate.execute(SQL_LOCK);

try {
    //Some work
} finally {
    jdbcTemplate.execute(SQL_UNLOCK);
}

Option 2:

try {
    jdbcTemplate.execute(SQL_LOCK);

    //Some work
} finally {
    jdbcTemplate.execute(SQL_UNLOCK);
}

Thanks, Mickael

도움이 되었습니까?

해결책

Option 2 seems preferable since it guarantees executing of SQL_UNLOCK statement even if exception was thrown during executing of SQL_LOCK.

다른 팁

Choose option 2, it will do finally sql unlock whether exception is raised or not in try-catch block.

In case option 1, if the exception is raised before try-catch block in SQL LOCK, the program don't guarantee to enter finally block to unlock it back.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top