Inherit @Before and @After methods from BaseTest Since for every class, a session is created before, use it for ServiceTest.testUpdate() when we have to use session.save.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

How does this method cause memory leaks? How do I make it multi-thread safe?

I have this JUnit test method that checks if my method is working. What it does is to.

  1. Inherit @Before and @After methods from BaseTest
  2. Since for every class, a session is created before, use it for ServiceTest.testUpdate() when we have to use session.save.

-----------------------------------------------------------------------------------------------------------------------

 

public class BaseTest{ @Autowired protected SessionFactory sessionFactory; protected Session session; @Before public void createSession() { session = sessionFactory.openSession(); } @After public void closeSession(){ if (session != null) { session.flush(); session.close(); } } }

 

public class ServiceTest extends BaseTest{ @Autowired private FoodService foodService; @Test public void testUpdate() { Food food= new Food("Japanese", "Ramen"); session.save(student); int id = (int) session.getIdentifier(student); session.flush(); session.close(); Food updatedFood= new Food("Japanese", "Sushi"); foodService.update(food, updatedFood); session = sessionFactory.openSession(); Food retrievedFood = session.get(Food.class, id); Assert.assertEquals(retrievedFood.getId(), food.getId()); Assert.assertEquals(retrievedFood.getCuisineFood(), retrievedFood.getCuisineFood()); }

 

Additional files if you need:

public class App { public static void main(String[] args) throws Exception { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); try { FoodService foodService = (FoodService )applicationContext.getBean("foodService "); Food food = new Food ("Japanese", "Ramen"); foodService.create(food ); //method not added into the files below //update Food newFood = new Food ("Japanese", "Sushi"); foodService.update(food,newFood); } finally { ((AbstractApplicationContext) applicationContext).close(); } } }

 

@Service("foodService") public class FoodService{ @Autowired private FoodRepository foodRepository; public void update(Food oldFood, Student newFood) throws Exception { foodRepository.update(oldFood, newFood); } }

 

@Repository("foodRepository") public class FoodRepository { @Autowired private SessionFactory sessionFactory; private Session session; public void update(Student oldFood, Student newFood) { session = startSession(); session.saveOrUpdate(oldFood); oldFood.setEmail(newFood.getCuisineFood()); session.update(oldStudent); session.flush(); } private Session startSession() { try { session = sessionFactory.getCurrentSession(); } catch (HibernateException e) { session = sessionFactory.openSession(); } return session; }

How do I make my code more efficient and multi-thread safe? In my ServiceTest, how would using session.flush() cause a session leak? Isn't flush to free up memory resources?

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education