Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top