Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm developing a desktop application using Java. My application will run in a network environment where multiple users will access the same database through the application.

There will be basic CRUD opreations (Insert, Update, Delete, & select), which means there will be chances of deadlock, or two users trying to update the same record at same time.

I'm using the following

  • Java Swing for Clients (MVC).
  • MySQL Server for database (InnODB).
  • Java Web start.

Now, MySQL is centralized on the network, and all of the clients connect to it. The Application for ERP Purpose.

I searched the internet to find a very good solution to ensure data integrity & to make sure that when updating one record from one client, other clients are aware of it.

I read about Socket-server-client & RESTful web services.

I don't want to go web application & don't want to use any extra libraries.

So how can I handle this scenario:

If User A updates a record:

  1. Is there a way to update User B's screen with the new value?
  2. If user A starts updating a record, how can I prevent other users from attempting to update the same record?
share|improve this question

closed as unclear what you're asking by MichaelT, GlenH7, Michael Kohne, jmort253, Glenn Nelson Nov 7 '13 at 20:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
It's not clear what your question is. Could you please clarify what your are asking? –  FrustratedWithFormsDesigner Oct 31 '13 at 17:46
    
my application will run in network, if user A updated Record, 1. is there a way to update user B screen with the new value? 2. if user A start updating Record, then prevent user B from updating at same time. –  Motasem Abu Aker Oct 31 '13 at 18:19

1 Answer 1

is there a way to update user B screen with the new value?

Yes. This could be done in one of two ways:

  1. The client application polls the data source and displays the results. This is pretty simple to implement, but the downside is the client must request new data every n seconds and then determine if there is any difference and refresh the display if there is.

  2. The client subscribes to be notified by the server if something changes. Harder to implement - I think you'd have to build a server-side process to handle this.

if user A start updating Record, then prevent user B from updating at same time.

I will assume that it could take several minutes for User A to update the record by modifying data in a form. In this case, when a user chooses to edit a record, you need to mark it in the database as being edited. You could do this by explicitly locking the record in the database, though how to do this will depend on what database you're using.

You could also use soft locks. This would involve having a table to indicate which records are locked and when another client tries to edit a record, it must first check that there is no lock recorded:

customers
---------
  ID (PK)
  (other fields)

customer_locks
--------------
  ID (PK)
  customer_id (FK to customers.id)
  client_id (String to identify which client system has locked the record)
  lock_timestamp

So when a client system wants to edit a customer with some ID, it must first check that there is no existing record in customer_locks. Then it creates a record for itself (if there are no locks - if there are, the client must be notified). Then it (eventually) does an UPDATE... for the customer in question, then it must delete the lock record.

Of course, then you have questions about what happens if a client is disconnected after they create a lock? You can use lock_timestamp to time out any old locks with a scheduled job.

Really, you're best off writing a small server-side process to handle all of this. I once tried to do similar stuff purely on the client-side (with the help of a couple of small stored procedures) and it worked but it was painful and complicated. A proper server-side process would have been much better.

share|improve this answer
    
that's what I talking about, but i don't want to do a web service. what do you think about Socket Server/Client? Server will have the logic & SQL Operations, Client will request these services, i Found using GSON will do the work, but i didn't figure out how to populate JTable from GSON Response Or CML?, i really need this. –  Motasem Abu Aker Oct 31 '13 at 19:12
1  
You don't need to use web services if you don't want to. I don't have much recent experience programming with sockets so I can't give you a lot of detailed technical direction for that, but I still think it's a bad idea for the clients to connect directly to the database. –  FrustratedWithFormsDesigner Oct 31 '13 at 19:23
    
Yes I know, But In case i used web service Tomcat with servlet, how & what technology i need to use? –  Motasem Abu Aker Oct 31 '13 at 19:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.