RE: CayenneRunTimeException when it's sent several requests quickly

From: Gentry, Michael (michael_gentr..anniemae.com)
Date: Tue Mar 16 2004 - 12:00:28 EST

  • Next message: Jorge Sopena: "Re: Creating several equal objects in the same DataContext"

    Jorge,
     
    Is there any reason why you don't want a DataContext object for every
    session? I would think in most session-based applications you would
    want to create a new DataContext object (as a session-scoped variable)
    to use. I would limit a global DataContext object to read-only (or
    read-mostly) data like states or counties or product IDs, etc. (things
    more global in scope and rarely change).
     
    /dev/mrg
     

            -----Original Message-----
            From: Jorge Sopena [mailto:jsopen..idsa.es]
            Sent: Tuesday, March 16, 2004 11:16 AM
            To: cayenne-use..bjectstyle.org
            Subject: Re: CayenneRunTimeException when it's sent several
    requests quickly
            
            
            Thanks Mike,
            
            I've found out what was happened.
            As I said, I create a new DataContext for each request in my
    doGet() method, and I save it in a static variable.
            Then I use a Singleton pattern to recover it.
            
            The first problem is that this static variable is changed in
    each received request.
            And in some circunstancies that could have happened in the
    middle of the two instructions.
            I mean, when I create a new request object the DataContext is
    one, but when I do the search for the customer,
            it could have changed because of a new received request, and I
    do the search with this DataContext.
            So the DataContext for each object is different, and produces
    this error.
            
            I've worked it out, checking if the static variable is null and
    synchronizing the method which create it.
            
            Thanks
            
            Jorge
            
            PD. I've got another problem now, but I'll write a new email.
            
            Mike Kienenberger wrote:
            

                    Jorge Sopena <jsopen..idsa.es>
    <mailto:jsopen..idsa.es> wrote:
                      

                            Eric,
                            I just create a DatContext in my doGet() method
    of my servlet, in the
                            following way:
                                   ctxt =
    BasicServletConfiguration.getDefaultContext(session)
                            
                            Each time I receive a request, a new DataContext
    would be created,
                            wouldn't it?
                            
                            What my application has to do is to register
    these requests with the
                            customer who did it.
                            So I create a new Request object in that way:
                                req
    =(Request)ctxt.createAndRegisterNewObject("Request");
                            
                            And then I set the customer to the request:
                                 r.setCustomer(Customer.search(clientId));
                            
                            And it's in that moment when this error happens.
                            
                            But the weird thing is that happens only when I
    do several request
                            quickly, if they are separated in time
    everything goes right.
                                

                    
                    What you haven't described is what DataContext the
    result from
                    Customer.search(clientId) uses.
                    If it's not the same as ctxt, you will get an error.
                    
                    If you want to understand what's happening, try
    outputing the datacontext
                    for both "r.dataContext()" and
    "Customer.search(clientId).dataContext()."
                    You will probably notice a pattern that will explain the
    observed behavior.
                    
                    In any case, as I stated yesterday, if you use
                    
            
    r.setCustomer(r.dataContext().localObjects(Arrays.asList(new Object[] {
                    Customer.search(clientId) } )));
                    
                    the problem should go away.
                    
                    If it did not, that would indicate a bug in the cayenne
    framework.
                    
                    -Mike
                    
                      



    This archive was generated by hypermail 2.0.0 : Tue Mar 16 2004 - 12:00:35 EST