This is well of topic - please use [OT]in your subject in the future. This is a classic multi user application problem. The problem is commonly known as 'lost update' & an often used solution is optimistic locking.
In my experience using a 'version' column on each table is easier to manage than timestamps. Just increment the version number when you do an update & check that the current version on the record is the same as the retrieved version prior to updating. Hibernate has this functionality build in - see http://www.hibernate.org/hib_docs/reference/en/html/transactions.html#transa ctions-optimistic Paul > -----Original Message----- > From: Lee Harrington [mailto:[EMAIL PROTECTED] > Sent: 22 February 2005 15:56 > To: Struts Users Mailing List > Subject: Hibernate: when 2 users simultaneous edite > > > Perhaps this is a bit off topic, but I know there are struts users > like me that use Hibernate: > > What happens when two people edit a record at the same time? Unless > something is done, the last person to save wins....the first edits are > lost. > > I'm seeking input on the following code I wrote to have some > protection from this. All of my records have a "last_mod_date" field. > When someone goes to a page, the keep track of the original > last_mod_date value. When they save...the last_mod_date in the > database is checked to see if it's still the same. If it's not, > someone has changed the record...and the user is informed...and there > are various resolutions that can be coded from that point. > > Please read and comment on the following code and let me know if there > is a better way to handle this situation. > > > /** > * safePersist(Metric) updates or saves specfied > <code>Metric</code> through Hibernate. > * Checks to see that value of <code>last_mod_date</code> has not > changed, and returns > * error if it has. > * > * @param Metric A <code>Metric</code> to be updated > * @param oldLastModDate the original lastModDate before > any edits were done > * > */ > public String safePersist(Metric metric, java.util.Date > oldLastModDate) throws InfrastructureException > { > Metric oldMetric; // initialize oldMetric > > // > // Begin transaction so that two people can't save at > the same time > // > Session session = HibernateUtil.getSession(); > HibernateUtil.beginTransaction(); > > // > // Retrieve current record from database so that the > last_mod_date > // can be compared > // > try { > oldMetric = (Metric) session.load(Metric.class, > metric.getMetricId()); > } catch (HibernateException ex) { > throw new InfrastructureException(ex); > } > > // > // Compare last_mod_dates...return exception if they are > different...as that > // means that someone else has changed the record in > the men time > // > if ( oldMetric.getLastModDate() != oldLastModDate) { > throw new InfrastructureException("Record was changed!"); > } > > // > // LastModDates are the same...so go ahead and save record > // > try { > HibernateUtil.getSession().saveOrUpdate(metric); > HibernateUtil.getSession().flush(); > } catch (HibernateException ex) { > throw new InfrastructureException(ex); > } catch (Exception e) { > throw new InfrastructureException(e); > } > > return metric.getMetricId().toString(); > } > > > Thanks in advance, > > Lee > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > Axios Email Confidentiality Footer Privileged/Confidential Information may be contained in this message. If you are not the addressee indicated in this message (or responsible for delivery of the message to such person), you may not copy or deliver this message to anyone. In such case, you should destroy this message, and notify us immediately. If you or your employer does not consent to Internet email messages of this kind, please advise us immediately. Opinions, conclusions and other information expressed in this message are not given or endorsed by my Company or employer unless otherwise indicated by an authorised representative independent of this message. WARNING: While Axios Systems Ltd takes steps to prevent computer viruses from being transmitted via electronic mail attachments we cannot guarantee that attachments do not contain computer virus code. You are therefore strongly advised to undertake anti virus checks prior to accessing the attachment to this electronic mail. Axios Systems Ltd grants no warranties regarding performance use or quality of any attachment and undertakes no liability for loss or damage howsoever caused. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]