In Spring Boot with JdbcTemplate, transactions can be managed using the TransactionTemplate or by using the @Transactional annotation.
Example:@Component
public class EmployeeService {
private final JdbcTemplate jdbcTemplate;
private final TransactionTemplate transactionTemplate;
public EmployeeService(JdbcTemplate jdbcTemplate, PlatformTransactionManager transactionManager) {
this.jdbcTemplate = jdbcTemplate;
this.transactionTemplate = new TransactionTemplate(transactionManager);
}
public void transferSalary(int employeeId1, int employeeId2, int amount) {
transactionTemplate.execute(status -> {
try {
jdbcTemplate.update("UPDATE employee SET employeeSalary = employeeSalary - ? WHERE employeeId = ?", amount, employeeId1);
jdbcTemplate.update("UPDATE employee SET employeeSalary = employeeSalary + ? WHERE employeeId = ?", amount, employeeId2);
return true;
} catch (Exception e) {
status.setRollbackOnly();
return false;
}
});
}
}
In the above example, we have a transferSalary method that transfers an amount from one employee to another. The method is wrapped in a transactionTemplate.execute() call, which manages the transaction.
If an exception is thrown during the transaction, the transaction will be rolled back.
We can also use the @Transactional annotation to manage transactions.
@Service
@Transactional
public class EmployeeService {
private final JdbcTemplate jdbcTemplate;
public EmployeeService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void transferSalary(int employeeId1, int employeeId2, int amount) {
jdbcTemplate.update("UPDATE employee SET employeeSalary = employeeSalary - ? WHERE employeeId = ?", amount, employeeId1);
jdbcTemplate.update("UPDATE employee SET employeeSalary = employeeSalary + ? WHERE employeeId = ?", amount, employeeId2);
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Programmatically Send an Email from Android App using Intent - Android
- JavaScript: Generate Random Numbers between 1 and 3 - JavaScript
- Sort Array in Ascending or Descending Order in Java - Java
- How to recover unsaved notepad file Windows 10 - NotepadPlusPlus
- Python List of Lists with Examples - Python
- Convert Collection List to Set using Java 8 Stream API - Java
- Go to Line Number option in Windows Notepad - NotepadPlusPlus
- Use 5G Network on Android Emulator - Android