[jboss-user] [JBoss Seam] - Re: Getting LazyInitializationErrors with a SMPC

SmokingAPipe do-not-reply at jboss.com
Tue Dec 26 20:28:30 EST 2006


"norman.richards at jboss.com" wrote : Yes, it works.  It's just a new name. Are you using a seam managed persistence context?  

Yes, definitely.  Here's the scenario, which is just like basically every other web application on the planet: A user goes to a login-page, logs in, the "User" object is stored in the session, and then the user can access a bunch of protected pages that display and manipulate the "user" object.  Just what every web app anywhere does.

Here's what I've got:

<!DOCTYPE faces-config PUBLIC
  |   "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  |   "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
  | 
  | <faces-config>
  | 
  |     <application>
  |         <view-handler>org.jboss.seam.ui.facelet.SeamFaceletViewHandler</view-handler>
  |     </application>
  | 
  |     <!-- We use the Seam Managed Persistence Context (SMPC) to avoid -->
  |     <!-- the dreaded LazyInitializationExceptions  -->
  |     <lifecycle>
  |         <phase-listener>org.jboss.seam.jsf.TransactionalSeamPhaseListener</phase-listener>
  |     </lifecycle>
  | 
  | <!-- a couple of converters also, removed for brevity -->
  | 
  | </faces-config>

and the LoginAction bean, with some boilerplate deleted:


  | @Stateful
  | @Name("login")
  | public class LoginAction implements Login {
  | 
  |     @In(required=false) @Out(required=false)
  |     private Customer customer;
  | 
  |     private String name = null;
  | 
  |     private String password = null;
  | 
  |     @In(create=true)
  |     private transient FacesMessages facesMessages;
  | 
  |     /** Injected not created */
  |     @In(create=true) EntityManager smpc;
  | 
  |     public String gatewayLogin() {
  |         if(smpc == null) {
  |             logger.severe("No entity manager was found, so this will not work.");
  |             return "/internal-error.jsp";
  |         }
  | 
  |         if(password == null) return "internal-login";
  | 
  |         if(name == null) return "internal-login";
  | 
  |         password = password.trim().toLowerCase();
  | 
  |         final List<Customer> results = smpc.createQuery("from Customer where " +
  |                 "name = :name and password = :password")
  |                 .setParameter("name", name)
  |                 .setParameter("password", getPassword())
  |                 .getResultList();
  |         if (results.isEmpty()) {
  |             logger.info("no such customer was found");
  |             facesMessages.add("invalidLogin",
  |                     new FacesMessage("The username or password was invalid"));
  |             customer = null;
  |         } else {
  |             customer = results.get(0);
  |             logger.info("About to log in this customer: " + customer);
  |             return "/customer/index";
  |         }
  |     }
  | 

And persistence.xml:
<persistence>
  |     <persistence-unit name="foo">
  |         <jta-data-source>java:/FooDS</jta-data-source>
  | 
  |         <properties>
  |             <property name="hibernate.hbm2ddl.auto"
  |                       value="update"/>
  | 
  |             <property name="jboss.entity.manager.factory.jndi.name"
  |                       value="java:/EntityManagerFactories/smpcData"/>
  | 
  |         </properties>
  |     </persistence-unit>
  | </persistence>
  | 

And components.xml:


  | <components>
  | 
  |     <component name="org.jboss.seam.core.init">
  |         <property name="debug">true</property>
  |         <property name="jndiPattern">Foo/#{ejbName}/local</property>
  |         <property name="myFacesLifecycleBug">false</property>
  |     </component>
  | 
  |     <!-- 120 second conversation timeout -->
  |     <component name="org.jboss.seam.core.manager">
  |         <property name="conversationTimeout">120000</property>
  |     </component>
  | 
  |     <!-- We can use Seam-managed persistence context to avoid -->
  |     <!-- dreaded LazyInitializationExceptions -->
  |     <component name="smpc"
  |           class="org.jboss.seam.core.ManagedPersistenceContext">
  |     <property name="persistenceUnitJndiName">java:/EntityManagerFactories/smpcData</property>
  | 
  |      </component>
  | 
  | </components>
  | 

And finally, the Customer class itself, trimmed:


  | @Entity
  | @Name("customer")
  | @Scope(ScopeType.SESSION)
  | @Roles({@Role(name="createCustomer",scope=ScopeType.CONVERSATION)})
  | public class Customer extends Person implements Serializable {
  | 


Anyway, after I do the login, the "customer" is properly set in the HTTP session, as you would expect, but when I go to access some collection from the customer object, I get a LIE.  I'm totally baffled by this, because I thought that SMPC was supposed to nearly eliminate LIEs.

Thanks for any help.


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3996359#3996359

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3996359



More information about the jboss-user mailing list