At first glance your code seems ok, The first place I would check is:
if(numlocs<MaxSaveLocs){
addLocationToUser(pm2, omruserkey, location);
} else {
msg = "You have exceeded the maximum number of
locations you
can save, please delete a location first.";
log.info("FAIL - Exceed max num locs");
}
Also as a side note, this is redundant:
pm.newQuery(query).execute(user);
you should simply do query.execute(...)
Hope this helps,
Yaniv Kessler
maybe some locations exceed their MaxSaveLocs ???
On Mon, Jun 21, 2010 at 8:27 AM, shaz <[email protected]> wrote:
>
> The problem is specifically with persisting objects in a many to one
> relationship. The odd thing is that the my code was working fine, and
> then suddenly it was only persisting the objects some of the time,
> completely non-deterministic and the code hadn't changed. This has
> made it really hard to debug (it only happens sometimes and there is
> no exception, it just doesn't persist)
>
> For context, the app is a route optimization solver, and there is a
> user class called OmrUser, and there are Location and Driver classes
> that map to an OmrUser (Location3 and Driver are the class names).
> (Each User saves locations and drivers, and solves optimizations to
> route them). Many locations roll up to one user, and right now
> locations save only some of the time and it is getting worse.
>
> The odd thing is the Driver class persists fine, and the code is the
> same.
>
>
>
> Snippet of OmrUser class (userlocs is the list collection where
> Location3 objects are saved in the many to one mapping):
>
> @PersistenceCapable(identityType = IdentityType.APPLICATION,
> detachable = "true")
> public class OmrUser {
> @PrimaryKey
> @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> private Key key;
>
> @Persistent
> private User gaeuser;
>
> @Persistent(mappedBy = "omruser")
> @Element(dependent = "true")
> @Order(extensions = @Extension(vendorName="datanucleus", key=
> "list-ordering", value="key asc"))
> private List<Location3> userlocs;
>
> @Persistent(mappedBy = "omruser")
> @Element(dependent = "true")
> @Order(extensions = @Extension(vendorName="datanucleus", key=
> "list-ordering", value="key asc"))
> private List<Driver> userdrivers;
>
> .....<rest cut>
>
>
> ******************************************************************************************************************************************************
>
> Snippet from Location3 class
>
> @PersistenceCapable(identityType = IdentityType.APPLICATION,
> detachable = "true")
> public class Location3 implements Comparable<Location3> {
>
> @PrimaryKey
> @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> private Key key;
>
> @Persistent
> private User author;
>
> @Persistent
> private OmrUser omruser;
>
> @Persistent
> private String addressname;
>
> .....<rest cut>
>
>
>
> *****************************************************************************************************************************************************************
>
> Servlet that saves a location to a User
>
>
> public class SaveLoc extends HttpServlet {
> private static final Logger log =
> Logger.getLogger(SaveLoc.class.getName());
>
> public void doGet(HttpServletRequest request, HttpServletResponse
> response)
> throws IOException {
> doPost(request, response);
> }
>
> public void doPost(HttpServletRequest req, HttpServletResponse
> resp)
> throws IOException {
> UserService userService = UserServiceFactory.getUserService();
> User user = userService.getCurrentUser();
> Integer MaxSaveLocs =
> Integer.parseInt(System.getProperty("opt.maxlocsave"));
>
> String address = req.getParameter("addr");
> String addressname = req.getParameter("addrname");
> Double addresslat =
> Double.parseDouble(req.getParameter("lat"));
> Double addresslon =
> Double.parseDouble(req.getParameter("lon"));
> Double servicetime =
> Double.parseDouble(req.getParameter("servicetime"));
> String msg = "";
> List<OmrUser> omruserquery;
> OmrUser thisomruser = null;
> Date date = new Date();
> Integer numlocs;
> Key omruserkey;
>
> PersistenceManager pm = PMF.get().getPersistenceManager();
> Location3 location = new Location3(user, address, addressname,
> date, addresslat, addresslon, servicetime);
> log.info("Attempting to Add Location - " +
> location.getAddressname());
>
> try {
> String select_query = "select from " +
> OmrUser.class.getName();
> Query query = pm.newQuery(select_query);
> query.setFilter("gaeuser == paramAuthor");
> query.declareParameters("java.lang.String paramAuthor");
> omruserquery = (List<OmrUser>)
> pm.newQuery(query).execute(user);
>
> if(omruserquery.iterator().hasNext()) {
> thisomruser = omruserquery.iterator().next();
> } else {
> thisomruser = CreateNewUser(user);
> pm.makePersistent(thisomruser);
> }
> } finally {
> numlocs = thisomruser.getNumlocs();
> omruserkey = thisomruser.getKey();
> pm.close();
> }
>
>
> PersistenceManager pm2 =
> PMF.get().getPersistenceManager();
> if(numlocs<MaxSaveLocs){
> addLocationToUser(pm2, omruserkey, location);
> } else {
> msg = "You have exceeded the maximum number of
> locations you
> can save, please delete a location first.";
> log.info("FAIL - Exceed max num locs");
> }
> pm2.close();
>
> if(msg == ""){
> resp.sendRedirect("locations.jsp");
> } else {
> resp.sendRedirect("/err.jsp?msg=" + msg);
> }
>
> }
>
> public void addLocationToUser(PersistenceManager pm2, Key UserKey,
> Location3 location) {
> pm2.currentTransaction().begin();
> try {
> OmrUser omruser = pm2.getObjectById(OmrUser.class,
> UserKey);
> omruser.getUserlocs().add(location);
> pm2.currentTransaction().commit();
> } finally {
> if (pm2.currentTransaction().isActive()) {
> pm2.currentTransaction().rollback();
> }
> }
> }
>
> public OmrUser CreateNewUser(User user) {
> OmrUser newuser = new OmrUser(user, "Basic");
> log.info("Created new user " + newuser.getGaeuser().getEmail());
> return newuser;
> }
>
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To post to this group, send email to
> [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-appengine-java%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.