Skip to content

sendkumaranil/SpringTransactionManagement

Repository files navigation

SpringTransactionManagement

Spring Transaction Management Examples

Spring Transaction Management

Transaction:

Begin -------------->[Transaction]
						/\
					   /  \
				      /    \
					 /      \
	Transaction Succeed		Transaction Falied
			|						|
			|						|
			|						|
		  Commit				Rollback

ACID Properties:

  • A. Atomicity
  • C. Consistency
  • I. Isolation
  • D. Durability

Spring Transaction Management:

  • Supports programmatic and declarative transactions.
  • Programmatic transaction management achieve via
    PlatformTransactionManager
    TransactionTemplate
  • Declarative transaction management achieve via
    Spring AOP
    Annotation
  • Supports many transaction properties:
    • Propagation
    • Isolation level
    • Rollback condition
    • Read Only
    • Timeout

Spring transaction Built-In Implementations:

  															PlatformTransactionManager
  																		|
  														AbstractPlatformTransactionManager 
  																		|
  																		|
  	------------------------------------------------------------------------------------------------------------------------------------------
  	|						|						|								|								|						|
  	JtaTransactionManager	JpaTransactionManager	DataSourceTransactionManager	HibernateTransactionManager		JdoTransactionManager	JmsTransactionManager

Spring transaction management API:

PlatformTransactionManager interface

TransactionStatus getTransaction(TransactionDefinition difinition) throws TransactionException

void commit(TransactionStatus status) throws TransactionException

void rollback(TransactionStatus status) throws TransactionException

Programmatic Transaction Management:(Transactional Class)

	import org.springframework.transaction.support.TransactionTemplate
	
	public class TransactionalJdbcBookShop extends JdbcDaoSupport implements BookShop{
		private TransactionTemplate transactionTemplate;
		
		public void setTransactionTemplate(TransactionTemplate transactionTemplate){
			this.transactionTemplate=transactionTemplate;
		}
		
		public void purchase(final String isbn,final String username){
			transactionTemplate.execute(new TransactionCallbackWithoutResult(){
				
				protected void doInTransactionWithoutResult(TransactionStatus status){
				
				.......
				}
			
			});
		}
	}

Programmatic Transaction Management:(Bean Definition)

	<beans>
		
		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource"/>
		</bean>
		<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
			<property name="transactionManager" ref="transactionManager"/>
		</bean>
		<bean id="bookShop" class="com.springexamples.TransactionalJdbcBookShop">
			<property name="transactionTemplate" ref="transactionTemplate"/>
		</bean>
	</beans>

Declarative Transaction Management:

  • aop:config, aop:pointcut, aop:advisor
  • tx:advice, tx:method, tx:attributes
Annotative Transaction Management:
  • @Transactional
Transaction Attributes:
  • Propogation
  • Read-Only
  • timeout
  • Isolation
  • Rollback-rules
Transaction Propogation Attributes:
  • REQUIRED: If there is an existing transaction in progress, the current method should run within this transaction. otherwise, it should start a new transaction and run within its own transaction.
  • REQUIRES_NEW:The current method must start a new transaction and run within its own transaction. If there's an existing transaction in progress, it should be suspended.
  • SUPPORTS:If there's existing transaction progress, the current method can run within this transaction. otherwise, it is not necessary to run within a transaction
  • NOT_SUPPORTED:The current method should not run within a transaction.If there's an existing transaction in progress, it should be suspended.
  • MANDATORY:The current method must run within a transaction.If there's no existing transaction in progress, an exception will trown.
  • NEVER:The current method should not run within a transaction. If there's an existing transaction in progress, exception will be thrown.
  • NESTED:If there's an existing transaction in progress,the current method should run within the nested transaction of the transaction.Otherwise it should start a new transaction and run within its own transaction.
Transactional Isolation issues:
  • Lost Update
  • Dirty Read
  • Unrepeatable Read
  • Second lost updates problem
  • Phantom read
Transactional Isolation Levels:
  • Read Uncommited:Permits dirty reads but not lost updates. One transaction may not write to a row if another uncommited transaction has already written to it.This isolation level may be implemented using exclusive write lock.
  • Read Committed:Permits unrepeatable reads but not dirty reads.This may be achieved using momentary shared read locks and exclusive write locks. Reading transaction dont block other transaction from accessing row.However, an uncommitted writing transaction blocks all other transaction from accessing a row.
  • Repeatable Read:Permits neither unrepeatable reads nor dirty reads.Phantom reads may occur.This may be achieved using shared reads lock and exclusive write lock Reading transaction block writing transaction and writing transaction blocks all other transaction.
  • Serializable:Provides the strictest transaction isolation.It emulates serial transaction execution.as if transaction had been executed one after another,serially, rather than concurrently.Serializablity may not implemented using only row-level locks

About

Spring Transaction Management Examples

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages