Skip to content

Commit 6a6d0b8

Browse files
committed
Example of spring transaction and async
1 parent 37412cf commit 6a6d0b8

File tree

5 files changed

+86
-39
lines changed

5 files changed

+86
-39
lines changed
Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
11
package com.eprogrammerz.examples.spring.springtrasaction;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.boot.CommandLineRunner;
5-
import org.springframework.context.ApplicationContext;
6-
import org.springframework.context.annotation.Configuration;
7-
import org.springframework.core.task.TaskExecutor;
87
import org.springframework.stereotype.Component;
8+
import org.springframework.util.Assert;
9+
10+
import java.util.List;
911

1012
@Component
1113
public class AppRunner implements CommandLineRunner {
12-
@Autowired
13-
private ApplicationContext appContext;
14+
private static final Logger log = LoggerFactory.getLogger(AppRunner.class);
1415

1516
@Autowired
16-
private TaskExecutor taskExecutor;
17+
private BookingService bookingService;
1718

1819
@Override
1920
public void run(String... args) throws Exception {
20-
Runnable t1 = this.appContext.getBean(SpringTxThread.class);
21-
taskExecutor.execute(t1);
21+
bookingService.book("yogen", "pratima", "esma");
22+
23+
List<String> bookings = bookingService.findAllBookings().get();
24+
Assert.isTrue(bookings.size() == 3, "Booking should be for 3");
25+
26+
log.info("Bookings: {}", bookings);
27+
28+
try {
29+
bookingService.book("sabina", "prateema", "yogs");
30+
} catch (RuntimeException e) {
31+
log.info(" --- this is because 'prateema' violates constraints ----");
32+
log.error(e.getMessage());
33+
}
34+
35+
bookings = bookingService.findAllBookings().get();
36+
Assert.isTrue(bookings.size() == 3, "Still booking should be for 3");
37+
38+
log.info("Bookings: {}", bookings);
39+
40+
try {
41+
bookingService.book("sabina", null, "yogs");
42+
} catch (RuntimeException e) {
43+
log.info(" --- this is because 'null' violates constraints ----");
44+
log.error(e.getMessage());
45+
}
46+
bookings = bookingService.findAllBookings().get();
47+
Assert.isTrue(bookings.size() == 3, "Still booking should be for 3");
48+
49+
log.info("Bookings: {}", bookings);
2250

23-
Runnable t2 = this.appContext.getBean(SpringTxThread.class);
24-
taskExecutor.execute(t2);
2551

26-
Runnable t3 = this.appContext.getBean(SpringTxThread.class);
27-
taskExecutor.execute(t3);
2852
}
2953
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.eprogrammerz.examples.spring.springtrasaction;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.scheduling.annotation.Async;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
import java.util.List;
12+
import java.util.concurrent.CompletableFuture;
13+
14+
@Service
15+
public class BookingService {
16+
private final static Logger log = LoggerFactory.getLogger(BookingService.class);
17+
18+
@Autowired
19+
private JdbcTemplate jdbcTemplate;
20+
21+
/**
22+
* Send and forget async method
23+
*
24+
* @param persons
25+
*/
26+
@Transactional
27+
// @Async
28+
public void book(String... persons) {
29+
for (String person : persons) {
30+
log.info("Booking {} in a seat . . .", person);
31+
jdbcTemplate.update("insert into BOOKINGS(FIRST_NAME) values(?)", person);
32+
}
33+
}
34+
35+
/**
36+
* The result should be wrapped in CompleteableFuture for function returning value
37+
*
38+
* @return
39+
*/
40+
@Async
41+
public CompletableFuture<List<String>> findAllBookings() {
42+
log.info("Querying bookings . . .");
43+
List<String> bookings = jdbcTemplate.query("select FIRST_NAME from BOOKINGS", ((rs, rowNum) -> rs.getString("FIRST_NAME")));
44+
return CompletableFuture.completedFuture(bookings);
45+
}
46+
}

spring-trasaction/src/main/java/com/eprogrammerz/examples/spring/springtrasaction/SpringTrasactionApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableAsync;
56

67
@SpringBootApplication
8+
@EnableAsync
79
public class SpringTrasactionApplication {
810

911
public static void main(String[] args) {

spring-trasaction/src/main/java/com/eprogrammerz/examples/spring/springtrasaction/SpringTxThread.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
drop table BOOKINGS if exists;
2+
create table BOOKINGS(IID serial, FIRST_NAME varchar(7) NOT NULL);

0 commit comments

Comments
 (0)