-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to avoid editing stale db table data (lamer needs help)
PostPosted: Sun Jul 06, 2008 9:05 pm 
Newbie

Joined: Sun Jul 06, 2008 8:43 pm
Posts: 1
Hi, all!

I am no Hibernate expert (and I also come from C background, so I may even have some lingo problem) so please forgive me if I am asking trivial things. However, here is the problem...

we have an application which consists of two parts - Gui client, where DB persistence is to be implemented using Hibernate; also a few background processes which are written in Pro*C and can acess the same DB schema and modify data in the tables. So when Gui user runs a query (via Hibernate) and the results are displayed in a form, and then if user wants to modify data, before doing this we need to check that the data is not stale, that is that the record wasn't changed by some other process in the meanwhile.

I got it to work using method below, but this is only because I don't know any better way of doing this (table is param and it has four fields code, data, desc, userupdateable ).

Please help, is there a good way of comparing old and new records without going through all fields in a table... and generally, what's the best way of dealing with the issue, if anyone knows it?


public boolean equals(Object o){
if (this == o) return true;
if (!(o instanceof Param)) return false;
Param temp = (Param)o;

if (!pcode.equals(temp.getCode())) return false;
if (!pdata.equals(temp.getData())) return false;
if (!pdesc.equals(temp.getDesc())) return false;
if (!userUpdatable.equals(temp.getUserUpdatable())) return false;
return true;
}


then in the code you have something like
Param isParam = (Param)session.get(Param.class, lockKey, LockMode.UPGRADE);
if (!isParam.equals(oldIsParam)) {
lockMsg = DB_ERROR_TAG + isParam.getPcode() +
" has been modified by other source,<br>" + MSG_END ;
return false;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 2:55 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi chartres,

you are writing code for a task, the Hibernate will do for you. Hibernate will compare the data from your GUI client with the latest db version - and it will present you the StaleObjectException, if it sees that the db contains are newer version. So why don't you just call this exception and present the user with it in an error message? You could THEN reload the newer version and - if you need to- do a compare or merge of user's data with db's data. All this depends on using the optimistic locking mechanism having a version member in your data.

BTW: it is always a problem having another application reading and writing to the same database; caching problems on each side may be manifest. Anyway: if you do this, you must rely on version changes on both sides.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.