Understanding the Spring Framework transaction abstraction - Java-Springs

The key to the Spring transaction abstraction is the notion of a transaction strategy.A transaction strategy is defined by the org. spring frame work. transaction. Platform Transaction Manager interface:

public interface PlatformTransactionManager {
TransactionStatus getTransaction(TransactionDefinition definition)
throws TransactionException;
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;

This is primarily a service provider interface (SPI), although it can be used programmatically from your application code. Because Platform Transaction Manager is an interface, it can be easily mocked or stubbed as necessary. It is not tied to a lookup strategy such as JNDI.

Platform Transaction Manager implementations are defined like any other object (or bean) in the Spring Framework IoC container. This benefit alone makes Spring Framework transactions a worthwhile abstraction even when you work with JTA.Transactional code can be tested much more easily than if it used JTA directly.

Again in keeping with Spring's philosophy, the Transaction Exception that can be thrown by any of the PlatformTransactionManager interface's methods is unchecked (that is, it extends the java.lang.RuntimeException class). Transaction infrastructure failures are almost invariably fatal. In rare cases where application code can actually recover from a transaction failure, the application developer can still choose to catch and handle TransactionException. The salient point is that developers are not forced to do so.

The get Transaction(..) method returns a Transaction Status object, depending on a Transaction Definition parameter. The returned Transaction Status might represent a new transaction, or can represent an existing transaction if a matching transaction exists in the current call stack.The implication in this latter case is that, as with Java EE transaction contexts, a TransactionStatus is associated with a thread of execution.

The TransactionDefinition interface specifies:

  • Isolation: The degree to which this transaction is isolated from the work of other transactions. For example, can this transaction see uncommitted writes from other transactions?
  • Propagation: Typically, all code executed within a transaction scope will run in that transaction.However, you have the option of specifying the behavior in the event that a transactional method is executed when a transaction context already exists. For example, code can continue running in the existing transaction (the common case); or the existing transaction can be suspended and a new transaction created. Spring offers all of the transaction propagation options familiar from EJB CMT.
  • Timeout: How long this transaction runs before timing out and being rolled back automatically by the underlying transaction infrastructure.
  • Read-only status: A read-only transaction can be used when your code reads but does not modify data.Read-only transactions can be a useful optimization in some cases,such as when you are using Hibernate.These settings reflect standard transactional concepts. Understanding these concepts is essential to using the Spring Framework or any transaction management solution.

The Transaction Status interface provides a simple way for transactional code to control transaction execution and query transaction status. The concepts should be familiar, as they are common to all transaction APIs:

public interface TransactionStatus extends SavepointManager {
boolean isNewTransaction();
boolean hasSavepoint();
void setRollbackOnly();
boolean isRollbackOnly();
void flush();
boolean isCompleted();

Regardless of whether you opt for declarative or programmatic transaction management in Spring, defining the correct Platform Transaction Manager implementation is absolutely essential. You typically define this implementation through dependency injection.

Platform Transaction Manager implementations normally require knowledge of the environment in which they work: JDBC, JTA, Hibernate, and so on.The following examples show how you can define a local Platform Transaction Manager implementation.(This example works with plain JDBC.)

You define a JDBC DataSource:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

The related Plat form Transaction Manager bean definition will then have a reference to the DataSource definition. It will look like this:

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>

If you use JTA in a Java EE container then you use a container Data Source, obtained through JNDI, in conjunction with Spring's Jta Transaction Manager.

The JtaTransactionManager does not need to know about the DataSource, or any other specific resources, because it uses the container's global transaction management infrastructure.

The Data Source bean definition will be similar to the local JDBC example shown previously and thus is not shown in the following example.

The tx Manager bean in this case is of the Hibernate Transaction Manager type. In the same way as the Data Source Transaction Manager needs a reference to the DataSource, the Hibernate Transaction Manager needs a reference to the SessionFactory.

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<property name="hibernateProperties">
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />

If you are using Hibernate and Java EE container-managed JTA transactions, then you should simply use the same JtaTransactionManager as in the previous JTA example for JDBC.<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>

In all these cases, application code does not need to change.You can change how transactions are managed merely by changing configuration, even if that change means moving from local to global transactions or vice versa.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics