-
1. Re: Question on Stateless Session Bean
hasc Mar 27, 2007 10:58 AM (in response to hasc)after a while i got this working.
@Stateless @Name("building") public class BuildingListAction implements BuildingList,Serializable{ private static final long serialVersionUID = 1L; private List<BuildingType> objects; Map<String,BuildingType> buildingsMap; @PersistenceContext EntityManager em; public Map<String,BuildingType> getBuildings() { objects = em.createQuery("select b from BuildingType b") .setHint("org.hibernate.cacheable", true) .getResultList(); Map<String,BuildingType> results = new TreeMap<String,BuildingType>(); for (BuildingType buildingtype: objects) { results.put(buildingtype.getName(),buildingtype); } buildingsMap = results; return buildingsMap; } public Converter getConverter() { return new BuildingTypeConverter(objects); } }
im not familiar with ejb but this obviously leads to a query every time the method getBuildings() is called. My goal is to load the objects only once and then return a list every time the methos is called. Can somone help me with that or point me to an example or tutorial which describes that?
regards,
hasc -
2. Re: Question on Stateless Session Bean
pmuir Mar 27, 2007 11:02 AM (in response to hasc)You'll either need to make the component stateful, or outject into a scope that holds state. Stateless components hold no state between invocations. That's what the snippet of code in your first post is doing.
-
3. Re: Question on Stateless Session Bean
hasc Mar 27, 2007 11:15 AM (in response to hasc)so is it impossible to create an object, thats is initialized once (in my example retrieves the required objects out of the database) and only one instance of it exists in the container?
I mean I have to display lists of objects on several pages and the objects in the database are almost never changed or new entities added. so my idea was to have one Stateless Bean that returns the list on each request.
Would a stateful bean with scope application be a solution?
Is there a way to declare that only one instance of an object should reside in an ejb container? -
4. Re: Question on Stateless Session Bean
supernovasoftware.com Mar 27, 2007 11:19 AM (in response to hasc)Is it possible to store the lists for s:selectItems in application scope?
Currently all my select items lists are in conversation scope. These lists are sometimes modified by users and I would like the changes to be globally visible.
If use Session or Conversation scope and outject the list upon update these changes will only be visible to that one user. Other users will have to start a new conversation or session to see the changes.
Maybe I should use PojoCache. If this is the case is there an example of this configuration? -
5. Re: Question on Stateless Session Bean
quilleashm Mar 27, 2007 11:26 AM (in response to hasc)Application scope should be fine for the above examples if you do the initial work in @Create and then just access it. Of course provided the data is the same for all users.
Be aware of threading issues in application scope tho as multiple threads may access it at the same time. If you do all your loading in @Create it should be fine.@Name( "cache" ) @Scope( ScopeType.APPLICATION ) @Intercept( InterceptionType.NEVER ) public class Cache { private Map cache; @Create public void create() { // build cache here... } public Map getCache() { return cache; } }
HTH.
Mike. -
6. Re: Question on Stateless Session Bean
hasc Mar 27, 2007 12:03 PM (in response to hasc)thanks a lot.
Is there a way to declare that only one instance of a class may exist in an ejb container and all requests are bound to this instance?
hasc -
7. Re: Question on Stateless Session Bean
pmuir Mar 27, 2007 12:18 PM (in response to hasc)As Mike says, put your Seam component in the application scope - then whenever you inject it (e.g. EL or @In) you'll get the same instance