db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Florian Fray" <florianf...@compuserve.de>
Subject Re: Torque BaseXXX in Transaction and the isNew() Method Check
Date Sat, 25 Sep 2004 09:06:08 GMT
I have had this problem some time ago, too.
In my opinion, there is no simple solution. So I wrote some code
to save those new-flags and manually rolled them back on an error.
A solutian I've worked on is to change the Transaction-class by adding
non-static methods for commit and rollback and two methods adding
Jobs for post-commit and post-rollback processing. Finally there should
be a save-method in the transaction, wich delegates to a save-method of
a Base-Object and which adds a new-Flag-false-Job to the commit-job-chain.
If commit is called, it should commit all changes and, if no exception is
the commit-job-chain should be processed.
I've made an implementation after reading your mail, cause my companies code
should not be contributed, as it contains too many specific changes. I'll
append the code
but it's not tested out now. It makes use of the fact that we are using
BaseObjects which contain the save-Method (I think this is optional).
Another way (my code doesn't exclude) is to add a save-Method which
uses a Transaction-instance (this would be nice as the java.sql.Connection
have to be used and won't appear in the app-code anymore).

In the code you sent in, there is another error.
The line "isAlreadyInSave = false" will never be executed if some
exception is thrown. This could be easily fixed with a try ... finally block
the template.

Florian Fray

----- Original Message ----- 
From: <Michael.Kashambuzi@bmwna.com>
To: <torque-user@db.apache.org>
Sent: Saturday, September 25, 2004 2:08 AM
Subject: Torque BaseXXX in Transaction and the isNew() Method Check

> Hi-
> 'm having a problem using Torque-generated classes within a transaction.
> Let's say I have a table, Person, for which Person.java and
> are generated by Torque.
> When I attempt to save my person instance for the first time, Torque will
> an insert and then set the new attribtue to false on my Person instance.
>     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())
>                 {
>                     PersonPeer.doInsert((Person) this, con);
>                     setNew(false);
>                 }
>                 else
>                 {
>                     PersonPeer.doUpdate((Person) this, con);
>                 }
>             }
>                       alreadyInSave = false;
>         }
>       }
> Now, if I'm running in a transaction and the transaction rolls back due to
> some application exception, once the user resolves the problem and
> to re-insert the Person instance, it will not be inserted.  Instead,
> isNew() now returns false, Torque will attempt to update the record.
> However, this record does not exist in the database since the initial
> transaction rolled back.
> Has anyone come up with an elegant solution for this problem?
> Thanks.
> Michael
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org

View raw message