17 Aug 06 0:51
I need to manage a certain BigDecimal class member in a web application which will not be destroyed. This class member represents a key in a database which from few reasons I would like to manage on my own and I don't use EJBs.
In order to implement that, I have created a private static class member at the application and a get method in order for the other views to get it's value.
The get method checks if the value is null.
In case it is it queries the Database for the maximum value and return it + 1 to the requestor.
In case it is not null it simply increments the current value by 1 and returns it to the requestor.
According to my assumption, a DB query should happen only once since this member is static, yet when I monitor the application I see it queries the Database occasionally, which mean that the member has been destroyed.
After an investigating this I noticed thatthe application is running in a clustered envirinment with 3 servers which means that 3 classes are loaded (one per JVM), every class maintains own MAX_ID and therefore I get into collisions.
Suppose it is not possible to migrate the whole application to use EJBs, is there a solution for this case? Perhaps a certain J2EE Design Pattern to manage the key in a clustered environment which can expose the key generation method to my application?