[jboss-user] [EJB 3.0] - jms lazy mdb Design pattern

bdevis do-not-reply at jboss.com
Mon May 14 16:28:35 EDT 2007


Hi, to all
i have this scenario

1)I have my entity with many collection (Lazy association)

2)This is my mdb

  |         public class LoginRequest implements MessageListener {
  |     
  |     public LoginRequest() {
  |     }
  |     
  |       public void onMessage(Message message) {
  |         
  |     
  |         try {
  |                
  |                
  |                 Clienti _msg = (Clienti) ((ObjectMessage)message).getObject();
  |                 System.out.println("MessageBean onMessage" +_msg.getNome());
  |                 ClientiFacadeLocal facade=(ClientiFacadeLocal)CachingServiceLocator.getInstance().getLocalHome(ClientiFacade.class);
  |                 Clienti _cli=facade.findByName(_msg.getNome());
  |                 System.out.println("onMessage toString "+_cli.toString());
  |                 
  |                
  |                 if(_cli!=null)
  |                    Send(_cli);
  |                 
  |         } catch (Exception e) {
  |             System.err.println("Exception in SimpleMessageBean");
  |             e.printStackTrace();
  |         }
  |     } 
  |       
  |     
  |     public void Send(Clienti vo){
  |     try {
  |         System.out.println("-->>");        
  |         TopicConnectionFactory connectionFactory;
  |                 Topic queue;
  |                 TopicSession session;
  |                 
  |     
  |             Hashtable environment = new Hashtable();
  |             environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
  |             environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
  |             environment.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099"); // remote machine IP
  |             InitialContext ctx = new InitialContext(environment);
  |             Object obj = ctx.lookup("topic/VdeResponseLoginQueue"); //ejb-name
  |             System.out.println("-->> lookup object successfully");
  |             queue = (Topic) obj;
  |             connectionFactory=(TopicConnectionFactory) ctx.lookup("/TopicConnectionFactory");
  |             TopicConnection connection =  connectionFactory.createTopicConnection();
  |             session =
  |                     connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);  
  |             
  |             connection.start();
  |             ObjectMessage message = session.createObjectMessage();
  |             System.out.println("Ho ricevuto l'ordine da te ora "+vo.getNome());
  |             message.setObject(vo);
  |             message.setJMSCorrelationID(vo.getNome());
  |             System.out.println("Preparato message per "+vo.getCognome()+" "+vo.getNome());
  |             MessageProducer messageProducer = session.createProducer(queue);
  |     
  |             messageProducer.send(message);
  |             
  |             session.close();
  |             connection.close();
  |             connection=null;
  |            
  |     } catch (Exception e) {
  |             System.out.println("Errroe in sendMessage "+e.getMessage());
  |             e.printStackTrace();
  |         }
  |     }
  |     
  |     
  |       
  |     }
  | 
  | 
  | this is my Session Facade
  | 
  | @Stateless
  | //@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
  | public class ClientiFacade implements ClientiFacadeLocal,ClientiFacadeRemote {
  | 
  | @PersistenceContext()
  | 
  |     private EntityManager em;
  |       //private EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("NuovVdeUN");
  |       //EntityManager em = entityManagerFactory.createEntityManager();
  |    
  |     
  |     /** Creates a new instance of ClientiFacade */
  |     public ClientiFacade() {
  |     }
  | 
  |     public void create(Clienti clienti) {
  |         em.persist(clienti);
  |     }
  | 
  |     public void edit(Clienti clienti) {
  |         em.merge(clienti);
  |     }
  | 
  |     public void destroy(Clienti clienti) {
  |         em.merge(clienti);
  |         em.remove(clienti);
  |     }
  | 
  |     public Clienti find(Object pk) {
  |         return (Clienti) em.find(Clienti.class, pk);
  |     }
  | 
  |     public List findAll() {
  |        
  |         return em.createQuery("select object(o) from Clienti as o").getResultList();
  |     }
  | 
  |     public Clienti findByName(String nome) {
  |         
  |         Clienti id=(Clienti) em.createNamedQuery("Clienti.findByNome").setParameter("nome",nome).getSingleResult();
  |         return em.getReference(Clienti.class, id.getIdcliente());
  |     }
  |         
  |         
  | 
  |     
  |     
  | }
  | 
  | 

my client

  |  public class TestTopic {
  | 
  | 
  |     
  |     public TestTopic() {
  |        
  |     }
  |     
  |     
  |   
  |     public void Send(String cosa){
  |     try {
  |         System.out.println("-->>");        
  |         TopicConnectionFactory connectionFactory;
  |                 Topic queue;
  |                 TopicSession session;
  |                 
  |     
  |             Hashtable environment = new Hashtable();
  |             environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
  |             environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
  |             environment.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099"); // remote machine IP
  |             InitialContext ctx = new InitialContext(environment);
  |             Object obj = ctx.lookup("topic/VdeResponseLoginQueue"); //ejb-name
  |             System.out.println("-->> lookup object successfully");
  |             queue = (Topic) obj;
  |             connectionFactory=(TopicConnectionFactory) ctx.lookup("/TopicConnectionFactory");
  |             TopicConnection connection =  connectionFactory.createTopicConnection();
  |             session =
  |                     connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);  
  |             
  |             connection.start();
  |             ObjectMessage message = session.createObjectMessage();
  |             ClientiFacadeRemote facade=(ClientiFacadeRemote)CachingServiceLocator.getInstance().getRemoteHome(ClientiFacade.class);
  |             Clienti vo=facade.findByName(cosa);
  |             
  |             
  |             
  |             System.out.println("Ho ricevuto l'ordine da te ora "+vo.getNome());
  |             System.out.println("Tarifa  "+vo.getIdtariffa().getIdtariffa());
  |             System.out.println("Lingue  "+vo.getLingua().getStringa());
  |             
  |             System.out.println("DVD Collection 9  "+vo.getLingua().getDvdCollection9().size());
  |             
  |             
  |             message.setJMSCorrelationID(vo.getNome());
  |             message.setObject(vo);
  |             System.out.println("Preparato message per "+message.getJMSCorrelationID()+" "+vo.getCognome());
  |             MessageProducer messageProducer = session.createProducer(queue);
  |     
  |             messageProducer.send(message);
  |             
  |             session.close();
  |             connection.close();
  |             connection=null;
  |            
  |     } catch (Exception e) {
  |             System.out.println("Errroe in sendMessage "+e.getMessage());
  |             e.printStackTrace();
  |         }
  |     }
  |     
  |     public static void main(String args[]){
  |         TestTopic t = new TestTopic();
  |         t.Send("devis");
  |     }
  |     
  |     
  | }
  | 
  | 
  | 
  | 

When my client call 
vo.getLingua().getDvdCollection9().size()

throw this error
Errroe in sendMessage failed to lazily initialize a collection of role: vde.com.ejb.vo.Lingue.dvdCollection9, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: vde.com.ejb.vo.Lingue.dvdCollection9, no session or session was closed
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
        at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
        at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)



I have read about "Open Session in View" and AOP solution ... but i'dont use web app i have some Flex/Apollo (Adobe) application.
I have used also, PersistenceContext Extended, Transaction NOT_SUPPORTED etc.
Can you help me how i can design this scenario? i'm disperate... how i can use my Lazy Collection....?
Thanks in advance 
Devis


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

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



More information about the jboss-user mailing list