Prevayler
The Open Source Prevalence Layer

Deterministic

Posted in Apr 4, 2008 by Justin Sampson - Edit - History

All your transactions must be completely deterministic, including any business object code called indirectly by a transaction. A transaction must always have exactly the same effect on the business objects whether it is being executed the first time or executed again later during journal recovery.

What does that mean in practice? Here are some things to avoid:

  • Don ' t call System.currentTimeMillis (), because it will give a different answer every time the transaction is run. If you do want to know the time, Prevayler provides an " official " timestamp, assigned when the transaction is first written to the journal. See the Transaction interface.
  • Don ' t access any files or sockets, because hardware I / O is inherently nondeterministic. Even if you ' re reading a file that always has the same contents, you could get an unexpected exception. Even if you ' re just writing to a log file and not depending on the results, again you could get an exception. (Doing any hardware I / O can also be arbitrarily slow, and you want your Prevayler transactions to run instantaneously.) You can, of course, do I / O outside of Prevayler and pass the actual data to your business objects through a transaction.
  • Don ' t use the default Object.hashCode () implementation, because it relies on the current low-level Memory address of the object, which will be different the next time the system runs.
  • Don ' t use the iterators of Hashtable, HashMap, or HashSet. They return entries in an arbitrary order that may be changed by some serialization mechanisms. LinkedHashMap is a good alternative, as it guarantees that the iteration order will be the same as the order that items were added. TreeMap and TreeSet keep entries sorted, so they ' re also okay.
  • Don ' t use Random without giving it a seed. It gets its seed from the system clock. SecureRandom is even worse, because it reads from hardware devices to get true random input. Instead, you can use Random or SecureRandom outside of Prevayler and pass random values or seeds in through a transaction.

Comments


harward?

Posted in Mar 26, 2008 by 80.203.147.164

fixed typo.

Posted in Mar 26, 2008 by Tasos Zervos