Question for the best way to implment a check for uniqueness of data in Tamino.
Let’s suppose we have person data inside XML document and each person has a unique ID. Let’s suppose that two people like to insert the same person at the same time. The system should garanty that one insert should work and the second one should return some duplicate exception.
As far as I know, there is no way to define a certain node as being unique (like possible inside a relational database).
I can think of a way to insert the docuument, check afterwards the number of documents found with the just inserted unique ID. When more then one document is found, then we delete the just inserted document, or use rollback when transactions are used.
Is there someboy who have implmented the check using another kind of technique?
There is a nice sample in the documentation, which solves exactly your problem.
Go to “Tamino”, “Documentation” in the start menu, select “Tamino API for Java”, “Doing more with the API”, “All That Jazz”, “Testing for Unique Keys”. There you go …
Seems to work fine, but is there no other way to do it?
Perhaps this way is simple enough, yet powerful enough for your needs:
Tamino enforces a unique constraint on the ino:docname attribute, and there is a method in TXMLObject: setDocname(String name).
If you pass the unique ID of the person to the setDocname() method, when you insert the TXMLObject Tamino will throw an exception if the ino:docname is not unique.
This would also bring the potential benefit that you can directly retrieve the person instance by the ino:docname/unique person ID.
You may need to do some extra work to make the “person unique ID” value available when you create the TXMLObject, but otherwise this should be straightforward.
Would that be better for your case?
Seems to be a much easier way of working. WE know in the businesscode the unique id of a person because it is a value the user has to enter (= required). So if two users enter the same info, the system will put in the docname twice the same value. Result will be that one will be inserted and the other one should fail with an exception.
I’m gone test this…
I’ve tested the proposed way of working but Tamino isn’t throwing an execption when the second user tries to insert a document with the same docName.
The documentation mentions that this is a normal way of reaction :
Inserts the XML document represented by the TXMLObject instance into the Tamino database. If the TXMLObject carries an id, the insert operation ignores it. If an XML document already exists with the same docname this object will be replaced and no new object will be inserted. If the insert succeeds, the id, collection and doctype properties are set on the TXMLObject instance. The following characterizes this behavior: !xmlObject.hasDocname() Insert.
xmlObject.hasDocname() Insert, if a document with the given ino:docname does not exist.
xmlObject.hasDocname() Update, if a document with the given ino:docname already exists
Like mentioned, no exception is throwed.
Oooops, sorry! I must have been asleep when I wrote that suggestion.
Given what you now know, perhaps we can see exactly why the documentation explains how to ensure uniqueness using transactions.
Would you like to try out the documented approach, or is there some concrete reason restricting you from using that?
I’ve implemented the way the sample is doing it now and it works fine. I was just wondering if there wasn’t a more simple way doing it instead of open ing second transaction, etc… This is more resource concumiong then needed.