|
9 | 9 | import com.iluwatar.cqrs.util.HibernateUtil; |
10 | 10 |
|
11 | 11 | /** |
12 | | - * This class is implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence. |
| 12 | + * This class is an implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence. |
13 | 13 | * |
14 | 14 | */ |
15 | 15 | public class CommandServiceImpl implements ICommandService { |
16 | 16 |
|
17 | 17 | private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); |
18 | 18 |
|
19 | 19 | private Author getAuthorByUsername(String username) { |
20 | | - Session session = sessionFactory.openSession(); |
21 | | - Query query = session.createQuery("from Author where username=:username"); |
22 | | - query.setParameter("username", username); |
23 | | - Author author = (Author) query.uniqueResult(); |
24 | | - session.close(); |
| 20 | + Author author = null; |
| 21 | + try (Session session = sessionFactory.openSession()) { |
| 22 | + Query query = session.createQuery("from Author where username=:username"); |
| 23 | + query.setParameter("username", username); |
| 24 | + author = (Author) query.uniqueResult(); |
| 25 | + } |
25 | 26 | return author; |
26 | 27 | } |
27 | 28 |
|
28 | 29 | private Book getBookByTitle(String title) { |
29 | | - Session session = sessionFactory.openSession(); |
30 | | - Query query = session.createQuery("from Book where title=:title"); |
31 | | - query.setParameter("title", title); |
32 | | - Book book = (Book) query.uniqueResult(); |
33 | | - session.close(); |
| 30 | + Book book = null; |
| 31 | + try (Session session = sessionFactory.openSession()) { |
| 32 | + Query query = session.createQuery("from Book where title=:title"); |
| 33 | + query.setParameter("title", title); |
| 34 | + book = (Book) query.uniqueResult(); |
| 35 | + } |
34 | 36 | return book; |
35 | 37 | } |
36 | 38 |
|
37 | 39 | @Override |
38 | 40 | public void authorCreated(String username, String name, String email) { |
39 | 41 | Author author = new Author(username, name, email); |
40 | | - Session session = sessionFactory.openSession(); |
41 | | - session.beginTransaction(); |
42 | | - session.save(author); |
43 | | - session.getTransaction().commit(); |
44 | | - session.close(); |
| 42 | + try (Session session = sessionFactory.openSession()) { |
| 43 | + session.beginTransaction(); |
| 44 | + session.save(author); |
| 45 | + session.getTransaction().commit(); |
| 46 | + } |
45 | 47 | } |
46 | 48 |
|
47 | 49 | @Override |
48 | 50 | public void bookAddedToAuthor(String title, double price, String username) { |
49 | 51 | Author author = getAuthorByUsername(username); |
50 | 52 | Book book = new Book(title, price, author); |
51 | | - Session session = sessionFactory.openSession(); |
52 | | - session.beginTransaction(); |
53 | | - session.save(book); |
54 | | - session.getTransaction().commit(); |
55 | | - session.close(); |
| 53 | + try (Session session = sessionFactory.openSession()) { |
| 54 | + session.beginTransaction(); |
| 55 | + session.save(book); |
| 56 | + session.getTransaction().commit(); |
| 57 | + } |
56 | 58 | } |
57 | 59 |
|
58 | 60 | @Override |
59 | 61 | public void authorNameUpdated(String username, String name) { |
60 | 62 | Author author = getAuthorByUsername(username); |
61 | 63 | author.setName(name); |
62 | | - Session session = sessionFactory.openSession(); |
63 | | - session.beginTransaction(); |
64 | | - session.update(author); |
65 | | - session.getTransaction().commit(); |
66 | | - session.close(); |
| 64 | + try (Session session = sessionFactory.openSession()) { |
| 65 | + session.beginTransaction(); |
| 66 | + session.update(author); |
| 67 | + session.getTransaction().commit(); |
| 68 | + } |
67 | 69 | } |
68 | 70 |
|
69 | 71 | @Override |
70 | 72 | public void authorUsernameUpdated(String oldUsername, String newUsername) { |
71 | 73 | Author author = getAuthorByUsername(oldUsername); |
72 | 74 | author.setUsername(newUsername); |
73 | | - Session session = sessionFactory.openSession(); |
74 | | - session.beginTransaction(); |
75 | | - session.update(author); |
76 | | - session.getTransaction().commit(); |
77 | | - session.close(); |
78 | | - |
| 75 | + try (Session session = sessionFactory.openSession()) { |
| 76 | + session.beginTransaction(); |
| 77 | + session.update(author); |
| 78 | + session.getTransaction().commit(); |
| 79 | + } |
79 | 80 | } |
80 | 81 |
|
81 | 82 | @Override |
82 | 83 | public void authorEmailUpdated(String username, String email) { |
83 | 84 | Author author = getAuthorByUsername(username); |
84 | 85 | author.setEmail(email); |
85 | | - Session session = sessionFactory.openSession(); |
86 | | - session.beginTransaction(); |
87 | | - session.update(author); |
88 | | - session.getTransaction().commit(); |
89 | | - session.close(); |
90 | | - |
| 86 | + try (Session session = sessionFactory.openSession()) { |
| 87 | + session.beginTransaction(); |
| 88 | + session.update(author); |
| 89 | + session.getTransaction().commit(); |
| 90 | + } |
91 | 91 | } |
92 | 92 |
|
93 | 93 | @Override |
94 | 94 | public void bookTitleUpdated(String oldTitle, String newTitle) { |
95 | 95 | Book book = getBookByTitle(oldTitle); |
96 | 96 | book.setTitle(newTitle); |
97 | | - Session session = sessionFactory.openSession(); |
98 | | - session.beginTransaction(); |
99 | | - session.update(book); |
100 | | - session.getTransaction().commit(); |
101 | | - session.close(); |
| 97 | + try (Session session = sessionFactory.openSession()) { |
| 98 | + session.beginTransaction(); |
| 99 | + session.update(book); |
| 100 | + session.getTransaction().commit(); |
| 101 | + } |
102 | 102 | } |
103 | 103 |
|
104 | 104 | @Override |
105 | 105 | public void bookPriceUpdated(String title, double price) { |
106 | 106 | Book book = getBookByTitle(title); |
107 | 107 | book.setPrice(price); |
108 | | - Session session = sessionFactory.openSession(); |
109 | | - session.beginTransaction(); |
110 | | - session.update(book); |
111 | | - session.getTransaction().commit(); |
112 | | - session.close(); |
| 108 | + try (Session session = sessionFactory.openSession()) { |
| 109 | + session.beginTransaction(); |
| 110 | + session.update(book); |
| 111 | + session.getTransaction().commit(); |
| 112 | + } |
113 | 113 | } |
114 | 114 |
|
115 | 115 | } |
0 commit comments