0

ZK HibernateUtil not working at SessionInit

asked 2011-11-08 02:14:48 +0800

RichardL gravatar image RichardL
768 4

I am using the HibernateUtil in my DAO class to retrieve the current session, as such:

Session currentSession() {
        return HibernateUtil.currentSession();
    }

The util works fine when the DAO methods are called from my controller, but if I call from SessionInit, i.e.
public class SessionInitiator implements SessionInit {

    @Override
    public void init(Session sn, Object o) throws Exception {
           ...
           User user = new AuthDAO().findUserByUsername("a_user_name");
           sn.setAttribute("user", user);//set user to session
           ...
    }
}

I get this error:

WARNING: StandardWrapperValve: PWC1406: Servlet.service() for servlet auEngine threw exception
org.hibernate.HibernateException: get is not valid without active transaction
	at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
	at $Proxy132.get(Unknown Source)
	at com.digstree.auth.data.AuthDAO.findUserByUsername(AuthDAO.java:42)
	at com.digstree.ui.init.SessionInitiator.init(SessionInitiator.java:23)
	at org.zkoss.zk.ui.util.Configuration.invokeSessionInits(Configuration.java:772)
	at org.zkoss.zk.ui.http.SimpleSession.<init>(SimpleSession.java:154)
	at org.zkoss.zk.ui.http.SimpleSession.<init>(SimpleSession.java:112)
	at org.zkoss.zk.ui.http.SimpleUiFactory.newSession(SimpleUiFactory.java:35)
	at org.zkoss.zk.ui.sys.SessionsCtrl.newSession(SessionsCtrl.java:150)
	at org.zkoss.zk.ui.http.WebManager.getSession(WebManager.java:404)
	at org.zkoss.zk.ui.http.WebManager.getSession(WebManager.java:398)

Why is this? Thanks

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2011-11-08 03:13:36 +0800

matthewgo gravatar image matthewgo
375

Hi RichardL,
Try to use "HibernateUtil.getSessionFactory().openSession() " instead of using "HibernateUtil.getSessionFactory().getCurrentSession()".

link publish delete flag offensive edit

answered 2011-11-08 09:51:08 +0800

RichardL gravatar image RichardL
768 4

Works!

Thank you for the solution, Matthew.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-11-08 02:14:48 +0800

Seen: 198 times

Last updated: Nov 08 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More