How to handle concurrency errors when data is manipulated by multiple users.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on data access basics in the Programmer's Guide |
The example displays two views of the same data, simulating the screens of two users. You can make a change in one of the views and click the Update button for that view to save the change. When you try to make a change to the same field of data in the other view and click that view's Update button, you'll get an error message. This alerts you that someone has made a change since the data was last refreshed.
You can click a view's Refresh button at any time to retrieve the latest data and display it on the screen.
The following code illustrates how to determine if an error that occurs during updating is a data concurrency error. If it is a data concurrency error, an error message is shown to you. You can then choose whether to refresh the screen with the latest data.
NOTE The code for button btnUpdateUser1 which updates changes made by the simulated User 1 is shown below. The code for button btnUpdateUser2 is similar, except that "2" should be substituted wherever "1" is used, and "1" should be substituted wherever "2" is used.
// Clicked event of the btnUpdateUser1 that has the // caption "Update for User 1" // Send User 1's changes to the database try { vwUser1.updateRows(); } catch (AgoApiException e) { // An exception has occurred, so check to see if it // is a data concurrency exception. if (e instanceof AgoDataConcurrencyException) { // We have a data concurrency exception, which // means that the data User 1 is looking at has // been changed by User 2 since the view was // populated. So ask User 1 if the view // should be refreshed to show the current data if (agDialog.showMessageOKCancel( "Database Error for User 1", "Data Concurrency error has occurred. Would you like to refresh the view with data from the server?")) { // User 1 has selected OK, so now refresh // User 1's view of the data try { vwUser1.refreshRows(); } catch (AgoApiException ex) { agDialog.displayError(ex); } } } }
updateRows()
.
catch (AgoApiException e)
. The code then checks to see if the error is due to a data concurrency problem in the line: if (e instanceof AgoDataConcurrencyException)
.
showMessageOKCancel()
. The message dialog will ask you whether the data on the screen should be refreshed.
refreshRows()
retrieves the latest data, overwriting any edits made by this user.
The following code illustrates how to retrieve the latest data from the database and display it on the screen.
NOTE The code for button btnRefreshUser1 which refreshes the screen for the simulated User 1 is shown below. The code for button btnRefreshUser2 is similar, except that "2" should be substituted wherever "1" is used.
// Clicked event of the btnRefreshUser1 that has the // caption "Refresh for User 1" // Refresh User 1's view of the data try { vwUser1.refreshRows(); } catch (AgoApiException e) { agDialog.displayError(e); }
refreshRows()
to retrieve the latest data from the database and display it on the screen for that user.