How to Debug an InvalidVersionException from Updating an ATG Order

I thought I saw a puddy cat.... on Flickr

If you ever update an order outside of a transaction then the next time you update it within a transaction you will get the infamous, dreaded InvalidVersionException.

WARN  atg.commerce.order.ShoppingCartModifier
atg.commerce.order.InvalidVersionException: This order (o3830002) is out of date. Changes have been made to the order and the operation should be resubmitted. Order version 333, Repository item version 334.
   at atg.commerce.order.OrderManager.updateOrder(OrderManager.java:2557)

For example this could happen if you update your order in your JSP page.

<dsp:setvalue bean=”order.foo” value=”bar” />

To fix this problem you must always make sure to update an order within a transaction like this.

Transaction tr = null;
try {
  tr = ensureTransaction();
  synchronized (getOrder()) {
    getOrder().setFoo("bar");
    try {
      getOrderManager().updateOrder(order);
    }
    catch (Exception exc) {
      processException(exc, MSG_ERROR_UPDATE_ORDER, pRequest, pResponse);
    }
  }
}
finally {
  if (tr != null) commitTransaction(tr);
}

In some cases you might find a method is called within a transaction by another method and in other cases it is not.

public boolean handleFoo(DynamoHttpServletRequest req, DynamoHttpServletResponse res) {
  Transaction tr = null;
  try {
    tr = ensureTransaction();
    synchronized (getOrder()) {
      setFoo("bar");
      try {
        getOrderManager().updateOrder(order);
      }
      catch (Exception exc) {
        processException(exc, MSG_ERROR_UPDATE_ORDER, pRequest, pResponse);
      }
    }
    return checkFormRedirect(getSuccessUrl(), getErrorUrl(), req, res);
  }
  finally {
    if (tr != null) commitTransaction(tr);
  }
}

public void setFoo(String foo) {
  getOrder().setFoo(foo);
}

In the above example the handleFoo method properly updates the order within the transaction.  However calling the setFoo method directly will cause a problem since the order is not updated within a transaction.  To fix this you use the same pattern again to ensure the order is updated within a transaction.  It is okay to do this more than once during a request.

To debug this problem you can use Eclipse to make sure that wherever you update an order is always within a transaction. You can use the debugger to find which methods are called and/or you can use the call hierarchy to find out how methods are called.

Another way to help debug this is adding JSP code similar to the one I list below. It outputs the version of the order that the form handler has and the version that is in the repository.  If there is a difference then you know that the action you took before at some point updated the order outside of a transaction.

<dspel:getvalueof bean="ShoppingCartModifier.order.id" var="orderId" />
FORM HANDLER ORDER ID: ${orderId}<br/>
FORM HANDLER ORDER VERSION: <dsp:valueof bean="ShoppingCartModifier.order.version" /><br/>
<dsp:droplet name="/atg/dynamo/droplet/RQLQueryForEach">
  <dsp:param name="queryRQL" value="ID IN { \"${orderId}\" }"/>
  <dsp:param name="repository" value="/atg/commerce/order/OrderRepository"/>
  <dsp:param name="itemDescriptor" value="order"/>
  <dsp:oparam name="output">
    REPOSITORY ORDER VERSION: <dsp:valueof param="element.version"/><br/>
  </dsp:oparam>
</dsp:droplet>

For further reading please see Nabble – ATG Dynamo – Commerce Assist Returns and the Transaction Management section in the ATG Programming Guide.

Spaces, not Tabs

The holy war about spaces or tabs continues and you can see all sorts of arguments for each side raging around the internet.

I prefer spaces because it’s a consistent format that remains the same no matter what you are using.  With tabs things appear differently depending on how your editor or other program is configured.  Plus the worst thing is when spaces and tabs get intermixed.

Arguments for tabs such as they take up less room or it’s easier to move around are in my mind weak.  In this day of terabyte disk drives, file size differences between tabs and spaces are trivial and most of your disk space is taken up by images and binary files anyway.  And being able to move around and format quickly is not an issue either with modern editors.

The two editors I use most are XEmacs and Eclipse.  Below I describe how to use spaces for indents for each program.

In Emacs:

(setq-default indent-tabs-mode nil)  ; Use spaces for indents except
                                     ; in buffers that already have
                                     ; their own local values for the
                                     ; variable.

