0

I'm currently deal with web applications calling stateless Session Beans in order to interact with a database. My question is about how such application can call the same session bean in a way that the latter can choose which entity manager must use to access the database.

At present time I managed this situation launching different Jboss instances, listening on different ports, and in each instance I've deployed the same Session Bean pool and one or more web application have to deal with the same database. In other words, every Jboss instance contains a certain number of web application, a persistence unit (persistence.xml in META-INF jar containing the @Entity's) and the pool of session bean all inside an ear. It's self evident where the main weakness reside on: as more different database I need to deal with, as much Jboss instance I must mantain, and it's not sustainable mainly, but not only, for resources reason.

My final question is: what's the best practice to deploy different web application calling session beans that differ only for the EntityManager injected in them?

Thanks in advance

Stfn

vinothp
  • 9,939
  • 19
  • 61
  • 103

2 Answers2

1

Do it with a CDI @Producer method. I'm assuming you know how to discriminate between the multitude of @PersistenceContexts that you have, so you make a producer, something like this:

// first inject all the entity managers that you have into your CDI producer
@PersistenceContext(name = "name0")
private EntitManager em0;

@PersistenceContext(name = "name1")
private EntitManager em1;

@PersistenceContext(name = "name2")
private EntitManager em2;

...

// then have your producer method with logic to chose the right EntityManager to be returned
@Produces @MyPersistenceContext
public EntityManager obtainTheRightEM(InjectionPoint caller) {
    if (hasCondition0(caller)) {
        return em0;
    } else if (hasCondition1(caller)) {
        return em1;
    } else if (hasCondition2(caller)) {
        return em2;
    } else {
        ...
    }
}

Then in your @EJBs you replace the @PersistenceContext with @Inject @MyPersistenceContext.

Also, creating the @MyPersistenceContext qualifier may or may not be necessary depending on your particular problem. If you have particular configuration values you need to pass down to the producer in order to facilitate the decision on which EntityManager to return from your producer, look into using some @Nonbinding fields in your custom qualifier.

You may end up getting away with having only one @EJB deployment.

Cheers! Hope this helps.

rdcrng
  • 3,415
  • 2
  • 18
  • 36
0

What you want to achieve is not really clear...

Are the different DB are use to separate the data from different customer (each DB have the same structure)? For this there is some multy-tenant support in Hibernate (http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch16.html) but not yet been standardized in JPA.

Any way you can put the different ear on the same Jboss instance (you will need to use different web context name) to reduce the resource consumption. (But you will lost os level process control, one tenant can use all the resources without way to make sure the other get minimal resources.)

Kazaag
  • 2,115
  • 14
  • 14
  • Thank a lot Kazaag for your answer. Let me explain some more. – user1444322 Nov 02 '12 at 10:30
  • Thank a lot for your answer.Let me explain some more. Databases are different but with the same relations.I've already tried the second solution You outlined, but doesn't work.In the same container(this was my first try)I had Session Beans conflict (the first session bean called is used for following request regardless of the application called id, a sort of lack of isolation at session bean pool level) and the only way would be rename the session beans interfaces that is what I would avoid in order to maintain a lighter code. So, let me experiment the first outlined solution. Stfn – user1444322 Nov 02 '12 at 10:50
  • If the EJB are in different ear, they should have a different global name, so retrieving them via JNDI should give you the corresponding one. – Kazaag Nov 02 '12 at 16:40