16 Aug 2007 21:04
triggers and update count
Alex B <alex4b <at> gmail.com>
2007-08-16 19:04:14 GMT
2007-08-16 19:04:14 GMT
Hi all,
in a recent project we are attempting to make use of triggers to increment snapshot ids (kept in separate tables), which will save CPU cycles when querying for recent changes to the database.
The reasons to use triggers over some implementation in the businesslogic ( e.g. SimpleORM's pre-flush hooks) are several, e.g.
a) one snapshot represents the state of several tables (i.e. several triggers work on the same snapshot)
b) manual data edits should still be safe and painless.
The issue we have come accross with this approach is that when SORM flushes a record to the database, the update count contains updates to tables updated by the trigger (SQL Server) and the optimistric lock check code (see below) in SRecordInstance.flush() will throw an SException.InternalError since it is expecting the update count to be 1:
Why is the check done for result != 1 rather than "result not in {1,2,3}"?
Simpleorm is built around primary keys and I am assuming that SRecordInstance.flush() could never update more that one row directly. If the update counter is indeed higher than 1, then only due to - intended - sideeffects. What uninteded effects could this code be trying to trap? Is it safe or would we miss nasty conditions if we were to write the optimistic check code as:
if ( result <= 0 || result >=3 ) {
// handle condition
Thanks for any responses in advance,
Alexander
__._,_.___
__,_._,___
in a recent project we are attempting to make use of triggers to increment snapshot ids (kept in separate tables), which will save CPU cycles when querying for recent changes to the database.
The reasons to use triggers over some implementation in the businesslogic ( e.g. SimpleORM's pre-flush hooks) are several, e.g.
a) one snapshot represents the state of several tables (i.e. several triggers work on the same snapshot)
b) manual data edits should still be safe and painless.
The issue we have come accross with this approach is that when SORM flushes a record to the database, the update count contains updates to tables updated by the trigger (SQL Server) and the optimistric lock check code (see below) in SRecordInstance.flush() will throw an SException.InternalError since it is expecting the update count to be 1:
/// Execute the QueryNow the question:
int result = 0;
try {
result = ps.executeUpdate();
} catch (Exception rsex) {
throw new SException.JDBC(
"Executing " + qry + " for " + this, rsex);
}
if ( result != 1) {
if (optimisticFieldValues != null && result == 0)
throw new BrokenOptimisticLockException(this);
else
throw new SException.InternalError(
"Rows Updated " + result + " != 1 " + this);
}
Why is the check done for result != 1 rather than "result not in {1,2,3}"?
Simpleorm is built around primary keys and I am assuming that SRecordInstance.flush() could never update more that one row directly. If the update counter is indeed higher than 1, then only due to - intended - sideeffects. What uninteded effects could this code be trying to trap? Is it safe or would we miss nasty conditions if we were to write the optimistic check code as:
if ( result <= 0 || result >=3 ) {
// handle condition
Thanks for any responses in advance,
Alexander
__._,_.___
SPONSORED LINKS
| Computer programming languages | Java programming language | Basic programming language |
| Programming languages |
Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe
__,_._,___
RSS Feed