1

I'm exposing the following EJB3 stateless session bean through a web service.

@Stateless
public class UserRoleFacade implements UserRoleFacadeLocal {
    @PersistenceContext(unitName = "SimpleEA-ejbPU")
    private EntityManager em;

    public int count() {
        System.out.println("thisClass=" + this.getClass().getSimpleName() + "@" + this.hashCode() + ", em=" + em);
        try {
            Thread.sleep(10000); // 10 sec
        } catch (InterruptedException ex) {
            Logger.getLogger(UserFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

}

As you can see, it doesn't do much - in fact it doesn't do anything but sleep for 10 seconds after getting the request. I created this code as an experiment to learn more about how the EntityManager works in a multithreaded environment.

The web service looks like this:

@WebService(serviceName="UserRole")
public class UserRoleWebService {
    @EJB
    private UserRoleFacadeLocal ejbRef;// Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Web Service Operation")

    @WebMethod(operationName = "count")
    public int count() {
        return ejbRef.count();
    }

}

To do the test, I started 5 browsers, pointed to the web service tester, and fired them all. Here's the results that were printed out:

INFO: thisClass=UserRoleFacade@11511572, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@23961
INFO: thisClass=UserRoleFacade@32513964, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@1af5c5a
INFO: thisClass=UserRoleFacade@11511572, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@23961
INFO: thisClass=UserRoleFacade@32513964, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@1af5c5a
INFO: thisClass=UserRoleFacade@11511572, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@23961

To do the test, I've taken the advice of Bozho and set up JMeter with 15 threads. Here's the results that were printed out:

INFO: thisClass=UserRoleFacade@3869465, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@55a5d0, thread=Thread[http-thread-pool-17025-(2),10,Grizzly]
INFO: thisClass=UserRoleFacade@5558947, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@d0e940, thread=Thread[http-thread-pool-17025-(1),10,Grizzly]
INFO: thisClass=UserRoleFacade@20208512, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@635f47, thread=Thread[http-thread-pool-17025-(39),10,Grizzly]
INFO: thisClass=UserRoleFacade@23924919, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@16478c1, thread=Thread[http-thread-pool-17025-(41),10,Grizzly]
INFO: thisClass=UserRoleFacade@6172173, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@121691b, thread=Thread[http-thread-pool-17025-(40),10,Grizzly]
INFO: thisClass=UserRoleFacade@3869465, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@55a5d0, thread=Thread[http-thread-pool-17025-(2),10,Grizzly]
INFO: thisClass=UserRoleFacade@5558947, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@d0e940, thread=Thread[http-thread-pool-17025-(1),10,Grizzly]
INFO: thisClass=UserRoleFacade@23924919, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@16478c1, thread=Thread[http-thread-pool-17025-(39),10,Grizzly]
INFO: thisClass=UserRoleFacade@20208512, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@635f47, thread=Thread[http-thread-pool-17025-(40),10,Grizzly]
INFO: thisClass=UserRoleFacade@6172173, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@121691b, thread=Thread[http-thread-pool-17025-(41),10,Grizzly]
INFO: thisClass=UserRoleFacade@3869465, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@55a5d0, thread=Thread[http-thread-pool-17025-(2),10,Grizzly]
INFO: thisClass=UserRoleFacade@5558947, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@d0e940, thread=Thread[http-thread-pool-17025-(1),10,Grizzly]
INFO: thisClass=UserRoleFacade@23924919, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@16478c1, thread=Thread[http-thread-pool-17025-(39),10,Grizzly]
INFO: thisClass=UserRoleFacade@6172173, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@121691b, thread=Thread[http-thread-pool-17025-(41),10,Grizzly]
INFO: thisClass=UserRoleFacade@20208512, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@635f47, thread=Thread[http-thread-pool-17025-(40),10,Grizzly]

As you can see (and what I see visually from the browser) is that only 2 only 5 threads ever run simultaneously. Both the instances of the EJB and the EntityManager look like they might be limited somewhere. Where is that?

On the Glassfish application server it looks like my settings are:

  • connection pool: 8 initial connection, 32 max
  • ejb container: 0 initial, 32 max

I'm guessing those are OK. Is there a setting somewhere for the EnitiyManager? What else should I be looking at?

0

2 Answers 2

4

Check configuration>thread-pools>thread-pool#min-thread-pool-size in the admin console (should be 2). But that's just a min, certainly not a max.

2
  • Thanks! After I got some proper testing setup with @Bohzo's suggestion and noticed a 5 thread cap, I looked into this setting and saw that it was capped at 5 (min was indeed 2). After increasing the thread count, it's working as expected.
    – Vinnie
    Feb 19, 2010 at 19:13
  • 1
    this answer saved my behind. i was doing some long polling, and i'd find after 2 request everything else would hang. there are still two things i don't understand. the MIN is 2, but the default max is 5. why was i blocked at 2? shouldn't it have grown the pool? second, WHY is the default 2? that seems unreasonably small. anyway. Mar 2, 2011 at 15:12
2

I doubt there is such limitation. Perhaps the job was finished quickly, combined with connection delays, and hence the entity managers were reused.

In order to make a better benchmark, use JMeter - fire 15 requests, each in X seconds, and share the output. (this article gives a start for using JMeter. But it's very easy anyway)

2
  • Thanks! I took your advice and posted the output as an update to the question. Now it looks like I'm limited at 5 threads...
    – Vinnie
    Feb 19, 2010 at 18:31
  • Your initial assumption was indeed correct. I took a look at the setting @Pascal suggested and saw that was the setting capping the number of my connections. I wouldn't have gotten that far without your JMeter suggestion however. Thanks so much!
    – Vinnie
    Feb 19, 2010 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.