No Session found for current thread

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

No Session found for current thread

musicpxg
Hi,

I had an error as follow,

No Session found for current thread; nested exception is org.hibernate.HibernateException: No Session found for current thread

My codes are like follows,

@Service("projectConfig")
@WebService(serviceName = "ProjectConfig", endpointInterface = "com.xx.REST.webservice.ProjectManagement")
@Produces(MediaType.APPLICATION_JSON)
public class ProjectManagementImpl extends GenericManagerImpl<Projects, Long> implements ProjectManagement{
       
        private ProjectsDao projectDao;
       
          @Autowired
            public ProjectManagementImpl(ProjectsDao projectDao){
                super(projectDao);
                this.projectDao = projectDao;
            }
       
        @POST
        @Path("/add")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
        public Response addANewProject(ClientProjects clientproject) {

                               
               
                try {

                        // this line lead to the error
                        Projects project = projectDao.get(1L);

                       
            return Response.status(Response.Status.OK).build();
                       
                } catch(Exception ex) {
                  String message = ex.getMessage();
                  System.out.println(message);
                  return Response.serverError().build();
                }
        }


There is an project with id 1L indeed. I tried to write a case to save a project  using projectDao. There was the same error.

Can anybody help me?

Thank you!

Best wishes,
Yang
Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

musicpxg
Hi, can anybody help me?

I tried to use the class User instead of Projects in that place, there was the same error. So, it demonstrated that the problem did not caused by the class Projects.java.

I tried to use the save function, there was the same error. I wonder, whether it was caused by interceptors or filters or security settings? I did not change any settings in .xml files.

I will really appreciate if anybody can help me.

Best wishes,
Yang
Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

mraible
Administrator
Does the error happen when you call the webservice or in a JUnit test? If it's from the webservice, you may need to enable the LazyLoadingFilter in web.xml.

On Fri, Feb 6, 2015 at 6:38 AM, musicpxg <[hidden email]> wrote:
Hi, can anybody help me?

I tried to use the class User instead of Projects in that place, there was
the same error. So, it demonstrated that the problem did not caused by the
class Projects.java.

I tried to use the save function, there was the same error. I wonder,
whether it was caused by interceptors or filters or security settings? I did
not change any settings in .xml files.

I will really appreciate if anybody can help me.

Best wishes,
Yang



--
View this message in context: http://appfuse.547863.n4.nabble.com/No-Session-found-for-current-thread-tp4657655p4657657.html
Sent from the AppFuse - User mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

musicpxg
Dear Mraible,

Thanks for your kind help! I tried to active the lazyLoadingFilter. It does work!

   <filter>
        <filter-name>lazyLoadingFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>lazyLoadingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


I really appreciate your help! Thank you so much!

Best wishes,
Yang
Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

musicpxg
In reply to this post by mraible
Dear Mraible,

I solved the problem. But another problem comes. I found I cannot save items to database. In other words, the **Dao.save did not actually save item into database. I also tried to update an item. But actually the item was not updated. It is wired that no Errors alarmed.

I think there should be a problem about config. I am call the webservice. Could you give me suggestions?

Thank you!
Best wishes,
Yang
Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

mraible
Administrator
I would try writing a test that hits your class directly, so you can prove its logic works. Once you've proven all that code works, you could try writing a web service integration test. 

This blog post I wrote last year might help you with testing:


Cheers,

Matt

On Tue, Feb 10, 2015 at 7:30 AM, musicpxg <[hidden email]> wrote:
Dear Mraible,

I solved the problem. But another problem comes. I found I cannot save items
to database. In other words, the **Dao.save did not actually save item into
database. I also tried to update an item. But actually the item was not
updated. It is wired that no Errors alarmed.

I think there should be a problem about config. I am call the webservice.
Could you give me suggestions?

Thank you!
Best wishes,
Yang



--
View this message in context: http://appfuse.547863.n4.nabble.com/No-Session-found-for-current-thread-tp4657655p4657664.html
Sent from the AppFuse - User mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

musicpxg
Dear Matt,

Thank you for your fast reply. The class is like following. I had a client to call this service. I did not write a test class to test this function since it is simple.

@Service("projectConfig")
@WebService(serviceName = "ProjectConfig", endpointInterface = "com.project.smart.REST.webservice.ProjectManagement")
@Produces(MediaType.APPLICATION_JSON)
public class ProjectManagementImpl extends GenericManagerImpl<User, Long> implements ProjectManagement{
       
