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.
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.
- 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.
-----------------------------------------------------------------------------------------------------------------------
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?
Step by step
Solved in 2 steps