Announcement Announcement Module
Collapse
No announcement yet.
sessionFactory.getCurrentSession() returns different sessions although OSIV is config Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • sessionFactory.getCurrentSession() returns different sessions although OSIV is config

    Hello all,
    I'm writing Vaadin based application and struggling to get rid of LazyInitializationException . Here are my web.xml and generic DAO

    Code:
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>Hibernate-OSIV pattern</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    
    </filter>
    <filter-mapping>
        <filter-name>Hibernate-OSIV pattern</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    Code:
    public class BasicDaoImpl implements BasicDao {
    
    private Class currentClass;
    
    private SessionFactory sessionFactory;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    @Transactional(readOnly = true)
    @Override
    public T getById(long id) {
        Session session = getCurrentSession();
        return (T) session.get(getGenericClass(), id);
    }
    
    @Transactional(readOnly = true)
    @Override
    public List<T> getAll() {
        Session session = getCurrentSession();
        return session.createCriteria(getGenericClass()).list();
    }
    
    @Transactional(readOnly = false)
    @Override
    public void create(T entity) {
        Session session = getCurrentSession();
        session.save(entity);
    }
    
    @Transactional(readOnly = false)
    @Override
    public void update(T entity) {
        getCurrentSession().update(entity);
    }
    
    @Transactional(readOnly = false)
    @Override
    public void delete(T entity) {
        Session session = getCurrentSession();
        session.delete(entity);
    }
    
    @Override
    @Transactional(readOnly = false)
    public void update(Collection<T> entities) {
        Session session = getCurrentSession();
        for (T entity : entities) {
            session.update(entity);
        }
    }
    
    @Override
    @Transactional(readOnly = false)
    public void create(List<T> entities) {
        Session session = getCurrentSession();
        for (T entity : entities) {
            session.save(entity);
        }
    }
    
    private Class getGenericClass() {
        if (currentClass == null) {
            ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
            currentClass = (Class) type.getActualTypeArguments()[0];
        }
        return currentClass;
    }
    
    protected Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
    }
    I get the applicationContext from servlet like that:
    Code:
    applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
    as you can guess sessionFactory is defined pretty straight forward:
    HTML Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"autowire="no">
    What I can see from the debugging that sessionFactory.getCurrentSession() on each request returns session with a different hashCode. I can't see what I've done wrong here. Would be thankful for any kind of help.
Working...
X