This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Database concurrency issues

Sep 1st, 2004, 09:44 PM

Are there any guidelines for how database concurrency issues should be handled when developing a web application using Spring and iBatis?

The only data persistence framework I've used to date is a proprietary framework that requires each table to have a number of standard columns, including a column named "id" which contains a unique object id, and a numeric column named "update_id" which is set to 1 on insert, then incremented by one for each update. The following example illustrates the optimistic locking strategy:

1. Session A instantiates a Product object, which gets populated with values for product id 100, fetched as follows:

Product product = new Product(100);
product.fetch();

The fetch() method generates and executes the following SQL, and sets the corresponding attributes in the product object.

Notice the condition on update_id in the above where clause. This ensures that the row will only be updated if it hasn't been changed by another session since it was fetched. If zero rows are updated, the save() method throws an exception to indicate stale data.

Is anything resemling this kind of functionality provided with Spring or iBatis? If not, how is concurrency normally handled?

Comment

If you need this strategy, you could also just use Hibernate, which supports versioning out of the box. There is in fact nothing stopping you from using HIbernate for most O/R mapping, and then still using iBatis for the somewhat easier SQL based queries it provides, for example.