|
1 |
| -package com.jpa; |
2 |
| - |
3 |
| -import com.vladmihalcea.concurrent.Retry; |
4 |
| -import java.io.Serializable; |
5 |
| -import java.util.logging.Level; |
6 |
| -import java.util.logging.Logger; |
7 |
| -import javax.persistence.EntityManager; |
8 |
| -import javax.persistence.EntityManagerFactory; |
9 |
| -import javax.persistence.EntityTransaction; |
10 |
| -import javax.persistence.OptimisticLockException; |
11 |
| -import org.springframework.beans.factory.annotation.Autowired; |
12 |
| -import org.springframework.context.ApplicationContext; |
13 |
| -import org.springframework.context.annotation.Scope; |
14 |
| -import org.springframework.stereotype.Repository; |
15 |
| - |
16 |
| -@Repository |
17 |
| -@Scope("prototype") |
18 |
| -public class InventoryRepository implements Serializable, Runnable { |
19 |
| - |
20 |
| - private final int quantity; |
21 |
| - private final int delay; |
22 |
| - |
23 |
| - @Autowired |
24 |
| - private ApplicationContext applicationContext; |
25 |
| - |
26 |
| - private static final Logger logger = Logger.getLogger(InventoryRepository.class.getName()); |
27 |
| - |
28 |
| - public InventoryRepository(int quantity, int delay) { |
29 |
| - this.quantity = quantity; |
30 |
| - this.delay = delay; |
31 |
| - } |
32 |
| - |
33 |
| - @Override |
34 |
| - @Retry(times = 10, on = OptimisticLockException.class) |
35 |
| - public void run() { |
36 |
| - |
37 |
| - EntityManagerFactory entityManagerFactory |
38 |
| - = applicationContext.getBean(EntityManagerFactory.class); |
39 |
| - |
40 |
| - EntityManager entityManager = entityManagerFactory.createEntityManager(); |
41 |
| - |
42 |
| - EntityTransaction entityTransaction = entityManager.getTransaction(); |
43 |
| - |
44 |
| - try { |
45 |
| - entityTransaction.begin(); |
46 |
| - |
47 |
| - Inventory inventory = entityManager.find(Inventory.class, 1L); |
48 |
| - |
49 |
| - if ((inventory.getQuantity() - quantity) >= 0) { |
50 |
| - inventory.setQuantity(inventory.getQuantity() - quantity); |
51 |
| - } |
52 |
| - |
53 |
| - try { |
54 |
| - Thread.sleep(delay); |
55 |
| - } catch (InterruptedException ex) { |
56 |
| - Thread.currentThread().interrupt(); |
57 |
| - } |
58 |
| - |
59 |
| - entityTransaction.commit(); |
60 |
| - |
61 |
| - logger.info("Transaction committed ..."); |
62 |
| - } catch (RuntimeException e) { |
63 |
| - |
64 |
| - if (is(e, OptimisticLockException.class)) { |
65 |
| - logger.log(Level.INFO, "OptimisticLockException has occured ..."); |
66 |
| - logger.log(Level.INFO, "Retry mechanism enter into the scene ..."); |
67 |
| - } |
68 |
| - |
69 |
| - if (entityTransaction != null && entityTransaction.isActive()) { |
70 |
| - entityTransaction.rollback(); |
71 |
| - } |
72 |
| - |
73 |
| - throw (e); |
74 |
| - } |
75 |
| - } |
76 |
| - |
77 |
| - public static <T extends Throwable> boolean is(Throwable exception, Class<T> type) { |
78 |
| - Throwable unwrappedException = exception; |
79 |
| - |
80 |
| - while (unwrappedException != null) { |
81 |
| - if (type.isInstance(unwrappedException)) { |
82 |
| - return true; |
83 |
| - } |
84 |
| - |
85 |
| - unwrappedException = unwrappedException.getCause(); |
86 |
| - } |
87 |
| - |
88 |
| - return false; |
89 |
| - } |
90 |
| -} |
| 1 | +package com.bookstore.repository; |
| 2 | + |
| 3 | +import com.bookstore.entity.Inventory; |
| 4 | +import com.vladmihalcea.concurrent.Retry; |
| 5 | +import java.io.Serializable; |
| 6 | +import java.util.logging.Level; |
| 7 | +import java.util.logging.Logger; |
| 8 | +import javax.persistence.EntityManager; |
| 9 | +import javax.persistence.EntityManagerFactory; |
| 10 | +import javax.persistence.EntityTransaction; |
| 11 | +import javax.persistence.OptimisticLockException; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.context.ApplicationContext; |
| 14 | +import org.springframework.context.annotation.Scope; |
| 15 | +import org.springframework.stereotype.Repository; |
| 16 | + |
| 17 | +@Repository |
| 18 | +@Scope("prototype") |
| 19 | +public class InventoryRepository implements Serializable, Runnable { |
| 20 | + |
| 21 | + private final int quantity; |
| 22 | + private final int delay; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private ApplicationContext applicationContext; |
| 26 | + |
| 27 | + private static final Logger logger = Logger.getLogger(InventoryRepository.class.getName()); |
| 28 | + |
| 29 | + public InventoryRepository(int quantity, int delay) { |
| 30 | + this.quantity = quantity; |
| 31 | + this.delay = delay; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + @Retry(times = 10, on = OptimisticLockException.class) |
| 36 | + public void run() { |
| 37 | + |
| 38 | + EntityManagerFactory entityManagerFactory |
| 39 | + = applicationContext.getBean(EntityManagerFactory.class); |
| 40 | + |
| 41 | + EntityManager entityManager = entityManagerFactory.createEntityManager(); |
| 42 | + |
| 43 | + EntityTransaction entityTransaction = entityManager.getTransaction(); |
| 44 | + |
| 45 | + try { |
| 46 | + entityTransaction.begin(); |
| 47 | + |
| 48 | + Inventory inventory = entityManager.find(Inventory.class, 1L); |
| 49 | + |
| 50 | + if ((inventory.getQuantity() - quantity) >= 0) { |
| 51 | + inventory.setQuantity(inventory.getQuantity() - quantity); |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + Thread.sleep(delay); |
| 56 | + } catch (InterruptedException ex) { |
| 57 | + Thread.currentThread().interrupt(); |
| 58 | + } |
| 59 | + |
| 60 | + entityTransaction.commit(); |
| 61 | + |
| 62 | + logger.info("Transaction committed ..."); |
| 63 | + } catch (RuntimeException e) { |
| 64 | + |
| 65 | + if (is(e, OptimisticLockException.class)) { |
| 66 | + logger.log(Level.INFO, "OptimisticLockException has occured ..."); |
| 67 | + logger.log(Level.INFO, "Retry mechanism enter into the scene ..."); |
| 68 | + } |
| 69 | + |
| 70 | + if (entityTransaction != null && entityTransaction.isActive()) { |
| 71 | + entityTransaction.rollback(); |
| 72 | + } |
| 73 | + |
| 74 | + throw (e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static <T extends Throwable> boolean is(Throwable exception, Class<T> type) { |
| 79 | + Throwable unwrappedException = exception; |
| 80 | + |
| 81 | + while (unwrappedException != null) { |
| 82 | + if (type.isInstance(unwrappedException)) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + unwrappedException = unwrappedException.getCause(); |
| 87 | + } |
| 88 | + |
| 89 | + return false; |
| 90 | + } |
| 91 | +} |
0 commit comments