updateObject - How do I get the numbers of rows that where changed

View: New views
4 Messages — Rating Filter:   Alert me  

updateObject - How do I get the numbers of rows that where changed

by AnaTatavu :: Rate this Message:

| View Threaded | Show Only this Message

In 90% of the cases , we need to do an update not an insert.
Using writeObject is causing to many db round-trips, since we are almost sure is an update , not an insert.
We still need to figure out , when the update didn't occurr, so we can do an insert.
At the end of the next code, I will need to know if all the updates succeeded(all the rows where found and they were updated).


      beginTransaction(cs);
                for ( Object row : rowList )
                {
                    if ( row != null )
                    {
                          Object query = cs.updateObject(row);
                    }
                }
                commitTransaction(cs);
In the eclipseLink doc is written:
The updateObject method updates existing objects in the database, but does not perform the does-exist check before it attempts the update operation. The updateObject is more efficient than the writeObject method if you are certain that the object does exist in the database. If the object does not exist, the database throws an exception when you execute the updateObject method.
(from http://wiki.eclipse.org/Using_Basic_Query_API_(ELUG))
I don't receive any exception in the case the object doesn't exists.

Thank you,
Ana

Re: updateObject - How do I get the numbers of rows that where changed

by James Sutherland :: Rate this Message:

| View Threaded | Show Only this Message

In general I would recommend using JPA or the UnitOfWork API, not writeObject/updateObject.  Especially with a ClientSession, as UnitOfWork is required with ClientSession, writeObject/updateObject should only be used with a DatabaseSession.

Using updateObject will just do the update, if the row does not exist it will just do nothing.  If you used optimistic locking in your object, the row count would be 0 and EclipseLink would throw an error.  Otherwise you could check the row-count yourself I suppose, but probably not a good idea to try updating something that does not exist.

AnaTatavu wrote:
In 90% of the cases , we need to do an update not an insert.
Using writeObject is causing to many db round-trips, since we are almost sure is an update , not an insert.
We still need to figure out , when the update didn't occurr, so we can do an insert.
At the end of the next code, I will need to know if all the updates succeeded(all the rows where found and they were updated).


      beginTransaction(cs);
                for ( Object row : rowList )
                {
                    if ( row != null )
                    {
                          Object query = cs.updateObject(row);
                    }
                }
                commitTransaction(cs);
In the eclipseLink doc is written:
The updateObject method updates existing objects in the database, but does not perform the does-exist check before it attempts the update operation. The updateObject is more efficient than the writeObject method if you are certain that the object does exist in the database. If the object does not exist, the database throws an exception when you execute the updateObject method.
(from http://wiki.eclipse.org/Using_Basic_Query_API_(ELUG))
I don't receive any exception in the case the object doesn't exists.

Thank you,
Ana

Re: updateObject - How do I get the numbers of rows that where changed

by AnaTatavu :: Rate this Message:

| View Threaded | Show Only this Message


I agree it is not recommended and not nice to blindly try to update, but in our case it will save a lot of lookups in the database.
Normally,we receive lots of updates from a different product and we need to update our database each time.The  cases when those updates are not a real update , but they are new objects are very rare.
and we cannot know in if they are or not a real update.
Using unitOfWork will still check the object exists in the database.

By "check the row-count yourself " do you mean just count the rows in the table with a separate Query or
once the transaction is commited I can get the information?


In general I would recommend using JPA or the UnitOfWork API, not writeObject/updateObject.  Especially with a ClientSession, as UnitOfWork is required with ClientSession, writeObject/updateObject should only be used with a DatabaseSession.

Using updateObject will just do the update, if the row does not exist it will just do nothing.  If you used optimistic locking in your object, the row count would be 0 and EclipseLink would throw an error.  Otherwise you could check the row-count yourself I suppose, but probably not a good idea to try updating something that does not exist.

AnaTatavu wrote:
In 90% of the cases , we need to do an update not an insert.
Using writeObject is causing to many db round-trips, since we are almost sure is an update , not an insert.
We still need to figure out , when the update didn't occurr, so we can do an insert.
At the end of the next code, I will need to know if all the updates succeeded(all the rows where found and they were updated).


      beginTransaction(cs);
                for ( Object row : rowList )
                {
                    if ( row != null )
                    {
                          Object query = cs.updateObject(row);
                    }
                }
                commitTransaction(cs);
In the eclipseLink doc is written:
The updateObject method updates existing objects in the database, but does not perform the does-exist check before it attempts the update operation. The updateObject is more efficient than the writeObject method if you are certain that the object does exist in the database. If the object does not exist, the database throws an exception when you execute the updateObject method.
(from http://wiki.eclipse.org/Using_Basic_Query_API_(ELUG))
I don't receive any exception in the case the object doesn't exists.

Thank you,
Ana


Re: updateObject - How do I get the numbers of rows that where changed

by James Sutherland :: Rate this Message:

| View Threaded | Show Only this Message

We retrieve the row-count for UpdateObjectQuery, but do not return it currently.  An UpdateAllQuery or DataModifyQuery returns the row-count.  UpdateObjectQuery will however raise the SessionEvent noRowsModified if the update did nothing, you should be able to register for this event.

You may also wish to log a bug that UpdateObjectQuery should return the row-count.




I agree it is not recommended and not nice to blindly try to update, but in our case it will save a lot of lookups in the database.
Normally,we receive lots of updates from a different product and we need to update our database each time.The  cases when those updates are not a real update , but they are new objects are very rare.
and we cannot know in if they are or not a real update.
Using unitOfWork will still check the object exists in the database.

By "check the row-count yourself " do you mean just count the rows in the table with a separate Query or
once the transaction is commited I can get the information?

James Sutherland wrote:
In general I would recommend using JPA or the UnitOfWork API, not writeObject/updateObject.  Especially with a ClientSession, as UnitOfWork is required with ClientSession, writeObject/updateObject should only be used with a DatabaseSession.

Using updateObject will just do the update, if the row does not exist it will just do nothing.  If you used optimistic locking in your object, the row count would be 0 and EclipseLink would throw an error.  Otherwise you could check the row-count yourself I suppose, but probably not a good idea to try updating something that does not exist.

AnaTatavu wrote:
In 90% of the cases , we need to do an update not an insert.
Using writeObject is causing to many db round-trips, since we are almost sure is an update , not an insert.
We still need to figure out , when the update didn't occurr, so we can do an insert.
At the end of the next code, I will need to know if all the updates succeeded(all the rows where found and they were updated).


      beginTransaction(cs);
                for ( Object row : rowList )
                {
                    if ( row != null )
                    {
                          Object query = cs.updateObject(row);
                    }
                }
                commitTransaction(cs);
In the eclipseLink doc is written:
The updateObject method updates existing objects in the database, but does not perform the does-exist check before it attempts the update operation. The updateObject is more efficient than the writeObject method if you are certain that the object does exist in the database. If the object does not exist, the database throws an exception when you execute the updateObject method.
(from http://wiki.eclipse.org/Using_Basic_Query_API_(ELUG))
I don't receive any exception in the case the object doesn't exists.

Thank you,
Ana