First, a warning: This post gets into issues that I think are complicated, but they are mostly implementation details not related to using jstm, which is easy :)
A few days ago I sent links to jstm to people I thought could be interested. I had the surprise to have an answer from Chris Brumme. In person. What a great day :) He made an interesting comment about distributed transactions. I quote also the beginning of his message because I like it:
It’s an interesting idea and may lead to an appealing programming model.
Of course, distributed transactions work best over short distances. I reject the notion of 2-phase commit over the Internet, and I dislike it in a data center or cluster.
The “indeterminate” state of 2PC is difficult to build on top of, particularly if partitions can endure. In some ways, it’s more appealing to build on top of asynchronous messages that can get delayed or lost. At least there you know that indeterminacy is unavoidable and the application will naturally build it into its assumptions.
Certainly transactional memory is a fascinating topic. It has the potential to evolutionize concurrency the way garbage collection revolutionized memory management. But for me it’s still too soon to know whether it will fulfil that potential.
I agree that it is difficult to build upon distributed transactions, but here is why I think that it is not a big issue with jstm:
Jstm transactions are not really distributed in the sense that there is only one authority who decides if they are committed or discarded. So even if transactions modify objects spread on multiple machines, the actual commit is always done on the same machine. In the client-server topology, the server is the authority. In the cluster one, a coordinator is elected (It will be possible to change it at runtime in the future). Let’s take an example with the client-server topology:
A server and clients applications have a share with identical objects. A client starts a transaction, modifies a few objects. For now those changes are private to the client, they are kept in the transaction. When the client wants to commit, the share will "intercept" this commit, the whole transaction with private changes is sent to the server, and it is applied on the server’s copies of the objects. The server decides if the transaction can commit, and then broadcasts the new version of the share: It sends an acknowledgement to the initiating client and forwards the changes to the other clients.
The server does not wait for all clients to have acknowledged the changes before considering a transaction committed, because we would then return to the joys and issues of 2 phases commit. It simplifies a lot, but then what happens if a client does not receive a change? To go in Chris’s direction, a library should not hide this indeterminacy but expose it to the developer, if possible in a way that naturally makes him deal with it. Jstm could probably do better with this. For now the solution is simple:
First transaction commits are asynchronous. The method is beginCommit(callback), so the developer has to wait explicitly for the callback to know that his transaction has succeeded. Second, jstm has a simple heartbeat mechanism which disconnect clients is they do not acknowledge changes after a certain time. When a client gets disconnected, all his local state is considered invalid so his share is cleared and pending transactions are discarded. If he manages to reconnect, he can download the content of the share again and continue to work.
If you are in Paris on July 4, go see Luciano show our GWT version
here