diff --git a/mem.lck b/mem.lck index 44901d18..b86de7a9 100644 Binary files a/mem.lck and b/mem.lck differ diff --git a/src/main/java/mate/academy/dao/MovieDaoImpl.java b/src/main/java/mate/academy/dao/MovieDaoImpl.java index 10c5f8c7..6f379761 100644 --- a/src/main/java/mate/academy/dao/MovieDaoImpl.java +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -11,26 +11,32 @@ @Dao public class MovieDaoImpl implements MovieDao { - private static final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); - @Override public Movie add(Movie movie) { - Session session = sessionFactory.openSession(); - Transaction transaction = session.beginTransaction(); + SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + Session session = null; + Transaction transaction = null; try { + session = sessionFactory.openSession(); + transaction = session.beginTransaction(); session.save(movie); transaction.commit(); return movie; } catch (Exception e) { - transaction.rollback(); - throw new DataProcessingException("Can't save movie " + movie.toString(), e); + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Failed to add new movie to the DB", e); } finally { - session.close(); + if (session != null) { + session.close(); + } } } @Override public Optional get(Long id) { + SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); try (Session session = sessionFactory.openSession()) { return Optional.ofNullable(session.get(Movie.class, id)); } catch (Exception e) { diff --git a/src/main/java/mate/academy/service/MovieServiceImpl.java b/src/main/java/mate/academy/service/MovieServiceImpl.java index 6297f0cb..32961f45 100644 --- a/src/main/java/mate/academy/service/MovieServiceImpl.java +++ b/src/main/java/mate/academy/service/MovieServiceImpl.java @@ -3,6 +3,7 @@ import java.util.Optional; import mate.academy.dao.MovieDao; import mate.academy.exception.DataProcessingException; +import mate.academy.lib.Inject; import mate.academy.lib.Injector; import mate.academy.lib.Service; import mate.academy.model.Movie; @@ -11,7 +12,8 @@ public class MovieServiceImpl implements MovieService { private static final Injector injector = Injector .getInstance("mate.academy"); - private static final MovieDao movieDao = (MovieDao) injector.getInstance(MovieDao.class); + @Inject + private final MovieDao movieDao = (MovieDao) injector.getInstance(MovieDao.class); @Override public Movie add(Movie movie) {