concept transaction in category spring

appears as: transaction, trsaction, trsactions, trsaction, trsactions, A trsaction
Spring Microservices in Action, 2E

This is an excerpt from Manning's book Spring Microservices in Action, 2E.

  • Credential management and propagation. How do you prevent a service client from constantly having to present their credentials for service calls involved in a transaction? To achieve this in this book, we’ll look at how token-based security standards such as OAuth2 and JSON Web Tokens (JWT) can be used to obtain a token that can be passed from service call to service call to authenticate and authorize the user.
  • Using correlation IDs to link together transactions across multiple services
  • Figure 11.2 With multiple services involved in a transaction, we can see that they share the same trace ID.
    Spring Microservices in Action

    This is an excerpt from Manning's book Spring Microservices in Action.

  • Log correlation— How do you tie together all the logs produced between services for a single user transaction? With this pattern, we’ll look at how to implement a correlation ID, which is a unique identifier that will be carried across all service calls in a transaction and can be used to tie together log entries produced from each service.
  • Using correlation IDs to link together transactions across multiple services
  • 9.3.5. Using Zipkin to trace transactions

    Let’s start this section with a scenario. Imagine you’re one of the developers on the EagleEye application and you’re on-call this week. You get a support ticket from a customer who’s complaining that one of the screens in the EagleEye application is running slow. You have a suspicion that the licensing service being used by the screen is running slow. But why and where? The licensing service relies on the organization service and both services make calls to different databases. Which service is the poor performer? Also, you know that these services are constantly being modified, so someone might have added a new service call into the mix. Understanding all the services that participate in the user’s transaction and their individual performance times is critical to supporting a distributed architecture such as a microservice architecture.

    Let’s dig into the details of the longest running call (3.204 seconds). You can see more detail by clicking on the transaction and drilling into the details. Figure 9.12 shows the details after you’ve clicked to drill down into further details.

    Figure 9.12. Zipkin allows you to drill down and see the amount of time each span in a transaction takes.

    Spring in Practice

    This is an excerpt from Manning's book Spring in Practice.

  • In recipe 2.3, you’ll replace your JDBC-based approach with ORM via Hibernate. We’ll also look at transactions.
  • Spring Integration in Action

    This is an excerpt from Manning's book Spring Integration in Action.

    4.2.2. What are transactions, and can we get by without them?

    Go to the most senior database administrator in your organization and ask this question. (It’ll be fun, we promise!) After you’ve listened carefully to his lecture, remember at least one thing: there’s no excuse for being clueless about transactions. That said, there’s no excuse for being a zealot, either. Transactions aren’t the answer to all your problems, and overusing them can be detrimental to performance and scalability.

    ACID, as we all know, stands for atomic, consistent, isolated, and durable. A transaction is supposed to give you all of this. Don’t be fooled: with a lot of ifs and buts, a transaction will give you something quite close to ACID, or an exception. The tricky part of ACID is isolation in combination with global consistency. Let’s look at two examples of multiple reasonable users in a standard web application.

    Even though we tend to focus on external system integration when discussing the roles of JMS, there are also benefits to using JMS internally within an application. JMS may be useful between producers and consumers running in the same process because a JMS provider can support persistence, transactions, load balancing, and failover. For this reason, Spring Integration provides a message channel implementation that delegates to JMS behind the scenes. That channel looks like any other channel as far as the message-producing and message-consuming components are concerned, so it can be used as an alternative at any point within a message flow as shown in figure 9.2.

    Figure 9.2. Design of the destination-backed channel of Spring Integration. It benefits from the guarantees supported by a JMS implementation, but it hides the JMS API behind a channel abstraction.

    Even for messaging within a single process, the use of a JMS-backed channel provides several benefits. Consider a message channel backed by a simple in-memory queue, as occurs when using the <queue> subelement within a <channel> without referencing any MessageStore. By default, such a channel doesn’t persist messages to a transactional resource. Instead, the messages are only stored in a volatile queue such that they can be lost in the case of a system failure. They’ll even be lost if the process is shut down intentionally before those messages are drained from the queue by a consumer. In certain cases, when dealing with real-time event data that doesn’t require persistence, the loss of those event messages upon process termination might not be a problem. It may be well worth the trade-off for asynchronous delivery that allows the producer and consumer to operate within their own threads, on their own schedules. With these message channels backed by a JMS Destination, though, you can have the best of both worlds. If persistence and transactions are important, but asynchronous delivery is also a desired feature, then these channels offer a good choice even if they’re only being used by producers and consumers in the same process space.

    The code is much simpler, and it also provides fewer chances for developer errors. Any JMSExceptions are caught and converted into RuntimeExceptions in Spring’s JmsException hierarchy. The JMS resources, such as Connection and Session, are also acquired and released as appropriate. In fact, if a transaction is active when this send operation is invoked, and some upstream process has already acquired a JMS Session, this send operation is executed in the same transactional context. If you’ve ever worked with Spring’s transaction management for data access, this concept should be familiar to you. The idea is roughly the same. If one particular operation in the transaction throws an uncaught RuntimeException, all operations that occurred in that same transactional context are rolled back. If all operations are successful, the transaction is committed.

    9.8. Managing transactions with JMS channel adapters and gateways

    Typically, the requirements of an application that utilizes a messaging system include such terms as “guaranteed delivery” and provide details about retry policies in anticipation of potential failure scenarios. Those requirements usually support one key concern: messages can’t be lost even if errors occur while processing them. In the worst case, after a number of attempts, a message should be delivered to a dead letter queue. Proper transaction management is essential when confronting such requirements. The primary goal when managing transactions in general is to ensure that data is always accounted for. No party should relinquish responsibility until another party has assumed responsibility. In a messaging interaction, the two parties that may assume responsibility at any given time are the producer and the consumer.

    Therefore, when working with JMS, it’s important to understand exactly how and where to apply transactions on the producer side as well as on the consumer side. It’s also important to consider whether the unit of work that should be transactional includes not only interaction with the JMS message broker but also some other transactional resource, such as a database. Finally, it’s important to understand the advanced options (those that may be outside of the JMS specification) that are available with a given broker, such as the retry policies. Such options are beyond the scope of this book, but the JMS provider’s documentation is a good place to start. If you’re using ActiveMQ, its options are covered in detail in ActiveMQ in Action. Here, we cover the general aspects of JMS transactions and specifically how they can be configured on Spring Integration’s JMS channel adapters and gateways.

    Spring Batch in Action

    This is an excerpt from Manning's book Spring Batch in Action.

    Why did ACME choose to build an online, web-based application to sell its products? As we mentioned, this is the best way for ACME to expand its business and to serve more customers. Web applications are easy to deploy, easy to access, and can provide a great user experience. ACME plans to deploy the online store application to a local web hosting provider rather than hosting it on its own network. The first version of the online store will provide a simple but efficient UI; ACME focuses on the catalog and transactions first, before providing more features and a more elaborate UI.

    Transactions make the interactions between an application and a data store reliable. When writing batch applications, you need to know exactly what you’re doing with transactions, because they affect the robustness of your batch jobs and even their performance. With a transaction, you can safely interact with a data store. An interaction consists of one or more operations—SQL statements if the data store is a database. “Safely” means that the transaction is atomic, consistent, isolated, and durable. We commonly refer to these kinds of transactions as having ACID properties. Here is what an ACID transaction requires:

  • Atomicity —All the operations in the transaction are successful, or none is.
  • Consistency —A transaction always leaves the data store in a consistent state.
  • Global transactions are different from local transactions, where only one resource is involved and the application directly communicates with the resource to demarcate transactions, as shown in figure 9.4.

    Figure 9.4. Local transactions between an application and a resource. The application directly communicates with the resource to demarcate transactions. Try to use local transactions as much as possible: they’re fast, simple, and reliable.

    9.3.1. Transaction attributes

    You learned in chapter 3 that you can use the transaction-attributes element in a tasklet element to set a transaction’s attributes, such as the propagation level, isolation level, and timeout. This allows you to have transaction attributes for a specific chunk different from the default attributes provided by a data source (which are commonly REQUIRED for the propagation level and READ_COMMITED for the isolation level).

    Listing 13.12. Configuring Spring Integration for a remote chunking slave
    <!-- JMS listener container --> <jms:listener-container connection-factory="connectionFactory" transaction-manager="transactionManager" acknowledge="transacted"> <jms:listener destination="requests" response-destination="replies" ref="chunkHandler" method="handleChunk"/> </jms:listener-container>
    Spring Dynamic Modules in Action

    This is an excerpt from Manning's book Spring Dynamic Modules in Action.

    Spring support is particularly useful in the data access area, with its common abstraction layer for transactions and its template-based approach to persistence. Spring deals with common resource management “plumbing” code and allows for declarative transaction management in various situations ranging from managed environments with JTA (Java Transaction API, the transaction standard in Java) to native database transaction with plain JDBC. Declarative transaction management is made possible through Spring AOP support and can be applied to any kind of component. It has no impact on application code, and switching from one environment to another is a matter of configuration. Application components can focus on their primary task and don’t even know they’re fully transactional.

    The last subject we’ll tackle in this chapter is that of transactions. We’ll see how to use Spring DM and the OSGi service registry to make transactions work smoothly.

    Figure 7.6. Autonomous service components manage transactions internally.

    Finally, transactions aren’t difficult to implement in an OSGi environment, but they can require the sharing of resources using the OSGi service registry, depending on how your bundles are organized.

    Dependency Injection: Design Patterns Using Spring and Guice

    This is an excerpt from Manning's book Dependency Injection: Design Patterns Using Spring and Guice.

    When you encounter classes whose dependencies cannot be mocked, you should probably rethink design choices. Older versions of EJB encouraged such container-dependent code. Infrastructure concerns like security, transactions, or logging often make things harder since their effects are not directly apparent. Developers tend to want to test everything as a whole and end up hacking together parts of a real application or environment. These don’t do justice to testing, because they remove the focus on testing individual units of functionality. Errors raised by such tests may be misleading because of subtle differences in application environments. So when testing, try focusing your attention on a single unit or class, mocking out the rest. If you can do this individually for all units, you will have much more confidence in them and can easily assemble them into working modules.

    So it looks like we’re on good ground. Before scoping, let’s define a use case. A nuclear power station needs software written to start the reactors and begin the flow of power to an electrical grid. The restriction is that there are three chambers to activate, and they must either all activate or none should. If the chambers are too hot, none must activate. We’ll model this as a transaction with a common logical goal, that being the startup of the power station. Listing 6.1 imagines this transactional power station.

    Listing 6.1. Three energy chambers activated in concert under a transaction
    public class PowerStation { ... public void start() { transaction.begin(); chamber1.fire(); chamber2.fire(); chamber3.fire(); transaction.commit(); } }

    We won’t worry too much about the transaction architecture. The important thing is the context that it provides. In PowerStation’s case, that context exists for the duration of method start(), finishing when it returns (figure 6.2 shows a model of this transaction). The call to transaction.commit() implies a successful completion of the transaction.

    6.1.1. A quick primer on transactions

    A transaction is a form of interaction with a data store that is atomic, consistent, isolated, and durable. Such transactions are sometimes referred to as ACID transactions. This is what they imply:

  • Atomicity— All of the tasks inside a transaction are successful or none are.
  • Consistency— Data modified by a transaction does not violate prior constraints.