3 Replies Latest reply on Oct 23, 2003 11:42 AM by divine_comedy

    findByPrimaryKey creates too many instances?

    woeye

      Hi!
      Thanks to JProfiler we found out that calling someBMPBeanHome.findByPrimaryKey() creates new someBMPBeanImpl instances even though an instance for a given PK was created before and therefore should be in the entity cache. We thought that the container would reuse instances already created and loaded?
      Even more interessting is the fact that ejbLoad() isn't invoked on those new instances. So what is their purpose?

      Here's what we have tested:
      for x in 1 to 10:
      for i in 1 to 100: findByPrimaryKey(i)

      The first run (x=1) produced the following result i times:
      1. Constructor of mooEB was called
      2. ejbFindByPrimaryKey of mooEB for PK 1 was called
      3. Constructor of mooEB was called (again!)
      4. ejbLoad of mooOB for PK 1 was called
      and so on ...

      Why are two instances needed for one findByPrimaryKey call?

      The second and all following runs (x>1) produced the following
      result i times:

      1. Constructor of mooEB was called
      That's it. No ejbFindByPrimaryKey and no ejbLoad?

      The primary key class is java.lang.Integer.

      Currently we assume that JBoss needs a "home" instance (or call it factory instance) for executing the home methods, for example ejbFindByPrimaryKey. And this home instance isn't reused but a new home instance is created for every home interface call?

      Thanks in advance,
      Lars

        • 1. Re: findByPrimaryKey creates too many instances?
          divine_comedy

          Hm, seems to me like the n+1 issues associated with entity beans ( some CMP container can be smarter ). but here's what I think is supposed to happen when ejbFindByPrimaryKey is called when the data was never loaded before. The first call only "verifies" that the primary key is there, while in ejbLoad, you are supposed to load the real data associated with that primary key. If you see multiple instances being created, check your container configurations, it might be set to "instance per transaction" setting which will cause multiple instances of the same entity bean to be created. I think Home interface must be thread safe by design so you should not need to lookup a Home if you already got one.

          • 2. Re: findByPrimaryKey creates too many instances?
            msuroo

            Hi,
            As for me I need to have only one instance of Entity Bean per JVM(something like Singleton).
            What should I do to reach it?
            Should I make something with commit options?

            Thanks in advance,
            Max

            • 3. Re: findByPrimaryKey creates too many instances?
              divine_comedy

              As far as I know, Commit Option A by default would only create one instance which is logical. The JBoss folks mention that they are working to make this instance per transaction as well. Not sure if this is in the newer JBoss versions. I myself used 3.0.4.

              You can take a look at standard-jboss.xml and see the standard configurations there. If you need any custom configurations, you can specify it in the jboss.xml of your ejb config files and specify that your ejb uses that config.