Recently doing a project with a piece of data persistence layer hibernate. After the previous study used no how, has been in use IBATIS. When the integration of spring security encountered "no session, or session was closed", so look for good solutions on the Internet.
Should tend to be more common now is to configure opensessioninview, but this thing feels too much loss of performance, because after all, need to use lazy loading when those data are not many, we can say very little. Just for this and all of the session all the end of the request to keep the page is worth the candle.
Our aim is to configure the delay in loading for optimal performance when needed to query data, not the time not to query. Appears "no session, or session was closed" when the error should be that we need these data, when the data is empty. That hibernate in the session before closing to get the data you are not on it. We will generally be in the business logic written in the service, call the dao in the service method, service method to configure things.
In response to this, we can control the service method which hibernate, let it load the data.
public SecUser getUserWithRole (String userName) (
SecUser user = userDAO.getUser (userName);
if (user! = null) (
user.getRoleSet (). size ();// click here to directly call the size method permission set, so that role hibernate load data.
So that we can simply close the session to avoid the exception, and will not affect the hibernate for session management.
These are some personal thoughts, if there are problems Paizhuan heroes please correct me.