Discussions

EJB design: Weblogic Server 6.0 Cluster - Static Object Inside a EJB

  1. Hi,

    We have written one Stateless Session Bean. It has two methods. Method A and B.

    Method: A
    Inside this method, I call a method on the helper class, which creates a static Hastable object. I store some value in this static object.

    Method: B
    Inside this method also, I call a method on the same helper class I used for Method: A and reading the value from the static Hashtable object which got stored by Method A.

    This is working fine for a single application server. If we go for Server Clustering, will it create any problem? How the value got changed in static object on one cluster member server will be replicated in the other member server? Whether it is possible or not?

    Please give your suggestions. Thank you in advance.

    Thanks,
    Karthik.

  2. Yea, it'll create a problem. Defining something static means one per classloader so you'll run into problems even with just one JVM if you have multiple classloaders. For lookup type stuff it might not be a big deal to have one per instance in the cluster but if you need truly one instance of something you'll have to look for a different approach. Take a look at Weblogic's clustering and JNDI documentation. You can bind your object to JNDI, make it RMI, make it an entity bean, etc.

    --
    Tinou
    www.tinou.com
  3. Sounds pretty much like you are trying to make a stateless session bean stateful. Either use a statefule bean or have the data generated by methodA returned to the caller and passed back into methodB

    Stu
  4. Hi Tinou, Stuart

    Thank you for the information. I just write the exact requirement here.

    In our application we need to generate some auto-generated numbers which will be the primary key in the database tables. We can go for sequences in oracle database. But, our application is database independent. So, what we think is we can cache a number in the app server and for every insertion in the database we will increase this number by one and insert into the table with other details. For primary key we will always take from the cache which is available in the app server.

    The alternate solution could be having some table in the database and store the sequence value in that table. For every insertion, we can read the sequence from this table and increment this sequence value by one. But, many times we have to access the database in order to get the sequence. We fear of dead lock also with this solution since the transactions are very big. That is the reason we thought of the above solution.

    But in the caching solution, as you pointed out, it will create problem while clustering of app servers. Could you please give a solution by which we should avoid of going to database frequently and clustering of app server also should not create problem.

    Presently, stateless session bean method requires such auto generation of sequence number.

    Expecting your help. Thank you in advance.

    Thanks,
    Karthik.
  5. Hi Karthik

    Have a look at the pattern called " GUIDs without Singletons and Databases " in the patterns section of this site i think it should address your problem.

    Stu