Option 2 seems preferable since it guarantees executing of SQL_UNLOCK
statement even if exception was thrown during executing of SQL_LOCK
.
LOCK TABLE and try-catch block
-
17-07-2023 - |
سؤال
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
المحلول
نصائح أخرى
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.
لا تنتمي إلى StackOverflow