I am working with a coworker on a project that uses Inductive Automation software. If you don't know what it is, all you need to know is it provides a drag-and-drop GUI designer (based in java swing) and lets you write jython 2.5 or jython 2.7 (depending on the version) at different extension points of components. Press a submit button, run this jython script, like that. It's great for quickly getting something up and running (and for it's main purpose of interfacing wiht PLC's but that's not relevant here). But as a result, it allows you to shoot yourself in the foot if you aren't paying attention. As a side result to this often leads to very procedural code, no OOP almost ever. I only bring that up in the case that OOP might be an answer to the following issue I am facing.
We recently had a problem of duplicate records in a database. My coworker said this was caused when people would double or triple click the button, running the jython script multiple times. My suggestion was to make a UNIQUE
index on whatever it is that defines the uniqueness of the table, so that if someone presses the button 3 times, we get that first record, but the next two are discarded as they violate the constraint. This would also allow us to do a try/except
, catch the error thrown back by the violated constraint and do something with that information if we wanted, like tell the user to slow down.
My coworker said my solution was just masking the problem, that we should fix the script so it doesn't do the duplicate inserts. This would require making it so the button could only be pushed once and then is disabled from future button presses until the script completes, or sometimes having a statement that checks the database for existence of the record first before inserting. I've explained the issue with the second way, that if someone double pressed the button super quickly, you could have two scripts running at the same time, checking the database table, seeing no duplicate record, and then running two inserts. But he insists then that we should script out that error.
I'm still relatively new to the software field, just getting into my third year while my coworker is the most senior person at the company, so we are going to be doing things his way. However, I can't shake the feeling that we are going about this wrong.
Whenever I make some personal application I always use a UNIQUE constraint when appropriate to avoid duplicates, but now I am wondering if that is a mistake. Can someone more experienced share their view? Is there a right way or are there good use cases for both ways?
Edit: Wow this blew up. So the main issue was that the coding/scripting part had a lot of race conditions that would have taken a while to refactor with a deadline pressing, and the issue where a person could double or triple click a button before the window was changed. It's supposed to be someone clicks the button, some logic is run and the window changes. But while the logic is running the button is still clickable, can be pressed a few times, hence duplicates.
To eliminate it at a scripting level would require eliminating race conditions and how all our submit buttons work/windows are opened and putting some logic into background threads etc - which SHOULD be done no doubt, but also I feel like we SHOULD have a UNIQUE INDEX as well to prevent these double-clicks/race conditions from creating unintended duplicates.
A lot of good info here. I appreciate all the input.