In Eclipse:

Eclipse Ganymede

I was about to write a post about how much I was starting to dislike Eclipse. Two years ago I was singing the praises of Eclipse but Eclipse Europa (version 3.3) in my opinion has been a disaster. It is incredibly slow, crashes often, runs out of memory, etc. I found myself at times using XEmacs instead because it was faster.

But today I installed Eclipse Ganymede (version 3.4) and so far life has been much better.  It is much more responsive, it is not crashing, things are running smoothly.   You can read about what is New and Noteworthy in Ganymede.  The most exciting new feature to me is the Retain case of match when replacing, just like XEmacs.

Also I noticed that in my JBoss project, hot swapping of code is again working.  I am not sure if this is because I upgraded to Ganymede or because I turned on “Build Automatically”.  I suspect it might be the latter.

Maven Integration for Eclipse

Previously I was using an old Maven integration for Eclipse, version 0.0.11, which I got from http://m2eclipse.codehaus.org/update.  The plugin was horribly slow and seemed to sometimes interfere with my builds.

Today I upgraded to the latest, version 0.9.4, from http://m2eclipse.sonatype.org/update/ and things are moving much more smoothly.  You can learn more at Maven Integration for Eclipse.

In Windows > Preferences > Maven I only turned on Download Artifact Sources and Download Artifiact JavaDoc.  Download repository index updates on startup is on by default but I turned it off after receiving this advice from a fellow developer, Bill Crook.

It’s unclear to me how the m2 plugin uses the indexes so I don’t think I can answer you. 🙂 Enabling it causes the plugin to rescan the entire repository if i remember correctly. I personally am not willing to have Eclipse do a full file scan of the local repository each time i start for unknown reasons. if you don’t mind the incurred overhead, go ahead and enable it!

The Maven plugin has a nice dependency feature which Bill Crook explains well.

There will come a time when you need to work on two projects simultaneously. Additionally, there will probably be a dependency from one project to another. The key to doing this effectively is understanding workspace resolution. Workspace resolution is a concept of the m2eclipse plugin. The way this works is that the plugin scans all projects in your workspace and analyzes the poms. Based on group and artifact ID’s the plugin will know if there are interdependencies between your projects. Once the plugin has successfully detected the dependency between projects, you can jump from one project source into another when jumping into methods (via control-click or F3).

There is one important caveat to this, versions. In addition to looking at group and artifact ID’s, the m2eclipse plugin will look at versions to determine if the project for a dependency is in the current workspace. Because of this you must make sure the versions match. Let’s take the example of projects A and B in your workspace. Assume A has a dependency on version 2.0.0.1 of B. Now, if the version of project B in your workspace is 3.0-SNAPSHOT, workspace resolution will NOT work. Can you figure out how to make this work? If you guessed, update the pom of project A to depend on 3.0-SNAPSHOT, you are correct. The moral of the story is be aware of versions when trying to get workspace resolution working.

You will know that workspace resolution is not functioning properly if you see duplicates of the same class when doing searches with control-shift-t. This behavior makes sense when you think about it as Eclipse sees two of these classes in your workspace. For example, continuing with projects A and B as above, let’s assume there is a class Foo in project B. Eclipse would find class Foo in project B source (as java source in your workspace) as well as in version 2.0.0.1 of B (as a class file in jar dependency).

Now if someone would improve the Perforce plugin…

Eclipse memory settings

Recently I have been having problems with Eclipse running out of memory.

Previously I had as my command line argument for Eclipse.

eclipse.exe -vmargs --Xmx512m

The double dashes were a problem, preventing Eclipse to properly load the correct memory settings from eclipse.ini.

I then removed the -vmargs –Xmx512m from the command line argument and instead modified eclipse.ini which is in the top level of the Eclipse installation. This blog article, Eclipse and memory settings, explains how you should put each argument on a separate line and that you can go to Help > About Eclipse Platform and then click on the Configuration Details button to check that Eclipse is running with the correct settings.

I changed this:

-Xms40m
-Xmx512m

to:

-Xms120m
-Xmx1024m

but I reverted back when I realized it was my original command line argument that was the problem.

I tried to use these settings as recommended in this forum, [news.eclipse.tools.jdt] Re: How to prevent out of memory errors?

