Friday 23 October 2015

Spring Transaction with annotation for Spring JDBC


Step-1
  <!-- 
        Spring is doing jndi lookup to access the connection pool manage by our server
        jndi is service running on server.
   -->

 <jee:jndi-lookup id="bankDataSource" jndi-name="jdbc/synergy-bank-ds"/>

  "bankDataSource"  is pointing to the database connection. means we can communicate with database using bankDataSource.


Step-2
Create spring transaction manager for spring jdbc 

<!--
DataSourceTransactionManager class comes from spring jdbc module
-->

 <!-- Creating TransactionManager Bean, since JDBC we are creating of type 
DataSourceTransactionManager -->
<bean id="jdbctransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="bankDataSource" />

</bean>

Step-3
Associating above transaction  manager with annotation...........
@Transactional


<tx:annotation-driven  proxy-target-class="true" transaction-manager="jdbctransactionManager"/>



Step-4
Applying above transaction manager  inside the dao layer
@Repository("BankAdminDaoImpl")
@Scope("singleton")
@Transactional(propagation=Propagation.REQUIRED)
public class BankAdminDaoImpl extends JdbcDaoSupport implements BankAdminDao{

@Autowired
@Qualifier("bankDataSource")
// here we are injecting bankDataSource inside JdbcDaoSupport super class
public void setDataSourceInBank(DataSource dataSource) {
super.setDataSource(dataSource);
}


@Override
public List<CustomerEntity> findPendingCustomerList(){
List<CustomerEntity> customerList = super.getJdbcTemplate().query(AdminQuery.FIND_PENDING_CUSTOMER_LIST,
new BeanPropertyRowMapper<CustomerEntity>(CustomerEntity.class));
return customerList;
}

P.S. - > Spring Declarative Transaction is based on AOP..

How to know spring transaction is working or not..............??

boolean action=TransactionSynchronizationManager.isActualTransactionActive();

-------------------------------------------------------------------------------------------
action - true means spring transaction is enabled inside this method

Files involves in spring transaction for spring jdbc
1. synergyDataSource.xml
2. BankAdminDaoImpl.java



No comments:

Post a Comment