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