Bug in DataObjectUtils???? Re: Using DataContext not getting value in database

From: Alex Gunawan Wibowo (netzone_tec..ahoo.com)
Date: Tue Mar 08 2005 - 06:15:24 EST

  • Next message: Cris Daniluk: "RE: Bug in DataObjectUtils???? Re: Using DataContext not getting value in database"

    It seems that this is a bug in Cayenne.

    Coz the following works:

    Code to retrieve SOLUTION object:

        DataContext ctxt =
    DataContext.getThreadDataContext();
    .
    .
    .

            Expression template =
    Expression.fromString("db:solution_id=$value1");
            Map params = new HashMap();
            params.put("value1", solutionId);
            Expression qual =
    template.expWithParameters(params);
            SelectQuery query = new
    SelectQuery(Solution.class, qual);
            List result = ctxt.performQuery(query);
            Solution aSolution = (Solution) result.get(0);
            
            if (aSolution==null){
                logger.error("The requested Solution
    cannot be found in database");
                errors.add(ActionErrors.GLOBAL_ERROR, new
    ActionMessage("error.admin.db.query",solutionId,"solution"));
                return null;
            }

    ----------------
    The code above will "see" the updated object, and not
    the old one..

    :( Do i have to change this in all my code.???
    Is there any better solution???

    Thanks a lot!

    Best regards,

    Alex Wibowo.

    --- Alex Gunawan Wibowo <netzone_tec..ahoo.com>
    wrote:
    > Hello,
    >
    > i'm experiencing a problem with DataContext's
    > getThreadDataContext() method.
    >
    > Say I have two tables..
    >
    > SOLUTIONS, with attributes: solution_id(PK),
    > solution_name, solution_type (FK to SOLUTION_TYPES)
    >
    > SOLUTION_TYPES, with attributes: solution_type(PK)
    >
    >
    > I'm using Struts, and in an Action, I call the
    > DataContext.getThreadDataContext() method to get the
    > DataContext object, perform an update on a record
    > SOLUTION_TYPES, say, changing TYPE1 to TYPE2.
    >
    > Now, I have a record in SOLUTIONS TABLE that's
    > referring to the SOLUTION_TYPES table, say a record
    > like:
    > 1, SolutionName1, TYPE1
    >
    > The 1st update is successful. Viewing all solution
    > types in the database showed that the value has been
    > updated.
    > Since I'm using MS SQL server, I'm able to view the
    > values in database directly. The values have been
    > updated correctly:
    > --------------------
    > SOLUTION_TYPES
    > * TYPE2
    >
    > SOLUTIONS
    > * 1, SolutionName1, TYPE2
    >
    > --------------------
    >
    > Now, I have another Action to edit the Solutions
    > record, and I wanted to display the current
    > information in the page. So, I called
    > DataContext.getThreadDataContext() in this Action,
    > and
    > use it to fetch Solution ID 1.
    > Strangely, this fetched object still have the
    > following information:
    > 1, SolutionName1, TYPE1
    > Although the value in database clearly have changed.
    > Ultimately, my application crashed.
    >
    > It seems that the old record is still in the memory,
    > and hence Cayenne did not refetch the object, and
    > not
    > knowing that solution with id 1 has been changed in
    > DB
    > to TYPE2. This is true, since if I restart the
    > application (restarting Tomcat), Cayenne fetch
    > Solution with ID 1 as 1, SolutionName1, TYPE2.
    >
    > My question: how do I make it so that Cayenne does
    > not
    > fetch the object from the memory??
    > Do i have to use:
    >
    > DataContext context =
    >
    BasicServletConfiguration.getDefaultContext(session);
    >
    > to solve this problem??? (Please don't say yes... it
    > would require significant changes in my
    > application.... :( )
    >
    >
    >
    > Thanks a lot for the help! Sorry for the lengthy
    > email....
    >
    >
    > Best regards,
    >
    >
    > Alex Wibowo
    >
    > By the way, the portion of code is here:
    >
    > ----------------------------------------------
    > Action that perform modification for SOLUTION_TYPES
    >
    > DataContext ctxt =
    > DataContext.getThreadDataContext();
    > .
    > .
    > .
    > Expression qual =
    > ExpressionFactory.matchExp("solutionType", "TYPE1");
    > SelectQuery query = new
    > SelectQuery(SolutionType.class, qual);
    > List result = ctxt.performQuery(query);
    > SolutionType aType = (SolutionType) result.get(0);
    > .
    > .
    > .
    > aType.setSolutionType("TYPE2");
    > .
    > .
    > .
    > ctxt.commitChanges();
    >
    >
    > -----------------------------------------------
    > Action that fetches SOLUTIONS
    >
    > DataContext ctxt =
    > DataContext.getThreadDataContext();
    > .
    > .
    > Solution aSolution = (Solution)
    > DataObjectUtils.objectForPK(ctxt, Solution.class,
    > new Integer("1"));
    > .
    > .
    > .
    > // This line below is where the error occurred
    > if
    >
    (aSolution.getToSolutionType().getSolutionType().compareTo("TYPE1")==0){
    > .
    > .
    > ;
    > }
    >
    >
    >
    > Cayenne reports this error:
    > org.objectstyle.cayenne.CayenneRuntimeException:
    > [v.1.1 December 6 2004] Error resolving fault, no
    > matching row exists in the database for ObjectId:
    > au.com.hybrid.comone.db.beans.SolutionType:
    > <solution_type: TYPE1>
    >
    org.objectstyle.cayenne.CayenneDataObject.resolveFault(CayenneDataObject.java:219)
    >
    >
    org.objectstyle.cayenne.CayenneDataObject.readProperty(CayenneDataObject.java:227)
    > .
    > .
    > .
    >
    >
    >
    >
    >
    >
    >
    > __________________________________
    > Celebrate Yahoo!'s 10th Birthday!
    > Yahoo! Netrospective: 100 Moments of the Web
    > http://birthday.yahoo.com/netrospective/
    >

    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com



    This archive was generated by hypermail 2.0.0 : Tue Mar 08 2005 - 06:15:25 EST