Monday, December 16, 2013

Examples on Spring Transaction Management


In Spring There are 2 ways to use Transaction Management

          1. Programmatic.                                    
          2. Declarative.

Programmatic Approach is outdated now so I don't want to Discuss it. So We move to Declarative Approach. In Declarative Approach There are 2 ways ...

      Using Xml Configurations.
      Using Annotations.

Let Me use 'Xml Configurations' in my following Eg. application.

Example on Spring Transaction Management using 'Xml Configurations':

In My Example i am taking

 Demo(I)
 DemoBean(C)
 Spring.cfg.xml(Spring configuration file)
 DemoClient(Class To Run Application)
 and mysql database.

Demo:
package p1;

public interface Demo {
public void bm1();
}

DemoBean:
package p1;

import org.springframework.jdbc.core.JdbcTemplate;
public class DemoBean implements Demo{
private JdbcTemplate jt;

/**
* @param jt the jt to set
*/
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}

@Override
public void bm1() {
int r1=jt.update("insert into Employee (eid,name,salary) values(2012,'Arjun1',100000)");
int r2=jt.update("UPDATE Employee set salary=50000 where eid=2010");

if(r1==0 || r2==0){
System.out.println("Transaction is Rollbacked.");
throw new RuntimeException();
}else{
System.out.println("Tx is Committed.");
}
}
}

DemoClient:
package p1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class DemoClient {

public static void main(String s[]){

/*FileSystemResource res=new FileSystemResource("Spring.cfg.xml");
XmlBeanFactory factory=new XmlBeanFactory(res);
Demo d=(Demo)factory.getBean("tfb");
d.bm1();*/
ApplicationContext ctx=new FileSystemXmlApplicationContext("Spring.cfg.xml");
Demo d=(Demo) ctx.getBean("tfb");
d.bm1();
}
}

Note: You can use Either BeanFactory or ApplicationContext container.

Spring.cfg.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
....">

<bean id="dmds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/database name"/>
 <property name="username" value="username"/>
 <property name="password" value="password"></property>
</bean>

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

<bean id="jt1" class="org.springframework.jdbc.core.JdbcTemplate">
   <property name="dataSource" ref="dmds"/>
</bean>
    <bean id="db" class="p1.DemoBean">
        <property name="jt" ref="jt1"/>
    </bean>

    <!-- Pointcut Advisor to apply Transaction Attributes. -->
    <bean id="tas" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
        <property name="properties">
            <props>
                <prop key="bm1">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
 
     <!-- Gives Proxy Object having Transaction Service. -->
    <bean id="tfb" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="target" ref="db"/>
        <property name="transactionManager" ref="dts"/>
        <property name="transactionAttributeSource" ref="tas"/>
    </bean>
 </beans>

Run it from DemoClient.

Example on Spring Transaction Management using  'Annotations':
In this Example I take..

 Demo(I)
 DemoBean(C)
 Spring.cfg.xml(Spring configuration file)
 DemoClient(Class To Run Application)
 and mysql database.

all are same as above Application Except these Changes

in DemoBean add @Transactional annotation on top of bm1() as shown below

 @Transactional(propagation=Propagation.REQUIRED)
   public void bm1()
  {.....
  .....
  ...}

2. The Spring Configuration file is as Follows
<beans....>
  <bean id="drds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/databasename"/>
        <property name="username" value="username"/>
        <property name="password" value="password"/>
    </bean>
 
    <bean id="txmgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="drds"/>
    </bean>

    <bean id="jt1" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="drds"/>
    </bean>
 
    <bean id="db" class="p1.DemoBean">
        <property name="jt" ref="jt1"/>
    </bean>
 
    <tx:annotation-driven transaction-manager="txmgr"/><!-- Enables Annotation Based Transaction Management. -->
</beans>


Run it with these Changes.

0 comments:

Post a Comment