-
1. Re: EntityManager not thread-safe?
bravocharlie.seam.signup.benny.me.uk Jul 14, 2008 10:51 AM (in response to fbarth)What scope are your components? Post some code?
-
2. Re: EntityManager not thread-safe?
fbarth Jul 14, 2008 11:17 AM (in response to fbarth)The box-backing bean (I like this triple-b-term ;)) is page scoped.
The CollabfilterBean in this example-box is not used in the other boxes. The data in the other boxes is generated from standard queries, e.g.:Query query = entityManager.createQuery( "select new "+WatchListWrapper.class.getCanonicalName()+"(loc) " + " FROM User as u" + " join u.watchedLocations as loc" + " WHERE u=?1" + " ORDER BY loc.modified desc") query.setParameter(1, user)); results = query.getResultList();
This is the backing logic for the contentBox I'm talking about.
@Name("userRecBox") @Scope(ScopeType.PAGE) public class UserRecBox implements ContentBox<UserRecWrapper>{ @Logger Log log; @In User user; @In Identity identity; @In(create = true) private CollabFilterBean collabFilter; private List<UserRecWrapper> results = new ArrayList<UserRecWrapper>(); public List<UserRecWrapper> getResults() { return results; } public Long getResultCount() { return new Long(results.size()); } public Object evaluate() { if (identity.isLoggedIn()) { results = new ArrayList<UserRecWrapper>(); HashMap<Location, Double> recom = collabFilter.getRecommendationsForUser(user); for (Entry<Location, Double> entry : recom.entrySet()) { results.add(new UserRecWrapper(entry.getKey(), entry.getValue())); } } return false; } }
The collabfilterBean that generates the data for the box: (just the interesting parts)
Name("collabFilter") @Scope(ScopeType.APPLICATION) public class CollabFilterBean implements CollabFilter { @Logger Log log; @In(create = true) EntityManager entityManager; (...)
and finally the xhtml-box-code (again, just interesting parts)
<h:form> <a4j:jsFunction name="userRec_eval" action="#{userRecBox.evaluate}" reRender="userRecommendationResult" status="status_userRec"> </a4j:jsFunction> </h:form> <rich:jQuery selector="document" rendered="#{identity.loggedIn}" timing="onload" query="each(function () {userRec_eval();})"/> <a4j:outputPanel id="userRecommendationResult"> <rich:panel rendered="#{identity.loggedIn}" style="border:0px;"> <h:form> <rich:dataList var="recWrap" value="#{userRecBox.results}" rows="#{userRecBox.resultCount}"> <h:outputText value="#{recWrap.location.name} #{recWrap.location.kind}" /> <br /> <h:outputText value="("></h:outputText> <h:outputText value="#{recWrap.location.description}" /> <h:outputText value=")"></h:outputText> <br /> <h:outputText value="score: "></h:outputText> <h:outputText value="#{recWrap.rating} " /> </rich:dataList> </h:form> </rich:panel> <rich:panel rendered="#{not identity.loggedIn}"> Not logged in! </rich:panel> </a4j:outputPanel>
-
3. Re: EntityManager not thread-safe?
gjeudy Jul 15, 2008 3:33 PM (in response to fbarth)EntityManager is not thread-safe but Seam will serialize concurrent requests made to CONVERSATION scope or SESSION scoped beans so it should not matter.
In your case you inject an entityManager in an APPLICATION scoped bean, chances are that the entityManager is also in APPLICATION scope and that would explain partly the behavior.
Can you post your entityManager config in components.xml ?
-
4. Re: EntityManager not thread-safe?
pmuir Jul 15, 2008 5:40 PM (in response to fbarth)The scope of the component into which you inject a component doesn't affect the scope of the injected component.
-
5. Re: EntityManager not thread-safe?
gjeudy Jul 15, 2008 5:52 PM (in response to fbarth)True, but does it also apply when the injected component does not exist and is autocreated as in:
@In(create=true)
?
-
6. Re: EntityManager not thread-safe?
pmuir Jul 15, 2008 5:56 PM (in response to fbarth)No. The scope of the injected component is never altered by injection.