-Xms120m
-Xmx1024m
-XX:PermSize=256M
-XX:MaxPermSize=512M

But for some reason that kept crashing the Eclipse startup.

Maven Assembly Convert Shell Scripts to use UNIX LF’s

In a previous post, Shell Scripts with Windows LF’s Fail in Latest Cygwin, I wrote about how to use Perforce to check out all files in UNIX format. However if you use Eclipse then this solution will not work because Eclipse always inserts Windows LF’s in any line you insert into a file leading to a mess with files that have both UNIX and Windows LF’s.

However if you happen to use Maven to generate / copy your shell scripts to their target directories you can take advantage of the lineEnding property in the Assembly Director. If you specify the lineEnding property to be “unix” then the outputted shell scripts will be in UNIX format. For example:

<file>
  <source>src/main/scripts/foo.sh</source>
  <outputDirectory>bin</outputDirectory>
  <lineEnding>unix</lineEnding>
</file>

Eclipse won’t start after Windows shutdown

My laptop unexpectedly shutdown when I had Eclipse open. After rebooting I could not start Eclipse. Instead it kept crashing with the message “An error has occurred. See the log file …” Looking at the log file I saw errors related to the Perforce plugin.

java.lang.NoClassDefFoundError
        at org.eclipse.ui.internal.themes.ThemeElementHelper.installFont(ThemeElementHelper.java:101)

org.osgi.framework.BundleException: Exception in org.eclipse.core.internal.compatibility.PluginActivator.start() of bundle com.perforce.team.ui.
        at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:1010)
Caused by: java.lang.ExceptionInInitializerError
        at com.perforce.team.ui.PerforceUIPlugin.initializeDefaultPreferences(PerforceUIPlugin.java:284)

I saw a post about this problem which referred to another post for a solution. Basically the post suggests that stale plugin cache information is causing the problem and suggests starting Eclipse with the -clean argument. After doing this I was successfully able to launch Eclipse.

Eclipse Serial Version UID Bug

Currently there is a bug with Eclipse and generating the serial version UID. When attempting to generate a serial version UID you will see a dialogue window which says “Computing serial version ID….” and “Starting virtual machine…”. Unfortunately this hangs and you are forced to kill your Eclipse IDE. I started seeing this bug after installing some JDBC plugins and the GEF plugin, I’m not sure if it’s related to that. I tried disabling the plugins but I still see the problem. Before I wasn’t having this problem.

I noticed that I haven’t encountered this problem on other installations of Eclipse, even w/ the JDBC plugins, so could just be that I have a bad build of Eclipse.

JDBC Plugins for Eclipse

I was previously using DbVisualizer to query and update my Oracle databases and was not completely satisfied with it. I actually prefer PhpMyAdmin but that only works with MySQL.

Since I use Eclipse so much for development I decided to look for a JDBC plugin that is at least as good as DbVisualizer. My goal is to have in one workspace access to my database as well as my source and configuration files.

I went to EclipsePlugins and downloaded the three highest rated JDBC plugins, all which happen to be free (in some cases just for non-commercial use).

  1. DBEdit
  2. QuantumDB
  3. SQLExplorer

I installed DBEdit 1.0.3_1, Quantum DB 3.0.1 (which requires GEF, I installed 3.1.1), SQLExplorer 2.2.4.

Setup was quite simple with DBEdit, just add the JDBC driver to the CLASSPATH, give it the JDBC URL and connect. Setup was almost as simple with Quantum DB though it asks you for the components of the JDBC URL instead of the URL directly. I couldn’t figure out how to setup SQLExplorer within five minutes so I gave up.

DBEdit and Quantum DB have similar interfaces and both are similar to DbVisualizer though Quantum DB’s appealed to me a little more. However Quantum DB’s interface responded much more quickly and querying of tables was much faster. DBEdit has a nice feature, which I did not completely test, of inline editing of cell values which seems quite powerful.

Because of Quantum DB’s speed and responsiveness I am going to continue to use that as my primary JDBC plugin but I will also keep DBEdit on the side to use for its inline editing but also in case I find Quantum DB does something in an unintuitive manner.

Update: Since I posted this less than a month ago I found DBEdit much more useful than Quantum DB. It’s inline editing, inserting feature, and other editing features really make it powerful. Also its filter, scrolling through result sets, etc. are great. I highly recommend DBEdit.