Solved

h:datatable row selection and multiple concurrent users

Posted on 2007-12-05
2
792 Views
Last Modified: 2008-02-01
Hi :)
I am developing a simple web app, and using an <h:datatable> to display some data from the database, and provide links to edit and remove the data in a particular row.

The problem I have is that if one user modifies the data (eg removes a row) other users who are already viewing the table get undesired results when interacting with it.

Example, two users both viewing the table which contains three rows of data. If user #1 removes row 1, and then user #2 tries to edit or remove row 2, user #2 actually gets the data for row 3 (because row 1 is gone and the other rows move up).

I've attached a code snippet from the backing bean. opcoDataModel is the data for the table, selectForEdit is the method attached to the link on the row of data to be edited.

It seems to me that the method I am using to select the row is flawed, since the form, and hence the DataModel, get rebuilt before the form is sumbitted.

Any help is greatly appreciated.
/**

 * Method to get all opcos from the database

 * @return DataModel for displaying in an h:dataTable

 */

public DataModel getOpcoDataModel() {

	if(opcoDataModel == null) {

		opcoDataModel = new ListDataModel();

	}

	Criteria criteria = new Criteria();

	Collection<Opco> opcos = DBMethods.getCollection(criteria, Opco.class);

	opcoDataModel.setWrappedData(opcos);

	

	return opcoDataModel;

}
 

/**

 * Method to populate the variables from the selected Opco

 * @return "edit_opco", or "nothing found" if opco was null

 */

public String selectForEdit() {

	Opco opco = (Opco)opcoDataModel.getRowData();

	if(opco != null) {

		

		opcoId = opco.getOpcoId();

		companyName = opco.getCompanyName();

		location = opco.getLocation();

		contactEmail = opco.getContactEmail();

		contactTel = opco.getContactTel();

		webLink = opco.getWebLink();

		

		return "edit_opco";

	} else {

		//should not be able to get here

		return "nothing_found";

	}

}

Open in new window

0
Comment
Question by:Memran
2 Comments
 
LVL 26

Accepted Solution

by:
mrcoffee365 earned 125 total points
ID: 20417460
The way this is generally handled is
1.  Each row has a unique id -- _not_ the display row number -- by which it can be retrieved, updated, or deleted from the db.
2.  If a user deletes a row that turns out to have been already deleted, then the page redisplays without that row.  The application can tell that the row has already been deleted by another user because the delete request to the db used the unique id for the record, not the row id.
3.  The page redisplays the table in its current state, which means that the deleted row is now deleted.  Most applications don't try to explain to the user that the row had already been deleted by another user, but you could do that if it is a good thing for your application.

Seeing data that might be out of date is the nature of allowing multiple people to update the same data in the database.  It is almost never a problem for an application -- most people can understand that the last person to update a record wins.  Or, in the case of delete, the first person wins (or they both win, since the goal was a delete).

If you have an application where you need to have perfect information displayed to the user, then you have to lock the table from updates by other people while user A is looking at/editing it.  As soon as there is some sort of signal from user A that they are done, then the table is free for edits by user B -- and the table must redisplay before user B begins his/her edit.
0
 

Author Comment

by:Memran
ID: 20440323
Thanks for your reply.
I was really hoping for a neat solution where I would pass the object, and not the ID (this is oop after all ;)), but since no such solution was forthcomming and time constraints are tight, I have gone with your reccommendation.
Have some points ;)
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now