Domanda

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

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top