Reyes RK
Reyes RK

Reputation: 1

Automatically update entries in an sql database table based on the values of another table

there's this thing where the program has to automatically change an item's column's value in a table based on their Type ID. Basically

-Maintenance Sched Table has Type ID and No. of Days -Items Table has Item ID, Type ID and Status ID

If an Items Table entry's Type ID has a matching Type ID in the MS Table, the Status ID changes (ex. from 1 [active] to 2 [inactive]) every [No. Of Days] days. The big problem i'm facing is that the items table has nearly 10k entries and I have no idea if it'll brick my pc or not if I run it kek. Y'all have any optimization ideas?

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleMaintenance {
    private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    private ArrayList<Item> itemsList;
    private ArrayList<Item> maintSched;
   
    public void startScheduler() {
        // Retrieve the lists from SharedData
        itemsList = SharedData.getInstance().getItemsList();
        maintStat = SharedData.getInstance().getMaintStat();
        maintSched = SharedData.getInstance().getMaintSched();
    int delay = 0;

        // Loop through itemsList to find matches in maintSched
        for (Item item : itemsList) {

            // Loop through maintSched to check for matching typeID
            for (Item schedItem : maintSched) {
                if (item.getItemTID() == schedItem.getItemTID()) {
                    delay = 10;
            break;
                } else if (item.expiration != null){
            delay = 20
            break;
        }
            }

            if (delay > 0) {
        scheduler.schedule(() -> performMaintenance(item), delay, TimeUnit.DAYS);
        }
        }
        
    }

    public void stopScheduler() {
        scheduler.shutdown();
        try {
            if (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) {
                scheduler.shutdownNow();
            }
        } catch (InterruptedException e) {
            scheduler.shutdownNow();
        }
    }

    private void performMaintenance(Item item) {
        // SQL Update here, for now, just printing
        System.out.println("Performing maintenance on item with ID: " + item.getItemID() + " and typeID: " + item.getItemTID());
    }
}

Upvotes: 0

Views: 36

Answers (0)

Related Questions