Results 1 to 3 of 3

Thread: Get Session is closed when reload the page!

  1. #1
    Join Date
    Aug 2008
    Posts
    28

    Default Get Session is closed when reload the page!

    Hi,

    I'm testing Spring Roo and its features, and it's simply amazing...

    But I have an strange problem with Hibernate Session

    I have experience with Hibernate, and I'm aware of OpenSessionInViewFilter matters, it works properly for me in a wide range of projects.

    But, now, I'm experiencing an strange behaviour...

    I'm recovering a list of entities, each of them have another associated (ManyToOne)

    (...)
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="codigomat")
    private MateriaPrima materiaPrima;
    (...)

    I retrieve the list, with the associated ones $(entity.materiaPrima.attr), and everything works properly... but, when I reload the page (via reload button in the web browser) an Internal Error is thrown:

    Session is closed!


    Is there anything wrong in Roo OpenSessionInView configuration?
    Is there anything wrong in my Mapping? (I need to do it by hand)

    The usecase works fine "the first time" when I load the page, with reloadings this error comes up.

    Thanks in advance

  2. #2
    Join Date
    Aug 2008
    Posts
    28

    Default Getting Session is closed when reloading the page - More information

    I just figured out what can be happening... but I don't know why...


    With an existing Roo environment, I would like to make some customized queries, so I implemented a "DAO" via @PersistenceContext injection the "clasical" way:

    Code:
    @Repository
    public class LoteMateriaPrimaDaoImpl implements LoteMateriaPrimaDao {
    
    	@PersistenceContext
    	private EntityManager entityManager;
    	
    	private Session hibernateSession = null;
    	
    	public LoteMateriaPrimaDaoImpl() {
    		
    	}
    	
    	public Session getSession() {
    		
    		if(hibernateSession==null){
    			
    			hibernateSession =  ((Session)entityManager.getDelegate());
    		}
    		
    		return hibernateSession;
    	}
    
    	@SuppressWarnings("unchecked")
    	public List<LoteMateriaPrima> obtenerLotes(Boolean abiertos) {
    		
    		List<LoteMateriaPrima> l = null;
    	
    		// lotes sin inicializar, abiertos y cerrados
    		if(abiertos==null) {
    			
    			l = (List<LoteMateriaPrima>) getSession().createQuery("from LoteMateriaPrima").list();
    			
    		} else {
    		
    			if(abiertos) {
    				
    				l = (List<LoteMateriaPrima>) getSession().createQuery("from LoteMateriaPrima where fechaApertura is not null and fechaCierre is null").list();
    				
    				
    			} else {
    				
    				l = (List<LoteMateriaPrima>) getSession().createQuery("from LoteMateriaPrima where fechaApertura is null and fechaCierre is not null").list();
    				
    			}
    		
    			
    		}
    		
    			
    		return l;
    		
    	}
    With the following service...

    Code:
    @Service
    @Transactional
    public class GestionLotesServiceImpl implements GestionLotesService {
    
    	@Autowired
    	private LoteMateriaPrimaDao loteMateriaPrimaDao;
    
    	
    	
    	public List<LoteMateriaPrima> obtenerLotesMateriaPrima(Boolean abiertos) {
    		
    		
    		return loteMateriaPrimaDao.obtenerLotes(null);
    	}
    
    (...)
    And finally injected in a controller...

    Code:
    @Controller
    public class GestionLotesController {
    
    	@Autowired
    	private GestionLotesService gestionLotesService;
    	
    	
    
    	@RequestMapping(value="/gestionarLotesMateriaPrima",method=RequestMethod.GET)
    	public void listadoLotes(ModelMap model) {
    		
    	List<LoteMateriaPrima> lotes = gestionLotesService.obtenerLotesMateriaPrima(null);
    
    		model.addAttribute("lotesMateriaPrima",lotes);
    	
    	}

    there may be another Session or SessionFactory hanging around??

    Is it necessary the @Repository/@Service in a class which accesses to PersistenceContext?


    Thanks in advance

  3. #3
    Join Date
    Oct 2011
    Location
    Mumbai, India
    Posts
    2

    Default

    Try either, based on your need

    @Transactional
    @Transactional(readOnly=true)
    @Transactional(readOnly=true, timeout=3000)

    Hope it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •