Vinnie
Vinnie

Reputation: 12730

Glassfish limited to two simultaneous threads with JPA/EJB/web service

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:

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

Upvotes: 1

Views: 1870

Answers (2)

Bozho
Bozho

Reputation: 597402

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)

Upvotes: 2

Pascal Thivent
Pascal Thivent

Reputation: 570615

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.

Upvotes: 4

Related Questions