Wednesday 7 October 2020

How to use Declarative transaction management in Spring


 Declarative Transaction in Spring 

@Tranactional

Spring Framework provide @Tranactional Annotation to mark class or class method which enable transactional management  processing  for that a class or a class particular method .   For Spring transaction management Spring provide class 
org.springframework.jdbc.datasource.DataSourceTransactionManager
and for enable transaction management you have to provide following bean definition .

<tx:annotation-driven transaction-manager="transactionManager" />
     
     <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="postgresDatasource"></property>
</bean>

bellow the source code Spring trancation management application .

EmployeeDao.java

package in.jk.spring.transaction.dao;

import in.jk.spring.transaction.Employee;

public interface EmployeeDao {

public void addEmployee(Employee employee);

}

EmployeeDaoImpl.java

package in.jk.spring.transaction.dao;

import java.sql.SQLException;
import java.sql.PreparedStatement;
import in.jk.spring.transaction.Address;
import in.jk.spring.transaction.Employee;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;

public class EmployeeDaoImpl implements EmployeeDao {

private JdbcTemplate jdbcTemplate;

@Override
public void addEmployee(Employee employee) {

Address address = employee.getAddress();

String employeeQuery = "Insert into employee values (?,?,?)";
String addressQuery = "INSERT into employee_address values (?,?,?,?,?)";

jdbcTemplate.execute(employeeQuery, new PreparedStatementCallback<Boolean>() {

@Override
public Boolean doInPreparedStatement(PreparedStatement preparedStatement)
throws SQLException, DataAccessException {

preparedStatement.setInt(1, employee.getEmployeeId());
preparedStatement.setString(2, employee.getName());
preparedStatement.setString(3, employee.getCompany());
                                return preparedStatement.execute();
}
});

System.out.println("Employee Added .....");
jdbcTemplate.execute(addressQuery, new PreparedStatementCallback<Boolean>() {

@Override
public Boolean doInPreparedStatement(PreparedStatement preparedStatement)
throws SQLException, DataAccessException {

preparedStatement.setInt(1, address.getAddressId());
preparedStatement.setString(2, address.getCity());
preparedStatement.setString(3, address.getCountry());
preparedStatement.setString(4, address.getPincode());
preparedStatement.setString(5, address.getState());
                                return preparedStatement.execute();
}
});

System.out.println("Employee address Added ...");
System.out.println("Employee Data Added Successully ...");

}

public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

}

EmpoyeeService .java

package in.jk.spring.transaction.service;

import in.jk.spring.transaction.Employee;

public interface EmpoyeeService {
public void addEmployee(Employee employee) ;

}

EmployeeServiceImpl.java

package in.jk.spring.transaction.service;

import org.springframework.transaction.annotation.Transactional;
import in.jk.spring.transaction.Employee;
import in.jk.spring.transaction.dao.EmployeeDao;

@Transactional
public class EmployeeServiceImpl implements EmpoyeeService  {

private EmployeeDao employeeDao;
@Override
public void addEmployee(Employee employee) {
employeeDao.addEmployee(employee);
}
public EmployeeDao getEmployeeDao() {
return employeeDao;
}
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
}

Address.java
package in.jk.spring.transaction;

public class Address {
private int addressId;
private String pincode;
private String city;
private String state;
private String country;
// Getter and Setter
}

package in.jk.spring.transaction;

public class Employee {
private int employeeId;
private String name;
private String company;
private Address address;

        // Getter and Setter
}

Context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
     <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="postgresDatasource"></property>
</bean>

    <bean id="employeeService" class="in.jk.spring.transaction.service.EmployeeServiceImpl">
<property  name="employeeDao" ref="employeeDao"></property>
</bean>
<bean id="employeeDao" class="in.jk.spring.transaction.dao.EmployeeDaoImpl">
<property  name="jdbcTemplate" ref="postgresJDBCTemplate"></property>
</bean>

<bean id="postgresDatasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url"
value="jdbc:postgresql://localhost:5432/postgres"></property>
<property name="driverClassName"
value="org.postgresql.Driver"></property>
<property name="username" value="postgres"></property>
<property name="password" value="jk123"></property>
</bean>

<bean id="postgresJDBCTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="postgresDatasource"></property>
</bean>
</beans>  

SpringTransactionApplication.java

package in.jk.spring.transaction;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import in.jk.spring.transaction.service.EmpoyeeService;

public class SpringTransactionApplication {
public static void main(String[] args) {
ApplicationContext applicationContext =  null;  
                 applicationContext = new  ClassPathXmlApplicationContext("Context.xml");
Address address = new Address();
address.setAddressId(1234);
address.setPincode("201203");
address.setCity("Ghaziabad");
address.setState("Uttar Pradesh");
address.setCountry("India");
Employee employee = new Employee();
employee.setEmployeeId(1234);
employee.setName("J k");
employee.setCompany("Google");
employee.setAddress(address);
EmpoyeeService empoyeeService = (EmpoyeeService) applicationContext.getBean("employeeService");
empoyeeService.addEmployee(employee);
}

}

No comments:

Post a Comment