Explainer
Handling Java concurrency on an edge gateway without a database
Short answer
No database engine is documented for the embedded Java runtime on this class of edge gateway, and string tags are not supported there. Keep shared in-memory state in a synchronized Vector, run every read-modify-write sequence and every file update through one synchronized single-writer boundary, and leave integer and float tags to process values.
On this page
You do not need a database; you need one atomicity boundary of your own. On the gateway platform this design targets, the tag interface exposed to the Java application carries integer and floating-point values only, with no string tags, and no database engine is documented for the runtime. Treat both as fixed boundaries. Keep shared string state in a synchronized collection such as Vector, route every read-modify-write sequence and every file update through a single synchronized writer, and leave tags to their real job of carrying process values. The Java platform documents Vector and Hashtable as thread-safe collections whose individual operations are synchronized, and the Java memory model guarantees that what one thread writes before releasing a lock is visible to the next thread that acquires it. Build on those two documented facts and the concurrency problem closes without any database.
What the runtime gives you, and what it does not #
Three documented facts set the design space:
- Tags hold integer and floating-point values on the Java-side tag interface of the gateway in question, and strings are not supported there. A tag cannot hold a shared string, and it was never a lock: it has no acquire and release, so two threads can both see a flag clear between polls.
- Vector and Hashtable are the thread-safe collections. Each individual operation on them is synchronized.
- Files come through an ordinary file API with no documented transactional behavior across files or write sequences. Atomicity on disk is something you impose by design, not something the runtime hands you.
Everything below follows from accepting those three lines rather than fighting them.
Three properties your design must deliver #
Concurrency requirements come apart into three properties, and mixing them up is what makes this look like a database problem:
- Mutual exclusion: only one thread executes a given sequence at a time.
- Visibility: a write performed by one thread is seen by the next, not cached away.
- Sequence atomicity: a read-modify-write spanning several operations commits as one unit or not at all.
One Java monitor delivers all three, on one condition: every thread that touches the shared state uses the same monitor. The guarantee is the Java memory model defined in the Java Language Specification. Releasing a lock and later acquiring that same lock establish a happens-before relationship, so every write made before the release is visible after the acquisition. A database would give you the same three properties with persistence and queries added; here you need only the three, plus a disciplined file update.
Where Vector stops being enough #
Vector thread safety is per operation: each addElement or size call is atomic on its own. Two limits follow, and the platform documentation states the second explicitly:
- A sequence of calls is not covered. Read size, decide, then addElement: three individually atomic calls, one racy sequence. Two threads can pass the same check before either adds.
- Iteration is not covered. To iterate safely you must hold the collection lock for the entire duration of the enumeration; a thread that adds while another iterates can otherwise invalidate the iteration underneath it.
The practical conclusion: use Vector as the storage, not as the locking protocol. Put the protocol, meaning which sequences are indivisible and who may iterate when, inside one class that owns the data, and make every entry point into that class synchronized on the monitor of that class.
Build one atomicity boundary in code #
The class below is the whole mechanism for in-memory state. Vector is the documented API; every other name is this example's own.
public class RecordStore {
private java.util.Vector lines = new java.util.Vector(); // shared state, starts empty
private int lastId = 0; // highest id issued
public synchronized int append(String text) {
lastId = lastId + 1; // read-modify-write
lines.addElement(Integer.toString(lastId) + "," + text);
return lastId;
}
public synchronized int count() {
return lines.size();
}
public synchronized String snapshot() {
StringBuffer out = new StringBuffer();
for (int i = 0; i < lines.size(); i++) { // lock held for whole loop
out.append((String) lines.elementAt(i));
out.append('\n');
}
return out.toString();
}
}
The test that proves it, run on the target runtime:
// Two threads started together, each calling append() 1000 times.
// Pass: count() == 2000 and ids 1 through 2000 each appear exactly once.
The arithmetic is exact: 2 threads x 1,000 appends = 2,000 entries. Remove the synchronized keyword and Vector still keeps its internal structure intact, but lastId is yours and unprotected: two threads can read the same value before either stores its increment, every such interleave issues one id twice, and count() lands below 2,000 with a duplicate in the data. The synchronized method makes that interleave impossible and, through the memory model, makes each increment visible to the next caller. When that test passes on the shipped runtime, you have mutual exclusion, visibility and sequence atomicity over shared strings: the three things the tags were being asked to do and cannot.
Persist through a single writer and a file swap #
State that must survive a restart gets one more rule: exactly one thread writes files, and it never writes in place over the only good copy.
- The writer thread owns the file. Other threads hand it updates through a queue, a Vector used under the same monitor, and never open the file themselves.
- Each update writes a complete new file: a version or sequence number on the first line, then the full state. When the write completes, the writer swaps the new file over the old one.
- At startup the application reads the version line, keeps the newest complete file, and treats a truncated file as the previous version.
This is the atomicity the application actually asked for: after any interruption there is one last-known-good state, never a half-applied one. The same discipline shows up wherever edge software must persist with no local engine; managing recipes on Ignition Edge without a database is the same decision at the tag layer, and it holds here for the same reason.
One rule protects the plant side: never run the file write inside the monitor your controller-polling thread also needs, because a slow flash write inside that lock stalls the poll. Keep file input and output in the writer thread, outside the lock that guards memory.
Sizing decides how often you rewrite. Illustrative numbers: a 100 kB state file rewritten once per minute writes 100 kB x 1,440 = 144,000 kB, about 144 MB per day onto flash. Batch to one rewrite per 10 minutes and the same file writes about 14.4 MB per day, a factor of ten less. So rewrite on a timer and at shutdown rather than per record, and prune the state so the file stays small.
What not to build #
- No on-device SQL engine. None is documented for this runtime, and porting one into an embedded Java profile is a project with no payoff at gateway scale.
- No strings packed into integer tags. A string split across numeric tags updates piece by piece, so a reader can observe it half-written, and one lost piece breaks every parse that follows.
- No tag-based locking. A numeric tag can signal, but it is not a monitor: there is no acquire and release, no visibility guarantee, and every handshake pays the polling interval.
The decision, case by case #
- Strings shared between threads, memory only: one RecordStore-style class, synchronized methods, iteration inside a synchronized method.
- Multi-step sequences that must commit as one unit: widen the synchronized method to cover the whole sequence; never assemble atomicity from several separate synchronized calls.
- State that must survive restart or power loss: single writer thread, complete-file writes, version line, swap on completion, newest-complete-wins at startup.
- Data that needs history, range queries or grows past memory: that data does not belong on the gateway; keep a live window in the store and send the rest downstream, the way choosing an edge data path for periodic records lays out.
- Tags: integer and floating-point process values to the control side, nothing else. They carry no synchronization role in this design.
Questions asked
Does a synchronized Vector make a loop over its entries safe?
No. Vector thread safety is per operation: the platform documents each call as synchronized, and it also requires the lock to be held for the entire duration of an enumeration. Wrap the loop in a synchronized block on the collection, or better, make the iteration a synchronized method of the one class that owns the data. Hashtable has the same rule for its keys and values.
Can integer tags work as a lock between Java threads?
A tag can signal, but it is not a monitor. There is no acquire and release with a visibility guarantee, so two threads can both read the flag clear between polls and proceed together, and every handshake pays the polling interval in latency. Use one Java monitor as the lock and leave tags reporting values.
How large can the in-memory Vector grow on the gateway?
No documented heap figure covers every deployment, so measure on the unit you ship: load entries until free memory reaches a margin you set, record that count, and put the pruning threshold below it. That measured number, on your runtime build, is the limit; a figure borrowed from another device is not.
Is a single file write atomic if power is lost partway through?
Treat it as interruptible. Unless the runtime file API documents atomic replace, a write can stop partway and leave a short or half-updated file. The new-file-then-swap pattern does not depend on that guarantee: the previous complete file stays in place until the new one is fully written.
Should I encode strings as pairs of integer tags?
No. Reassembling a string from numeric tags has no atomic unit: a reader can catch the sequence half-updated, and one lost write corrupts every parse after it. Keep the string whole as one entry in the synchronized store, where each append is indivisible.