Programming     Travel Logs     Life Is Good     Surfing Online     About Me
There are no get rich quick schemes. That’s just someone else getting rich off you.
-Naval Ravikant
2018-06-16 22:49:18

Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/185

When trying to publish the web application onto the server, I found a bug: multiple access of the page can cause the Tomcat server to block and not responding. After some investigation, I finally found the reason: it opens a session when performing queries to the database each time, but it never close the session.

The fix is very straightforward, so I'll just list all the changes of the code in this article, in stead of explaining too much. Just like before, let's take it easy and enjoy a few beautiful photos first.

/Images/20171024/01.jpg

/Images/20171024/02.jpg

/Images/20171024/03.jpg

/Images/20171024/04.jpg

/Images/20171024/05.jpg

/Images/20171024/06.jpg

/Images/20171024/07.jpg

/Images/20171024/08.jpg

1. Open the file of the class com.casperlee.today.service.DayServiceImpl, and change the code as follows:

package com.casperlee.today.service;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.casperlee.today.domain.Day;
import com.casperlee.today.domain.DayBirth;
import com.casperlee.today.domain.DayDeath;
import com.casperlee.today.domain.DayEvent;
import com.casperlee.today.domain.DayFestival;

@Service
@Transactional
public class DayServiceImpl implements DayService {
	
	@Autowired
	private HibernateTemplate template;
	
	private Session session = null;
	public Session getSession() {
		
		if (session != null) {
			
			session.close();	
		}
		
		session = template.getSessionFactory().openSession();
		return session;
	}

	@Override
	public Day getByKey(String aKey) {

		Query<Day> query = getSession().createQuery("from Day where key = :keyName", Day.class);
		query.setParameter("keyName", aKey);
		return query.uniqueResult();
	}

	@Override
	public DayEvent getEventById(int aId) {
		
		Query<DayEvent> query = getSession().createQuery("from DayEvent where id = :id", DayEvent.class);
		query.setParameter("id", aId);
		return query.uniqueResult();
	}

	@Override
	public DayBirth getBirthById(int aId) {

		Query<DayBirth> query = getSession().createQuery("from DayBirth where id = :id", DayBirth.class);
		query.setParameter("id", aId);
		return query.uniqueResult();
	}

	@Override
	public DayDeath getDeathById(int aId) {

		Query<DayDeath> query = getSession().createQuery("from DayDeath where id = :id", DayDeath.class);
		query.setParameter("id", aId);
		return query.uniqueResult();
	}

	@Override
	public DayFestival getFestivalById(int aId) {

		Query<DayFestival> query = getSession().createQuery("from DayFestival where id = :id", DayFestival.class);
		query.setParameter("id", aId);
		return query.uniqueResult();
	}

	@Override
	public void add(Day aDay) {
		
		template.save(aDay);
	}

	@Override
	public void update(Day aDay) {
		
		template.update(aDay);
	}

	@Override
	public void delete(Day aDay) {
		
		template.delete(aDay);
	}

	@Override
	public void addEvent(DayEvent aDayEvent) {
		
		template.save(aDayEvent);
	}

	@Override
	public void updateEvent(DayEvent aDayEvent) {
		
		template.update(aDayEvent);
	}

	@Override
	public void deleteEvent(DayEvent aDayEvent) {
		
		template.delete(aDayEvent);
	}

	@Override
	public void addBirth(DayBirth aDayBirth) {
		
		template.save(aDayBirth);
	}

	@Override
	public void updateBirth(DayBirth aDayBirth) {
		
		template.update(aDayBirth);
	}

	@Override
	public void deleteBirth(DayBirth aDayBirth) {
		
		template.delete(aDayBirth);		
	}

	@Override
	public void addDeath(DayDeath aDayDeath) {
		
		template.save(aDayDeath);
	}

	@Override
	public void updateDeath(DayDeath aDayDeath) {
		
		template.update(aDayDeath);
	}

	@Override
	public void deleteDeath(DayDeath aDayDeath) {
		
		template.delete(aDayDeath);
	}

	@Override
	public void addFestival(DayFestival aDayFestival) {
		
		template.save(aDayFestival);
	}

	@Override
	public void updateFestival(DayFestival aDayFestival) {
		
		template.update(aDayFestival);
	}

	@Override
	public void deleteFestival(DayFestival aDayFestival) {
		
		template.delete(aDayFestival);
	}
}

2. Done!