Mock tests, Interview questions, Tutorials and Tech news
 
 

Archive

Posts Tagged ‘Spring Transactions’

@Transactional(readOnly=true) in Spring

December 19th, 2010 Vinay 2 comments

@Transactional(readOnly=true) in Spring

What does this mean ?

When a Multi Version Concurrency Control database (Oracle, Postgresql, MySQL + InnoDb) is used, a read only transaction can be translated to the non standard isolation level: READ_ONLY.

The READ_ONLY isolation level provides the same protection as the SERIALIZED isolation level (no dirty reads, no unrepeatable reads, no phantom reads) but doesn’t allow any updates. It also doesn’t cause any lock contention because no locking is required (the database is able to revert back to previous versions of the records ignoring all new changes).

Ms Sql 2005 also has a similar isolation level: SNAPSHOT.

If you specify readOnly as true, the flush mode will be set as FlushMode.NEVER in the current Hibernate Session preventing the session from committing the transaction. Read more…

Spring Transaction Management – Mock Test

September 14th, 2009 Vinay No comments

A mock test on  Spring Transaction Mock test has been added to skill-guru.  This will test your understanding about Spring transaction management. Some questions are based on code samples and some are based on on Spring documentation

This test in addition to Spring technical test / Spring Interview questions

This test will also help in Spring Certification exams

Update: A new test Core Spring Certification Mock has been added bu danielbutcher

User defined exception thrown in Spring Transaction

August 17th, 2009 Vinay No comments

This post is a result of a problem I faced while working on one of my project. Spring is used for managing transactions and we have not upgraded to Spring 2.5 annotation based transaction. But the concept here is important not the code

We had service class which had transaction attributes defined in applicationContext.xml file as

<bean id=”helloService” parent=”txProxyTemplate”>

<property name=”target”>
<bean
class=”com.skillguru.impl.HelloServiceImpl”
autowire=”byName” />
</property>

<property name=”transactionAttributes”>
<props>
<prop key=”*”>PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

Within the same file,  the transaction proxy template is defined as

<bean id=”txProxyTemplate” abstract=”true”
class=”org.springframework.transaction.interceptor.TransactionProxyFactoryBean”>
<property name=”transactionManager”>
<ref bean=”transactionManager” />
</property>
<property name=”transactionAttributes”>
<props>
<prop key=”save*”>PROPAGATION_REQUIRED</prop>
<prop key=”remove*”>PROPAGATION_REQUIRED</prop>
<prop key=”*”>PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

The class HelloServiceImpl throws a User defined exception, UserDuplicateException

The logic is that a method HelloServiceImpl#saveUser inserts the user ina  aloop and then checks for duplicate user based on some coniftions.

If the user is duplicate , none of the records are to inserted.

This works fine on paper but not in real world. Reason being UserDuplicateException is  a user defined exception and not a an instance or subclass of RuntimeException, Spring was not rolling back the transaction.

Look at Spring docs here

For the transaction to roll back, the exception has to be configured like this

<bean id=”helloService” parent=”txProxyTemplate”>

<property name=”target”>
<bean

autowire=”byName” />
</property>

<property name=”transactionAttributes”>
<props>
<prop key=”*”>PROPAGATION_REQUIRED,-UserDuplicateException</prop>
</props>
</property>
</bean>

Look at -UserDuplicateException. This is the key here. For a user defined exception to rollback , you to have define it explicitly with a -ve sign.

Things have changed in newer version of Spring but the concept is still same.

Other Spring related posts

Spring Interview Questions

Which is better : Spring’s Transaction management or EJB’s Container managed transaction


Which is better : Spring’s Transaction management or EJB’s Container managed transaction

July 27th, 2009 Vinay 1 comment

I was a big fan of EJB’s CMT  (Container managed transaction) feature because it reduces the developer of complex task of managing transaction through code. One of the biggest drawback of this was that this feature could only be used with EJB’s and not ordinary Java classes.

Then came the Spring’s Transaction management feature which changed the way applications can manage transactions.  We will explore about Spring’s transactional capabilities and if they are better than EJB’s transaction

Read more…

Get Adobe Flash playerPlugin by wpburn.com wordpress themes