The recommended practice for correct locking with exceptions in Java is the following:
lock();
try {
doStuff();
} finally {
unlock();
}
This makes sure that the lock is released no matter what exception is thrown (or not), without the need to catch or re-throw it. However, this might still not be the safest thing to do in your case, since unlock()
is a remote call and can itself throw exceptions.
The standard for coordination between services and databases is WS-AT. Any Java application server should support it, bridge with JTA, and with 2-phase commit on the database itself. When configuring the database connection in the application server, make sure you select the XADataSource option.