Practical Microservices Architectural Patterns by Binildas Christudas

Practical Microservices Architectural Patterns by Binildas Christudas

Author:Binildas Christudas
Language: eng
Format: epub
ISBN: 9781484245019
Publisher: Apress


Distributed Transactions Revisited

Before you look into concrete examples, you need to understand a few concepts that will set the context for the examples you will explore.

Local Transactions

If you take a typical resource manager, typically that single resource will be confined to a single host or node (even though that may not be the case mandatorily). Operations confined to such a single resource are local transactions and they affect only one transactional resource. Within a single node, there are less (or for practical considerations, nil) nondeterministic operations, hence a command sent to a single node must be considered deterministic, and in case of any catastrophes, there are local recovery mechanisms. These resources have their own transactional APIs, and the notion of a transaction is often exhibited as the concept of a session, which can encapsulate a unit of work with demarcating APIs to tell the resource when the buffered work should be committed to the underlying resource. Thus, from a developer perspective, you do not manage with transactions in a local transaction, but with just “connections.”

The java.sql.Connection interface is a transactional resource that can wrap a database. By default, a Connection object is in auto-commit mode, which means that it automatically commits changes after executing each statement. If auto-commit mode is disabled, the method commit must be called explicitly in order to commit changes; otherwise, database changes will not be saved. It is preferable to collect several related statements into a batch and then commit all, or none, when you have more than one statement. You do this by first setting the Connection’s setAutoCommit() method to false and later explicitly calling Connection.commit() or Connection.rollback() at the end of the batch. See Listing 13-1.try{

connection.setAutoCommit(false);

Statement statement = conn.createStatement();



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.