views:

84

answers:

1

I'm porting a class from plain JDBC to use Spring. Part of this class consists of a block that find the most recent row, updates a field, and selects it into an object to be processed later. This block should only be executed by one thread on one machine at a time to make sure no two threads process the same row. We've been handling mutual exclusion by locking the table, but as part of the move to Spring would like to use the transaction management provided.

Is the DataSourceTransactionManager powerful enough to provide mutual exclusion in the case where our code is accessing the database from multiple machines?

+1  A: 

DataSourceTransactionManager makes use of the setAutoCommit method on java.sql.Connection. That in turn controls the transactional behaviour on the database server. The transaction itself resides in the database.

So yes, what you want to do shouldn't be a problem.

skaffman
Will I also need to manually execute LOCK TABLE or does that get done in DataSourceTransactionManager also?
scompt.com
`DataSourceTransactionManager` won't do that for you, no, that's an explicit command to the database. If you do it within the scope of the `DataSourceTransactionManager` transaction, though, the lock will obey the scope of that transaction.
skaffman
Thanks for that confirmation!
scompt.com