Hibernate + JSF2 Session Is Closed

240 views Asked by At

I am getting session closed exception in my JSF + Hibernate Application when adding a new country. Here is the code for your reference

country.xhtml

<f:metadata>
    <f:event listener="#{country.getPreRequisites}" type="preRenderView"/>
</f:metadata>

Above code populates the countries Map to be used below. If I remove f:metadata block then i can add country and won't get session is closed message.

<p:selectOneRadio value="#{country.countryId}" id="countryId" columns="4" style="width: 700px" layout="grid">
      <f:selectItems value="#{country.countries}" />
</p:selectOneRadio>

CountryManagedBean.java

@ManagedBean(name = "country")
@ViewScoped
public class CountryManagedBean
{
   public String saveCountry()
   {
      String actionFlag="country";
      FacesMessage message=null;
      try
      {
        int flag=countryDao.saveCountry(this);
        if(flag==1)
        {
            message=new FacesMessage(FacesMessage.SEVERITY_INFO,"Country","Saved Successfully");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
    catch(Exception e)
    {
        LOG.logp(Level.SEVERE, "CountryManagedBean","saveCountry", "Saving Country Failed", e);
    }
    return actionFlag;
  }

   public void getPreRequisites(ComponentSystemEvent event)
  {
      FacesContext fc = FacesContext.getCurrentInstance();
     if (!fc.isPostback())    //Page Is Called / Rendered
     {
         countryDao.getPreRequisites(this); 
     }
  }
}

CountryDao.java

public class CountryDao
{

   Session session = null;
   Transaction tx=null;

   public void getPreRequisites(CountryManagedBean country)
   {
       try
      {
          tx=session.beginTransaction();
         Criteria cr=session.createCriteria(CountryPojo.class)
        .setProjection(Projections.projectionList()
            .add(Projections.property("countryId"),"countryId")
            .add(Projections.property("countryName"),"countryName")
            .add(Projections.property("countryShortCode"),"countryShortCode")
            )
        .setResultTransformer(Transformers.aliasToBean(CountryPojo.class));
        List<CountryPojo> countryList=cr.list();
        System.out.println("Total Countries Found:"+countryList.size());
        for(CountryPojo pojo:countryList)
        {
            country.getCountries().put(pojo.getCountryName()+" ["+pojo.getCountryShortCode()+"]",pojo.getCountryId());
        }
        CountryPojo p=countryList.get(0);
        System.out.println("%%%%%%%%%%% Country Id:"+p.getCountryId()+"\t############# Country Name:"+p.getCountryName());
        tx.commit();
    }
    catch(HibernateException e)
    {
        LOG.logp(Level.SEVERE, "Country Dao", "getPreRequisites","Caught HibernateException while getting pre requisites",e);
        try
        {
            tx.rollback();
        }
        catch(HibernateException ex)
        {
            LOG.logp(Level.SEVERE,"CountryDao","getPreRequisites","Caught HibernateException while rolling back transaction",ex);
        }
    }
    finally
    {

    }
}
public int saveCountry(CountryManagedBean country)
{
    int flag=-1;
    try
    {
        tx=session.beginTransaction();
        CountryPojo countryPojo=new CountryPojo(country.getCountryName(),country.getCountryShortCode(),country.getLastUpdateBy());
        country.setCountryId((Integer)session.save(countryPojo));
        LOG.log(Level.INFO, "Country {0} Saved With Id {1}", new Object[]{country.getCountryName(), country.getCountryId()});
        tx.commit();
    }
    catch(HibernateException e)
    {
        LOG.logp(Level.SEVERE, "Country Dao", "saveCountry","Caught HibernateException while saving country",e);
        try
        {
            tx.rollback();
        }
        catch(HibernateException ex)
        {
            LOG.logp(Level.SEVERE,"CountryDao","saveCountry","Caught HibernateException while rolling back transaction",ex);
        }
    }
    finally
    {

    }
    return flag;
}
public CountryDao()
{
    session = HibernateUtil.getSessionFactory().getCurrentSession();
}
private static final Logger LOG = Logger.getLogger(CountryDao.class.getName());
}
1

There are 1 answers

1
SergeyB On BEST ANSWER

Most likely you are seeing this because your JPA/Hibernate session is closed by the time your JSF calls getPreRequisites. You'll need to add this to your web.xml to keep the session open so that the view can load your lazy loaded objects.

<filter>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>