Tofetopo
Tofetopo

Reputation: 496

how to stop Java application from inserting same values in database - SQL server

I'm working in an application that saves process data (processName, processID, sessionStart, sessionEnd, computerName, currentUser) into the database every time a specific device is being in use by a process.

My problem is that the data process is being added over and over again (every 4 seconds as per specified in the runnable task), so I end up with many similar rows, I would like to find a solution for the process just being added once, and then one more time adding a sessionEnd datetime when the device stop being used. My code is as follows:

if(found){
    while(found == true){
        System.out.println("\nALERT! Device in use!\nThe process currently using the device is: \n" + strLineProcess+"\n");

        final String regex = "^(\\S+) pid: (\\d+) ([^\\\\s]+)\\\\(.+)";
        final String processDetails = strLineProcess;
        String processName = null;
        String processID = null;
        String computerName = null;
        String currentUser = null;


        final Pattern pattern = Pattern.compile(regex);
        final Matcher matcher = pattern.matcher(processDetails);

        if (matcher.find()) {
            processName = matcher.group(1);
            processID = matcher.group(2);
            computerName = matcher.group(3);
            currentUser = matcher.group(4);
        }   

        //Array containing all the process data
        String[] processData = new String[6];

        processData[0]=""+processName;
        processData[1]=""+processID;
        processData[2]=getDateTime();
        processData[3]="";
        processData[4]=""+computerName;
        processData[5]=""+currentUser;

        String[] processRepeated = new String[1];


        insert.insertRow(processData);

        break;
    }
}

I though about do a select from the application, check if the processID is the same than the one I'm trying to insert, and if so, don't do anything, but I found this a bit clumsy as all this needs to happen every 4 seconds. Just can't get my head around it!

Just to clarify, strLineProcess contain a string similar to this: Skype.exe pid: 3068 WATCHOUT\tofetopo , which I divide with regex to add each value to its corresponding variable.

Upvotes: 0

Views: 74

Answers (1)

Mike Nakis
Mike Nakis

Reputation: 61993

It is not clear from the information that you have provided, but could you solve your problem simply by adding a unique index on all columns except the datetime column?

The next easiest thing to try would be searching the database for an already existing row. There is nothing wrong with it happening every 4 seconds, or every 4 milliseconds, and it is advisable to start with the easiest possible solution, and only provide a more exotic fix if the easy solution really causes problems.

The exotic fix would be to maintain a list in memory, caching all the rows that you have seen in the last X seconds, and to search through that list for duplicates before inserting a new row. Every time you iterate through the list you drop any entries that are too old and not of interest anymore.

Upvotes: 1

Related Questions