I am not an expert in EJBs but will give this a try...
Approach 1:
How would I do it if I use plain
JDBC to do the above task?
1. Open transaction
2. Select data
3. If conditions meet, Update data
4. Close transaction
And would wrap this code in a synchronized method or block, but EJBs forbid the use of synchronization in the bean code!...So would follow Kyle's idea of the SELECT FOR UPDATE clause...
I would put the SQL code in a simple method of a Stateless Session
bean to accomplish the above task with BMP and CMT.
Approach 2 :
Since the number field and the counter member fields belong to the same entity bean, I would write a home business mehtod in the home interface which performs the Select and update actions together. I guess we still need to use Select For Update clause as locking is required...I know, it can be done using BMP but not sure how you do it in CMP, if your bean is already CMP2.0 :roll:
Whenever we use Select For Update clause, we make sure that we release the lock on the rows as soon as possible, after we are finished with updating. Otherwise this could turn to be a performance issue in the code later...