Skip to content

Commit

Permalink
debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
layzimayner committed Oct 28, 2024
1 parent c6a2f2b commit a0bf2f3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Binary file modified mem.lck
Binary file not shown.
20 changes: 13 additions & 7 deletions src/main/java/mate/academy/dao/MovieDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Movie> get(Long id) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
try (Session session = sessionFactory.openSession()) {
return Optional.ofNullable(session.get(Movie.class, id));
} catch (Exception e) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/mate/academy/service/MovieServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit a0bf2f3

Please sign in to comment.