Spring Transactions: By, Srinivas Reddy.S
Spring Transactions: By, Srinivas Reddy.S
By,
Srinivas Reddy.S
www.JAVA9S.com
Introduction to Transactions
• A transaction comprises a unit of work performed
within a database management system (or similar
system) against a database.
• Transactions provide “all-or-nothing” proposition –
Each unit of work performed should be entirely
committed or rolled back.
Funds Transfer
Debit your account Transfer funds to other account Update the statement
Transaction
Rollback Transaction-
Commit –
Persist
Don’tdata
persist data
www.JAVA9S.com
Transactions - ACID
• Atomic: Atomicity ensures that all operations in the
transaction happen or none of them happen.
• Consistent: The system should be left in a consistent
state even if the transaction succeeds or fails.
• Isolated: Should allow multiple users to work.
Transactions should be isolated from each others work
– This prevents concurrent reads and writes.
• Durable: After successful transaction, the system
should store the details permanently.
www.JAVA9S.com
Before Spring Transactions
• EJB was a powerful API available offering Bean
Managed(BMT) and Container Managed
Transactions(CMT).
• It offers both programmatic and declarative
based transaction management.
Problems:
• Needs an application server to run the CMT.
• EJB relies on JTA for transactions.
• Using EJB itself is a heavy choice.
www.JAVA9S.com
Spring Transactions - Advantages
• Offers both programmatic and Declarative
transactions.
• Declarative transactions are equivalent of
the Container managed transactions and
there is no need for an application server.
• No need for using JTA.
• Wide range of Transactional Managers
defined in spring transactions API.
www.JAVA9S.com
Spring Transactions - Limitation
• Spring transactions are good at applications
using single database.
• If you need to access multiple databases,
distributed transactional(XA) management is
needed.
• Spring supports any kind of JTA
implementation
www.JAVA9S.com
Programmatic Vs Declarative
Transactions
• Programmatic transactions give precise control on the boundaries
of the transaction.
E.g..
ticketBooking(){
T1.start();
checkAvailability();
T1.Commit();
T2.start();
selectSeats();
payment();
ticketConfirmation();
T2.commit();
}
www.JAVA9S.com
Programmatic Vs Declarative
Transactions
• Declarative transactions are less intrusive
and are defined in a Configuration file.
• Developed based on the AOP concepts. This
gives an advantage of keeping the cross
cutting concerns like transactions out of our
DAO layer code.
www.JAVA9S.com
Spring Transaction Managers
• Note: Spring does not directly manage the
transactions.
• Springs transaction managers delegate the
responsibility to platform specific transaction
management API.
• Spring transaction managers act as façade to
platform specific transaction
implementation.
www.JAVA9S.com
Spring Transaction Managers
Platform Specific
Transaction Manager
www.JAVA9S.com
TransactionManagers Configuration
<bean id="transactionManager"class="org.springframework.jdbc.
datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.
orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"> <property
name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager"class="org.springframework.
transaction.jta.JtaTransactionManager">
<propertyname="transactionManagerName"
value="java:/TransactionManager"/>
</bean>
www.JAVA9S.com
Transaction Attributes
• Propagation
• Isolation
• Read only
• Roll back rules
• Timeout
www.JAVA9S.com
Transaction Attribute-Propagation
• Propagation behavior defines the boundaries
of a transaction.
• Propagation configuration tells the
transaction manager if a new transaction
should be started or can use the transaction
which already exists.
www.JAVA9S.com
Propagation Rules
• PROPAGATION_MANDATORY: Method should run in a transaction and
nothing exists – exception will be thrown
• PROPAGATION_NESTED: Method should run in a nested transaction.
• PROPAGATION_NEVER: Current method should not run in a transaction.
If exists an exception will be thrown.
• PROPAGATION_NOT_SUPPORTED: Method should not run in a
transaction. Existing transaction will be suspended till method
completes the execution.
• PROPAGATION_REQUIRED: Method should run in a transaction. If
already exists, method will run in that and if not, a new transaction will
be created.
• PROPAGATION_REQUIRES_NEW: Method should run in a new
transaction. If already exists, it will be suspended till the method
finishes.
• PROPAGATION_SUPPORTS: Method need not run in a transaction. But if
already exists, it supports one which is already in progress.
ISOLATION levels
• Dirty reads occur when transaction reads an
uncommitted data.
• Non-repeatable Reads occurs when a
transaction reads the same data multiple
times but gets different results each time.
• Phantom Reads occur when two transactions
work on a same row where one updates and
other reads. The reading transaction get new
data.
ISOLATION attributes
• ISOLATION_DEFAULT – default isolation specific to the data
source.
• ISOLATION_READ_UNCOMMITTED – Read changes that are
uncommitted. Leads to dirty reads, Phantom reads and non
repeatable reads.
• ISOLATION_READ_COMMITTED: Reads only committed data. Dirty
read is prevented but repeatable and non repeatable reads are
possible.
• ISOLATION_REPEATABLE_READ: Multiple reads of same field yield
same results unless modified by same transaction. Dirty and non
repeatable reads are prevented but phantom reads are possible as
other transactions may edit the fields.
• ISOLATION_SERIALIZABLE: Dirty, phantom and non repeatable
reads are prevented. But hampers the performance of application.
www.JAVA9S.com
Read-Only
• By applying Read-Only to a transaction, the
underlying data store will apply some
performance optimizations to render data
more faster.
www.JAVA9S.com
Transaction Timeout
• By declaring the Timeout attribute, we can
ensure that long running transactions are
rolled back after certain number of seconds.
• The count down starts when transaction is
started .So, Timeout attribute can be
meaningful when applied for
PROPAGATION_REQUIRED
PROPAGATION_REQUIRES_NEW
PROPAGATION_NESTED
www.JAVA9S.com
Rollback
• Rollback tells a transaction manager when to
rollback a transaction when an exception
occurs.
• By default the transactions will be rolled
back when runtime exceptions occurs.
• But, by specifically mentioning the checked
exceptions, transaction manager will be able
to rollback the transactions.
www.JAVA9S.com
Thank you
Follow me on to get more updates on latest video posts
Subscribe on
http://www.youtube.com/user/java9s
Twitter : @java9s
facebook: www.facebook.com/java9s
www.JAVA9S.com