1

I am getting the following error message when trying to refer to the current session in an application using spring and hibernate 4:

SEVERE: Servlet.service() for servlet [spring] in context with path [/DocumentManager] threw exception [Request processing failed;  
nested exception is org.hibernate.HibernateException:  
No Session found for current thread] with root cause org.hibernate.HibernateException:  
No Session found for current thread

The error message is triggered by sessionFactory.getCurrentSession() in the following line of code in a controller class:

Blob blob = Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(file.getInputStream(), file.getSize());  

Here is the block of code in which it resides:

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("document") Document document, @RequestParam("file") MultipartFile file) {
    try {
        Blob blob = Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(file.getInputStream(), file.getSize());
        document.setFileName(file.getOriginalFilename());
        document.setContent(blob);
        document.setContentType(file.getContentType());
    } catch (IOException e) {e.printStackTrace();}
    try {documentDao.save(document);} 
    catch(Exception e) {e.printStackTrace();}
    return "redirect:/index.html";
}

I tried axtavt's answer in this posting, but eclipse started forcing a lot of changes that would propagate to other parts of the application, such as making doInTransactionWithoutResult() protected and thus forcing my document variable to become final in scope.

My application does have a transactionManager bean as follows:

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>  

But how do I modify the code (above) inside my controller class so that it can getCurrentSession() without throwing the error?

Community
  • 1
  • 1
CodeMed
  • 9,527
  • 70
  • 212
  • 364

1 Answers1

1

add this <tx:annotation-driven transaction-manager="transactionManager" /> to your context.

see this links it might help you.

Spring Hibernate transaction management

How to integrate spring with hibernate session and transaction management?

Automatic Hibernate Transaction Management with Spring?

edit

here the save method in DocumentDaoImpl only Transactional.

class DocumentDaoImpl
{
     @Transactional   
     public void save(document){

     }
     public void someMethod(){

     }
}

or

here all the methods in DocumentDaoImpl are Transactional.

@Transactional   
class DocumentDaoImpl
{
     public void save(document){

     }
     public void someMethod(){

     }
}

Note:

Move these type of hibernate related code to DocumentDaoImpl

    Blob blob = Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(file.getInputStream(), file.getSize());
Community
  • 1
  • 1
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • Thank you. +1 for helping me find an application-level approach. I added the line you suggested. But where do I put at_Transactional in my code? In one of the links you sent, at_Transactional was in front of the class, but then only in front of one of the methods in the class and not in front of the other method. Where would at_Transactional go in my code above? What would it look like? And why? – CodeMed Nov 22 '13 at 01:28
  • Thank you. But either I am not understanding something, or you did not really answer my question. When I put at_Transactional in front of the save method in my code above (in the controller class), it triggers a runtime error in my dao class. The app only has 3 classes, but I hesitate to put up a bunch of code. Can you tell me what code you need me to post in order to address this problem? – CodeMed Nov 22 '13 at 01:54
  • Thank you. I added the current exception as an edit to my original posting above. – CodeMed Nov 22 '13 at 02:03
  • @CodeMed This exception caused by the blob content. Sorry I am not sure how to solve it. – Prabhakaran Ramaswamy Nov 22 '13 at 02:21
  • Thank you. I guess I will just mark this as the answer and move on to another question. I appreciate your help. – CodeMed Nov 22 '13 at 02:28