db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Blake Nicholson <tor...@package.com>
Subject question about saving after exception thrown during initial save
Date Tue, 22 Apr 2003 21:41:29 GMT
I am using Torque 3.0.0, Oracle 8.1.6.

I have a class called BaseCustomList.java that was generated by Torque.  It 
contains the following method:

     public void save(Connection con) throws TorqueException
         if (!alreadyInSave)
             alreadyInSave = true;

             // If this object has been modified, then save it to the database.
             if (isModified())
                 if (isNew())
                     CustomListPeer.doInsert((CustomList) this, con);
                     CustomListPeer.doUpdate((CustomList) this, con);

             if (collCustomListItems != null)
                 for (int i = 0; i < collCustomListItems.size(); i++)
                     ((CustomListItem) collCustomListItems.get(i)).save(con);
             alreadyInSave = false;

I have a unique constraint on the name attribute of the custom list 
table.  If I do a save and the name is a duplicate, an exception will be 
thrown.  I catch this exception, verify that the unique constraint is what 
was wrong, and then display an error message to the user (giving them the 
opportunity to fix the error and try saving again).

Unfortunately, since the exception was thrown, the code that sets 
alreadyInSave back to false never gets executed.  Any subsequent attempts 
to save the object will appear to work, but will not actually be successful 
because if (!alreadyInSave) does not evaluate to true.

Is my only option (short of modifying the source of the framework) to run a 
query to verify that the name is not a duplicate prior to saving?  I am 
also considering modifying the Object.vm template to put the doInsert and 
doUpdate invocations in a try block that will set alreadyInSave back to 
false in a finally block.  I'm not sure what other ramifications this may 
have, though, so I am hesitant to do so.

Thanks for any ideas/suggestions,

View raw message