        private UserDao userDao;
        @Autowired
        private ProjectsDao projectDao;
       
          @Autowired
            public ProjectManagementImpl(UserDao userDao){
                super(userDao);
                this.userDao = userDao;
            }
       
        @POST
        @Path("/add")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
        public Response addANewProject(ClientProjects clientproject) {

                try {

                        Projects serverproject = new Projects();
                        serverproject.setProjectCode(clientproject.getProjectCode());
                        serverproject.setUserId(clientproject.getUserId());
                        serverproject.setAddress(clientproject.getAddress());
                        serverproject.setCity(clientproject.getCity());
                        serverproject.setCountry(clientproject.getCountry());
                        serverproject.setPostalCode(clientproject.getPostalCode());
                        serverproject.setProvince(clientproject.getProvince());
                        serverproject.setEnergancyUser(clientproject.getEnergancyUser());

                        serverproject.setId(2L);
                       
                        // save the item 'project' here
                        projectDao.save(serverproject);
                       
                        // Error is alarmed here
                        Projects trx = projectDao.get(2L);

                                               
            return Response.status(Response.Status.OK).build();
                       
                } catch(Exception ex) {
                  String message = ex.getMessage();
                  System.out.println(message);
                  return Response.serverError().build();
                }
        }
}

The Error is like this,

WARN [qtp18801099-173] ProjectsDaoHibernate.get(145) | Uh oh, 'class com.project.smart.appfuse.model.Projects' object with id '2' not found...
Object of class [com.project.smart.appfuse.model.Projects] with identifier [2]: not found

I think there should a configure problem. But I do not know how to handle it.

Best wishes,
Yang
Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

mraible
Administrator
Try projectDao.flush() between your .save() and .get(). The Hibernate session is still open still you're in the same transaction and the details haven't been flushed to the database. You could also just return the object w/o calling get since the save returns a Project. Also, it doesn't look like you're doing anything with the "trx" project, so there's no need to fetch it.

On Tue, Feb 10, 2015 at 8:18 AM, musicpxg <[hidden email]> wrote:
Dear Matt,

Thank you for your fast reply. The class is like following. I had a client
to call this service. I did not write a test class to test this function
since it is simple.

@Service("projectConfig")
@WebService(serviceName = "ProjectConfig", endpointInterface =
"com.project.smart.REST.webservice.ProjectManagement")
@Produces(MediaType.APPLICATION_JSON)
public class ProjectManagementImpl extends GenericManagerImpl<User, Long>
implements ProjectManagement{

        private UserDao userDao;
        @Autowired
        private ProjectsDao projectDao;

          @Autowired
            public ProjectManagementImpl(UserDao userDao){
                super(userDao);
                this.userDao = userDao;
            }

        @POST
        @Path("/add")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
        public Response addANewProject(ClientProjects clientproject) {

                try {

                        Projects serverproject = new Projects();
                        serverproject.setProjectCode(clientproject.getProjectCode());
                        serverproject.setUserId(clientproject.getUserId());
                        serverproject.setAddress(clientproject.getAddress());
                        serverproject.setCity(clientproject.getCity());
                        serverproject.setCountry(clientproject.getCountry());
                        serverproject.setPostalCode(clientproject.getPostalCode());
                        serverproject.setProvince(clientproject.getProvince());
                        serverproject.setEnergancyUser(clientproject.getEnergancyUser());

                        serverproject.setId(2L);

                        *// save the item 'project' here*
                        projectDao.save(serverproject);

                       * // Error is alarmed here*
                        Projects trx = projectDao.get(2L);


                return Response.status(Response.Status.OK).build();

                } catch(Exception ex) {
                  String message = ex.getMessage();
                  System.out.println(message);
                  return Response.serverError().build();
                }
        }
}

The Error is like this,

WARN [qtp18801099-173] ProjectsDaoHibernate.get(145) | Uh oh, 'class
com.project.smart.appfuse.model.Projects' object with id '2' not found...
Object of class [com.project.smart.appfuse.model.Projects] with identifier
[2]: not found

I think there should a configure problem. But I do not know how to handle
it.

Best wishes,
Yang



--
View this message in context: http://appfuse.547863.n4.nabble.com/No-Session-found-for-current-thread-tp4657655p4657673.html
Sent from the AppFuse - User mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

musicpxg
Dear Matt,

I found the problem related to the GenericDaoHibernate.java. The save() function is as follows,

