Close sessions using try with resources

This commit is contained in:
Sabiq Ihab 2017-07-29 12:13:34 +01:00
parent 8073b93182
commit 8bf4497879
2 changed files with 82 additions and 77 deletions

View File

@ -9,7 +9,7 @@ import com.iluwatar.cqrs.domain.model.Book;
import com.iluwatar.cqrs.util.HibernateUtil; import com.iluwatar.cqrs.util.HibernateUtil;
/** /**
* This class is implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence. * This class is an implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence.
* *
*/ */
public class CommandServiceImpl implements ICommandService { public class CommandServiceImpl implements ICommandService {
@ -17,99 +17,99 @@ public class CommandServiceImpl implements ICommandService {
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
private Author getAuthorByUsername(String username) { private Author getAuthorByUsername(String username) {
Session session = sessionFactory.openSession(); Author author = null;
try (Session session = sessionFactory.openSession()) {
Query query = session.createQuery("from Author where username=:username"); Query query = session.createQuery("from Author where username=:username");
query.setParameter("username", username); query.setParameter("username", username);
Author author = (Author) query.uniqueResult(); author = (Author) query.uniqueResult();
session.close(); }
return author; return author;
} }
private Book getBookByTitle(String title) { private Book getBookByTitle(String title) {
Session session = sessionFactory.openSession(); Book book = null;
try (Session session = sessionFactory.openSession()) {
Query query = session.createQuery("from Book where title=:title"); Query query = session.createQuery("from Book where title=:title");
query.setParameter("title", title); query.setParameter("title", title);
Book book = (Book) query.uniqueResult(); book = (Book) query.uniqueResult();
session.close(); }
return book; return book;
} }
@Override @Override
public void authorCreated(String username, String name, String email) { public void authorCreated(String username, String name, String email) {
Author author = new Author(username, name, email); Author author = new Author(username, name, email);
Session session = sessionFactory.openSession(); try (Session session = sessionFactory.openSession()) {
session.beginTransaction(); session.beginTransaction();
session.save(author); session.save(author);
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); }
} }
@Override @Override
public void bookAddedToAuthor(String title, double price, String username) { public void bookAddedToAuthor(String title, double price, String username) {
Author author = getAuthorByUsername(username); Author author = getAuthorByUsername(username);
Book book = new Book(title, price, author); Book book = new Book(title, price, author);
Session session = sessionFactory.openSession(); try (Session session = sessionFactory.openSession()) {
session.beginTransaction(); session.beginTransaction();
session.save(book); session.save(book);
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); }
} }
@Override @Override
public void authorNameUpdated(String username, String name) { public void authorNameUpdated(String username, String name) {
Author author = getAuthorByUsername(username); Author author = getAuthorByUsername(username);
author.setName(name); author.setName(name);
Session session = sessionFactory.openSession(); try (Session session = sessionFactory.openSession()) {
session.beginTransaction(); session.beginTransaction();
session.update(author); session.update(author);
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); }
} }
@Override @Override
public void authorUsernameUpdated(String oldUsername, String newUsername) { public void authorUsernameUpdated(String oldUsername, String newUsername) {
Author author = getAuthorByUsername(oldUsername); Author author = getAuthorByUsername(oldUsername);
author.setUsername(newUsername); author.setUsername(newUsername);
Session session = sessionFactory.openSession(); try (Session session = sessionFactory.openSession()) {
session.beginTransaction(); session.beginTransaction();
session.update(author); session.update(author);
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); }
} }
@Override @Override
public void authorEmailUpdated(String username, String email) { public void authorEmailUpdated(String username, String email) {
Author author = getAuthorByUsername(username); Author author = getAuthorByUsername(username);
author.setEmail(email); author.setEmail(email);
Session session = sessionFactory.openSession(); try (Session session = sessionFactory.openSession()) {
session.beginTransaction(); session.beginTransaction();
session.update(author); session.update(author);
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); }
} }
@Override @Override
public void bookTitleUpdated(String oldTitle, String newTitle) { public void bookTitleUpdated(String oldTitle, String newTitle) {
Book book = getBookByTitle(oldTitle); Book book = getBookByTitle(oldTitle);
book.setTitle(newTitle); book.setTitle(newTitle);
Session session = sessionFactory.openSession(); try (Session session = sessionFactory.openSession()) {
session.beginTransaction(); session.beginTransaction();
session.update(book); session.update(book);
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); }
} }
@Override @Override
public void bookPriceUpdated(String title, double price) { public void bookPriceUpdated(String title, double price) {
Book book = getBookByTitle(title); Book book = getBookByTitle(title);
book.setPrice(price); book.setPrice(price);
Session session = sessionFactory.openSession(); try (Session session = sessionFactory.openSession()) {
session.beginTransaction(); session.beginTransaction();
session.update(book); session.update(book);
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); }
} }
} }

View File

@ -23,55 +23,60 @@ public class QueryServiceImpl implements IQueryService {
@Override @Override
public Author getAuthorByUsername(String username) { public Author getAuthorByUsername(String username) {
Session session = sessionFactory.openSession(); Author authorDTo = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session SQLQuery sqlQuery = session
.createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\"" .createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\""
+ "FROM Author a where a.username=:username"); + "FROM Author a where a.username=:username");
sqlQuery.setParameter("username", username); sqlQuery.setParameter("username", username);
Author authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult(); authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult();
session.close(); }
return authorDTo; return authorDTo;
} }
@Override @Override
public Book getBook(String title) { public Book getBook(String title) {
Session session = sessionFactory.openSession(); Book bookDTo = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session SQLQuery sqlQuery = session
.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title"); .createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title");
sqlQuery.setParameter("title", title); sqlQuery.setParameter("title", title);
Book bookDTo = (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult(); bookDTo = (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult();
session.close(); }
return bookDTo; return bookDTo;
} }
@Override @Override
public List<Book> getAuthorBooks(String username) { public List<Book> getAuthorBooks(String username) {
Session session = sessionFactory.openSession(); List<Book> bookDTos = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\""
+ " FROM Author a , Book b where b.author_id = a.id and a.username=:username"); + " FROM Author a , Book b where b.author_id = a.id and a.username=:username");
sqlQuery.setParameter("username", username); sqlQuery.setParameter("username", username);
List<Book> bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list(); bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list();
session.close(); }
return bookDTos; return bookDTos;
} }
@Override @Override
public BigInteger getAuthorBooksCount(String username) { public BigInteger getAuthorBooksCount(String username) {
Session session = sessionFactory.openSession(); BigInteger bookcount = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery( SQLQuery sqlQuery = session.createSQLQuery(
"SELECT count(b.title)" + " FROM Book b, Author a where b.author_id = a.id and a.username=:username"); "SELECT count(b.title)" + " FROM Book b, Author a where b.author_id = a.id and a.username=:username");
sqlQuery.setParameter("username", username); sqlQuery.setParameter("username", username);
BigInteger bookcount = (BigInteger) sqlQuery.uniqueResult(); bookcount = (BigInteger) sqlQuery.uniqueResult();
session.close(); }
return bookcount; return bookcount;
} }
@Override @Override
public BigInteger getAuthorsCount() { public BigInteger getAuthorsCount() {
Session session = sessionFactory.openSession(); BigInteger authorcount = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery("SELECT count(id) from Author"); SQLQuery sqlQuery = session.createSQLQuery("SELECT count(id) from Author");
BigInteger authorcount = (BigInteger) sqlQuery.uniqueResult(); authorcount = (BigInteger) sqlQuery.uniqueResult();
session.close(); }
return authorcount; return authorcount;
} }