    @SuppressWarnings("unchecked")
    public T save(T object) {
        Session sess = getSession();
        return (T) sess.merge(object);
    }

If I change it into code like follows, the save() will really save an item to database.

    @SuppressWarnings("unchecked")
    public T save(T object) {
        Session sess = getSession();
       
        getSession().save(object);
        getSession().flush();
           
        return (T) object;
    }

And, I find that in the UserDaoHibernate.java, the save() is actually overrided. The save() processes the following codes,

    public User saveUser(User user) {
        if (log.isDebugEnabled()) {
            log.debug("user's id: " + user.getId());
        }
        getSession().saveOrUpdate(user);
        // necessary to throw a DataIntegrityViolation and catch it in UserManager
        getSession().flush();
        return user;
    }

So, my questions are as follows,
1. why the GenericDaoHibernate.java is designed to do not have flush()
2. If I want to use GenericDaoHibernate directly, should I revise it to have flush() in save(), delete(), etc?
3. whether it is necessary to call close() after flush();

I am definitely new to hibernate. I make the descriptions clear to be helpful to other beginners.

 Thank you very much!

Best wishes,
Yang
Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

mraible
Administrator

> On Feb 12, 2015, at 2:23 AM, musicpxg <[hidden email]> wrote:
>
> Dear Matt,
>
> I found the problem related to the GenericDaoHibernate.java. The save()
> function is as follows,
>
>    @SuppressWarnings("unchecked")
>    public T save(T object) {
>        Session sess = getSession();
>        return (T) sess.merge(object);
>    }
>
> If I change it into code like follows, the save() will really save an item
> to database.
>
>    @SuppressWarnings("unchecked")
>    public T save(T object) {
>        Session sess = getSession();
>
>        getSession().save(object);
>        getSession().flush();
>
>        return (T) object;
>    }
>
> And, I find that in the UserDaoHibernate.java, the save() is actually
> overrided. The save() processes the following codes,
>
>    public User saveUser(User user) {
>        if (log.isDebugEnabled()) {
>            log.debug("user's id: " + user.getId());
>        }
>        getSession().saveOrUpdate(user);
>        // necessary to throw a DataIntegrityViolation and catch it in
> UserManager
>        getSession().flush();
>        return user;
>    }
>
> So, my questions are as follows,
> 1. why the GenericDaoHibernate.java is designed to do not have flush()

Because if you look at the code above, it’s only needed if there’s exceptions that might be thrown. If you run “appfuse:full-source” on your project, you can modify the default behavior of GenericDaoHibernate.

> 2. If I want to use GenericDaoHibernate directly, should I revise it to have
> flush() in save(), delete(), etc?

Only if it seems necessary for your needs.

> 3. whether it is necessary to call close() after flush();

No, if you close the session, those objects that are lazy-loaded will not be available.

Hope this helps,

Matt

>
> I am definitely new to hibernate. I make the descriptions clear to be
> helpful to other beginners.
>
> Thank you very much!
>
> Best wishes,
> Yang
>
>
>
> --
> View this message in context: http://appfuse.547863.n4.nabble.com/No-Session-found-for-current-thread-tp4657655p4657690.html
> Sent from the AppFuse - User mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: No Session found for current thread

developer
This post has NOT been accepted by the mailing list yet.
In reply to this post by musicpxg
annotate method with @Transactional, there is a similar discussion here no session found for current thread