You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Ayub Khan <ay...@gmail.com> on 2008/09/11 11:22:18 UTC

Implementing Hibernate Open session per view in Apache MyFaces

Hi,
I have impleted the phaseListener using the idea presented on
http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per

I am getting "org.hibernate.HibernateException: createQuery is not valid
without active transaction" error message.

Below is the code of phase listeners:

public class RestoreViewPhaseListener implements PhaseListener {

  public void afterPhase(PhaseEvent event) {
  }

  public void beforePhase(PhaseEvent event) {
SessionFactory sessionFactory =

              HibernateUtil.getSessionFactory();
sessionFactory.getCurrentSession().beginTransaction();


  }

  public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
  }

}

Please let me where to open and close session and transaction.

Thanks
Ayub



-- 
-- "Two roads diverged in a wood
And I took the one less traveled by
And that has made all the difference.

Robert Frost....."

Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Hazem Saleh <ha...@apache.org>.
I updated the blog entry by the single working PL.

http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per

*Here it is:*
public class OSPVFPL implements PhaseListener {

    public void beforePhase(PhaseEvent event) {
             SessionFactory sessionFactory =
HibernateSessionFactory.getInstance();
             sessionFactory.getCurrentSession().beginTransaction();
    }

    public void afterPhase(PhaseEvent event) {
            SessionFactory sessionFactory =
HibernateSessionFactory.getInstance();
            try {

sessionFactory.getCurrentSession().getTransaction().commit();
            } catch (Throwable ex) {

              // very bad error here, occured...
              if (sessionFactory.getCurrentSession().
                  getTransaction().isActive()) {
                      sessionFactory.getCurrentSession().
                      getTransaction().rollback();
              }
            }
    }

    public PhaseId getPhaseId() {
            return PhaseId.INVOKE_APPLICATION;
    }
}

On Thu, Sep 11, 2008 at 12:34 PM, Hazem Saleh <ha...@apache.org> wrote:

> I will provide the new proper way soon to the wiki as well + a working
> sample to make everything clear.
>
>
> On Thu, Sep 11, 2008 at 12:27 PM, Hazem Saleh <ha...@apache.org> wrote:
>
>> Just a small note, I think you do not need two transactions as the SELECT
>> operations do not transactions.
>>
>> If you implemented a single INVOKE_APPLICATION PL that will be enough.
>>
>> Thank you.
>>
>>
>> On Thu, Sep 11, 2008 at 11:48 AM, Ayub Khan <ay...@gmail.com> wrote:
>>
>>> Thank you Simon !!! I will try implementing the transactions around the
>>> listeners and check if it resolves the issue.
>>> Regards
>>> Ayub
>>>
>>>
>>> On Thu, Sep 11, 2008 at 9:35 PM, Simon Kitching <sk...@apache.org>wrote:
>>>
>>>> Ayub Khan schrieb:
>>>>
>>>>> Hi,
>>>>>
>>>>> I have impleted the phaseListener using the idea presented on
>>>>> http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>>>>>
>>>>> I am getting "org.hibernate.HibernateException: createQuery is not
>>>>> valid without active transaction" error message.
>>>>> Below is the code of phase listeners:
>>>>>
>>>>> public class RestoreViewPhaseListener implements PhaseListener {
>>>>>
>>>>>  public void afterPhase(PhaseEvent event) {
>>>>>  }
>>>>>
>>>>>  public void beforePhase(PhaseEvent event) {
>>>>> SessionFactory sessionFactory =
>>>>>  HibernateUtil.getSessionFactory();
>>>>> sessionFactory.getCurrentSession().beginTransaction();
>>>>>
>>>>>
>>>>>  }
>>>>>
>>>>>  public PhaseId getPhaseId() {
>>>>>   return PhaseId.RESTORE_VIEW;
>>>>>  }
>>>>>
>>>>> }
>>>>>
>>>>> Please let me where to open and close session and transaction.
>>>>>
>>>>
>>>> Firstly, as Hazem's blog says, you need two PhaseListeners.
>>>>
>>>> But the blog article looks incomplete anyway. As the comment by wolveman
>>>> points out, there probably needs to be a transaction around the render phase
>>>> as well as one around the postback phase. Shouldn't be too hard to
>>>> implement..
>>>>
>>>> Regards,
>>>> Simon
>>>>
>>>>
>>>
>>>
>>> --
>>> -- "Two roads diverged in a wood
>>> And I took the one less traveled by
>>> And that has made all the difference.
>>>
>>> Robert Frost....."
>>>
>>
>>
>>
>> --
>> Hazem Ahmed Saleh Ahmed
>>
>> Web blog: http://www.jroller.com/page/HazemBlog
>>
>> [Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
>> http://code.google.com/p/gmaps4jsf/
>>
>
>
>
> --
> Hazem Ahmed Saleh Ahmed
>
> Web blog: http://www.jroller.com/page/HazemBlog
>
> [Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
> http://code.google.com/p/gmaps4jsf/
>



-- 
Hazem Ahmed Saleh Ahmed

Web blog: http://www.jroller.com/page/HazemBlog

[Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
http://code.google.com/p/gmaps4jsf/

Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Hazem Saleh <ha...@apache.org>.
I will provide the new proper way soon to the wiki as well + a working
sample to make everything clear.

On Thu, Sep 11, 2008 at 12:27 PM, Hazem Saleh <ha...@apache.org> wrote:

> Just a small note, I think you do not need two transactions as the SELECT
> operations do not transactions.
>
> If you implemented a single INVOKE_APPLICATION PL that will be enough.
>
> Thank you.
>
>
> On Thu, Sep 11, 2008 at 11:48 AM, Ayub Khan <ay...@gmail.com> wrote:
>
>> Thank you Simon !!! I will try implementing the transactions around the
>> listeners and check if it resolves the issue.
>> Regards
>> Ayub
>>
>>
>> On Thu, Sep 11, 2008 at 9:35 PM, Simon Kitching <sk...@apache.org>wrote:
>>
>>> Ayub Khan schrieb:
>>>
>>>> Hi,
>>>>
>>>> I have impleted the phaseListener using the idea presented on
>>>> http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>>>>
>>>> I am getting "org.hibernate.HibernateException: createQuery is not valid
>>>> without active transaction" error message.
>>>> Below is the code of phase listeners:
>>>>
>>>> public class RestoreViewPhaseListener implements PhaseListener {
>>>>
>>>>  public void afterPhase(PhaseEvent event) {
>>>>  }
>>>>
>>>>  public void beforePhase(PhaseEvent event) {
>>>> SessionFactory sessionFactory =
>>>>  HibernateUtil.getSessionFactory();
>>>> sessionFactory.getCurrentSession().beginTransaction();
>>>>
>>>>
>>>>  }
>>>>
>>>>  public PhaseId getPhaseId() {
>>>>   return PhaseId.RESTORE_VIEW;
>>>>  }
>>>>
>>>> }
>>>>
>>>> Please let me where to open and close session and transaction.
>>>>
>>>
>>> Firstly, as Hazem's blog says, you need two PhaseListeners.
>>>
>>> But the blog article looks incomplete anyway. As the comment by wolveman
>>> points out, there probably needs to be a transaction around the render phase
>>> as well as one around the postback phase. Shouldn't be too hard to
>>> implement..
>>>
>>> Regards,
>>> Simon
>>>
>>>
>>
>>
>> --
>> -- "Two roads diverged in a wood
>> And I took the one less traveled by
>> And that has made all the difference.
>>
>> Robert Frost....."
>>
>
>
>
> --
> Hazem Ahmed Saleh Ahmed
>
> Web blog: http://www.jroller.com/page/HazemBlog
>
> [Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
> http://code.google.com/p/gmaps4jsf/
>



-- 
Hazem Ahmed Saleh Ahmed

Web blog: http://www.jroller.com/page/HazemBlog

[Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
http://code.google.com/p/gmaps4jsf/

Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Hazem Saleh <ha...@apache.org>.
Just a small note, I think you do not need two transactions as the SELECT
operations do not transactions.

If you implemented a single INVOKE_APPLICATION PL that will be enough.

Thank you.

On Thu, Sep 11, 2008 at 11:48 AM, Ayub Khan <ay...@gmail.com> wrote:

> Thank you Simon !!! I will try implementing the transactions around the
> listeners and check if it resolves the issue.
> Regards
> Ayub
>
>
> On Thu, Sep 11, 2008 at 9:35 PM, Simon Kitching <sk...@apache.org>wrote:
>
>> Ayub Khan schrieb:
>>
>>> Hi,
>>>
>>> I have impleted the phaseListener using the idea presented on
>>> http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>>>
>>> I am getting "org.hibernate.HibernateException: createQuery is not valid
>>> without active transaction" error message.
>>> Below is the code of phase listeners:
>>>
>>> public class RestoreViewPhaseListener implements PhaseListener {
>>>
>>>  public void afterPhase(PhaseEvent event) {
>>>  }
>>>
>>>  public void beforePhase(PhaseEvent event) {
>>> SessionFactory sessionFactory =
>>>  HibernateUtil.getSessionFactory();
>>> sessionFactory.getCurrentSession().beginTransaction();
>>>
>>>
>>>  }
>>>
>>>  public PhaseId getPhaseId() {
>>>   return PhaseId.RESTORE_VIEW;
>>>  }
>>>
>>> }
>>>
>>> Please let me where to open and close session and transaction.
>>>
>>
>> Firstly, as Hazem's blog says, you need two PhaseListeners.
>>
>> But the blog article looks incomplete anyway. As the comment by wolveman
>> points out, there probably needs to be a transaction around the render phase
>> as well as one around the postback phase. Shouldn't be too hard to
>> implement..
>>
>> Regards,
>> Simon
>>
>>
>
>
> --
> -- "Two roads diverged in a wood
> And I took the one less traveled by
> And that has made all the difference.
>
> Robert Frost....."
>



-- 
Hazem Ahmed Saleh Ahmed

Web blog: http://www.jroller.com/page/HazemBlog

[Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
http://code.google.com/p/gmaps4jsf/

Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Ayub Khan <ay...@gmail.com>.
Thank you Simon !!! I will try implementing the transactions around the
listeners and check if it resolves the issue.
Regards
Ayub

On Thu, Sep 11, 2008 at 9:35 PM, Simon Kitching <sk...@apache.org>wrote:

> Ayub Khan schrieb:
>
>> Hi,
>>
>> I have impleted the phaseListener using the idea presented on
>> http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>>
>> I am getting "org.hibernate.HibernateException: createQuery is not valid
>> without active transaction" error message.
>> Below is the code of phase listeners:
>>
>> public class RestoreViewPhaseListener implements PhaseListener {
>>
>>  public void afterPhase(PhaseEvent event) {
>>  }
>>
>>  public void beforePhase(PhaseEvent event) {
>> SessionFactory sessionFactory =
>>  HibernateUtil.getSessionFactory();
>> sessionFactory.getCurrentSession().beginTransaction();
>>
>>
>>  }
>>
>>  public PhaseId getPhaseId() {
>>   return PhaseId.RESTORE_VIEW;
>>  }
>>
>> }
>>
>> Please let me where to open and close session and transaction.
>>
>
> Firstly, as Hazem's blog says, you need two PhaseListeners.
>
> But the blog article looks incomplete anyway. As the comment by wolveman
> points out, there probably needs to be a transaction around the render phase
> as well as one around the postback phase. Shouldn't be too hard to
> implement..
>
> Regards,
> Simon
>
>


-- 
-- "Two roads diverged in a wood
And I took the one less traveled by
And that has made all the difference.

Robert Frost....."

Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Simon Kitching <sk...@apache.org>.
Ayub Khan schrieb:
> Hi,
>
> I have impleted the phaseListener using the idea presented 
> on http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>
> I am getting "org.hibernate.HibernateException: createQuery is not 
> valid without active transaction" error message. 
>
> Below is the code of phase listeners:
>
> public class RestoreViewPhaseListener implements PhaseListener {
>
>  public void afterPhase(PhaseEvent event) {
>  }
>
>  public void beforePhase(PhaseEvent event) {
> SessionFactory sessionFactory = 
>           
>               HibernateUtil.getSessionFactory();
> sessionFactory.getCurrentSession().beginTransaction();
>
>
>  }
>
>  public PhaseId getPhaseId() {
>    return PhaseId.RESTORE_VIEW;
>  }
>
> }
>
> Please let me where to open and close session and transaction.

Firstly, as Hazem's blog says, you need two PhaseListeners.

But the blog article looks incomplete anyway. As the comment by wolveman 
points out, there probably needs to be a transaction around the render 
phase as well as one around the postback phase. Shouldn't be too hard to 
implement..

Regards,
Simon


Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Ayub Khan <ay...@gmail.com>.
Hi Hazem,
Attached is the hibernate.cfg.xml file for your reference.

Thank you

Regards
Ayub

On Fri, Sep 12, 2008 at 3:38 PM, Hazem Saleh <ha...@apache.org> wrote:

> Please send me the (Hibernate.cfg.xml) file content.
> Thank you.
>
>
> On Fri, Sep 12, 2008 at 2:03 AM, Ayub Khan <ay...@gmail.com> wrote:
>
>> Hi Hazem,
>> I have implemented as per your suggestion using a single PL.
>>
>> The PL is registered in facesConfig.xml and the hibernateconfig file has
>>
>>  <property name="current_session_context_class">thread</property>
>>
>> still I am getting
>>
>>  "org.hibernate.LazyInitializationException: could not initialize proxy -
>> no Session" error
>>
>> Any clues ?
>> ==========================================
>> =========================================
>> public class RestoreViewPhaseListener implements PhaseListener {
>>
>>  public void beforePhase(PhaseEvent event) {
>>          SessionFactory sessionFactory =
>> HibernateUtil.getSessionFactory();
>>           sessionFactory.getCurrentSession().beginTransaction();
>>
>> }
>>
>> public void afterPhase(PhaseEvent event) {
>>         SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
>>          try {
>>             sessionFactory.getCurrentSession().getTransaction().commit();
>>         } catch (Throwable ex) {
>>
>>           // very bad error here, occured...
>>           if (sessionFactory.getCurrentSession().
>>               getTransaction().isActive()) {
>>                   sessionFactory.getCurrentSession().
>>                   getTransaction().rollback();
>>           }
>>         }
>> }
>>
>> public PhaseId getPhaseId() {
>>         return PhaseId.INVOKE_APPLICATION;
>>
>> }
>> }
>>
>>  =========================
>> HibernateUtil.java
>> ==========================
>> public class HibernateUtil {
>>     private static final SessionFactory sessionFactory;
>>
>>     static {
>>         try {
>>             // Create the SessionFactory from hibernate.cfg.xml
>>             sessionFactory = new
>> Configuration().configure().buildSessionFactory();
>>
>>         } catch (Throwable ex) {
>>             // Make sure you log the exception, as it might be swallowed
>>             System.err.println("Initial SessionFactory creation failed." +
>> ex);
>>             throw new ExceptionInInitializerError(ex);
>>         }
>>     }
>>
>>     public static SessionFactory getSessionFactory() {
>>         return sessionFactory;
>>     }
>>
>> }
>>
>>
>> Can you tell me if I am missing anything here?
>>
>> Thank you.
>>
>> Regards
>> Ayub
>>
>> On Thu, Sep 11, 2008 at 9:45 PM, Hazem Saleh <ha...@apache.org> wrote:
>>
>>> Hi Ayub,
>>>
>>> I tried this code before and it worked fine.
>>> You should check your hibernate configuration, and make sure that:
>>> 1. You are registered your PLs.
>>> 2. <property name="current_session_context_class">thread</property>.
>>>
>>> Another better idea, I got later, you can use only one PL on the
>>> (INVOKE_APPLICATION) phase to implement the OSPVP.
>>>
>>> Thank you.
>>>
>>>
>>> On Thu, Sep 11, 2008 at 11:22 AM, Ayub Khan <ay...@gmail.com> wrote:
>>>
>>>> Hi,
>>>> I have impleted the phaseListener using the idea presented on
>>>> http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>>>>
>>>> I am getting "org.hibernate.HibernateException: createQuery is not
>>>> valid without active transaction" error message.
>>>>
>>>> Below is the code of phase listeners:
>>>>
>>>> public class RestoreViewPhaseListener implements PhaseListener {
>>>>
>>>>   public void afterPhase(PhaseEvent event) {
>>>>   }
>>>>
>>>>   public void beforePhase(PhaseEvent event) {
>>>> SessionFactory sessionFactory =
>>>>
>>>>               HibernateUtil.getSessionFactory();
>>>> sessionFactory.getCurrentSession().beginTransaction();
>>>>
>>>>
>>>>   }
>>>>
>>>>   public PhaseId getPhaseId() {
>>>>     return PhaseId.RESTORE_VIEW;
>>>>   }
>>>>
>>>> }
>>>>
>>>> Please let me where to open and close session and transaction.
>>>>
>>>> Thanks
>>>> Ayub
>>>>
>>>>
>>>>
>>>> --
>>>> -- "Two roads diverged in a wood
>>>> And I took the one less traveled by
>>>> And that has made all the difference.
>>>>
>>>> Robert Frost....."
>>>>
>>>
>>>
>>>
>>> --
>>> Hazem Ahmed Saleh Ahmed
>>>
>>> Web blog: http://www.jroller.com/page/HazemBlog
>>>
>>> [Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
>>> http://code.google.com/p/gmaps4jsf/
>>>
>>
>>
>>
>> --
>> -- "Two roads diverged in a wood
>> And I took the one less traveled by
>> And that has made all the difference.
>>
>> Robert Frost....."
>>
>
>
>
> --
> Hazem Ahmed Saleh Ahmed
>
> Web blog: http://www.jroller.com/page/HazemBlog
>
> [Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
> http://code.google.com/p/gmaps4jsf/
>



-- 
-- "Two roads diverged in a wood
And I took the one less traveled by
And that has made all the difference.

Robert Frost....."

RE: Implementing Hibernate Open session per view in Apache MyFaces

Posted by bobhedlund <bo...@gmail.com>.
Could it be that you need to store your session somewhere? Perhaps on the
ThreadLocalSessionContext, or your HttpSession? That way you can retrieve
and use the one you created. Your transaction code afterPhase looks ok. I
have had success storing the Hibernate Session on the HttpSession. 
 
In the PL:
static{

    sf = SessionFactorySingleton.getInstance().getSessionFactory();

}

 

Try this for the before phase: 
public void beforePhase(PhaseEvent phaseEvent) {


    ThreadLocalSessionContext.unbind(sf );

    Session hibernateSession =
(Session)getHttpSession().getAttribute(SESSION_ATTR);

    if(hibernateSession == null || !hibernateSession.isOpen()) {

        log.debug("No Hibernate Session found on HttpSession, create and
bind");

        ThreadLocalSessionContext.bind(sf.openSession());

        hibernateSession = sf.getCurrentSession();

    } else {

        ThreadLocalSessionContext.bind(hibernateSession);

    }

    addHttpSessionBean(SESSION_ATTR,hibernateSession);

    log.debug("Starting a database transaction");

    sf.getCurrentSession().beginTransaction();

}

 

Then your afterPhase call should work ok. 

Hope that helps 

Bob Hedlund

GorillaLogic

 
 
 
 
  _____  

From: hazem.saleh@gmail.com [mailto:hazem.saleh@gmail.com] On Behalf Of
Hazem Saleh
Sent: Thursday, September 11, 2008 9:38 PM
To: MyFaces Discussion
Subject: Re: Implementing Hibernate Open session per view in Apache MyFaces


Please send me the (Hibernate.cfg.xml) file content.
Thank you.


On Fri, Sep 12, 2008 at 2:03 AM, Ayub Khan <ay...@gmail.com> wrote:


Hi Hazem, 


I have implemented as per your suggestion using a single PL.

The PL is registered in facesConfig.xml and the hibernateconfig file has

 <property name="current_session_context_class">thread</property>

still I am getting

 "org.hibernate.LazyInitializationException: could not initialize proxy - no
Session" error

Any clues ?
==========================================
=========================================
public class RestoreViewPhaseListener implements PhaseListener {

public void beforePhase(PhaseEvent event) {
         SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
         sessionFactory.getCurrentSession().beginTransaction();

}

public void afterPhase(PhaseEvent event) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        try {
            sessionFactory.getCurrentSession().getTransaction().commit();
        } catch (Throwable ex) {

          // very bad error here, occured...
          if (sessionFactory.getCurrentSession().
              getTransaction().isActive()) {
                  sessionFactory.getCurrentSession().
                  getTransaction().rollback();
          }
        }
}

public PhaseId getPhaseId() {
        return PhaseId.INVOKE_APPLICATION;

}
}

=========================
HibernateUtil.java
==========================
public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new
Configuration().configure().buildSessionFactory();
            
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." +
ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}


Can you tell me if I am missing anything here?

Thank you.

Regards
Ayub

On Thu, Sep 11, 2008 at 9:45 PM, Hazem Saleh <ha...@apache.org> wrote:


Hi Ayub,

I tried this code before and it worked fine.
You should check your hibernate configuration, and make sure that:
1. You are registered your PLs.
2. <property name="current_session_context_class">thread</property>.

Another better idea, I got later, you can use only one PL on the
(INVOKE_APPLICATION) phase to implement the OSPVP.

Thank you. 


On Thu, Sep 11, 2008 at 11:22 AM, Ayub Khan <ay...@gmail.com> wrote:


Hi, 

I have impleted the phaseListener using the idea presented on
http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_p
er

I am getting "org.hibernate.HibernateException: createQuery is not valid
without active transaction" error message. 

Below is the code of phase listeners:

public class RestoreViewPhaseListener implements PhaseListener {

 public void afterPhase(PhaseEvent event) {
 }

 public void beforePhase(PhaseEvent event) {
SessionFactory sessionFactory = 
          
              HibernateUtil.getSessionFactory();
sessionFactory.getCurrentSession().beginTransaction();


 }

 public PhaseId getPhaseId() {
   return PhaseId.RESTORE_VIEW;
 }

}

Please let me where to open and close session and transaction.

Thanks
Ayub



-- 
-- "Two roads diverged in a wood
And I took the one less traveled by
And that has made all the difference.

Robert Frost....."





-- 
Hazem Ahmed Saleh Ahmed

Web blog: http://www.jroller.com/page/HazemBlog

[Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
http://code.google.com/p/gmaps4jsf/





-- 
-- "Two roads diverged in a wood
And I took the one less traveled by
And that has made all the difference.

Robert Frost....."





-- 
Hazem Ahmed Saleh Ahmed

Web blog: http://www.jroller.com/page/HazemBlog

[Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
http://code.google.com/p/gmaps4jsf/


Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Hazem Saleh <ha...@apache.org>.
Please send me the (Hibernate.cfg.xml) file content.
Thank you.

On Fri, Sep 12, 2008 at 2:03 AM, Ayub Khan <ay...@gmail.com> wrote:

> Hi Hazem,
> I have implemented as per your suggestion using a single PL.
>
> The PL is registered in facesConfig.xml and the hibernateconfig file has
>
>  <property name="current_session_context_class">thread</property>
>
> still I am getting
>
>  "org.hibernate.LazyInitializationException: could not initialize proxy -
> no Session" error
>
> Any clues ?
> ==========================================
> =========================================
> public class RestoreViewPhaseListener implements PhaseListener {
>
>  public void beforePhase(PhaseEvent event) {
>          SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
>          sessionFactory.getCurrentSession().beginTransaction();
>
> }
>
> public void afterPhase(PhaseEvent event) {
>         SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
>         try {
>             sessionFactory.getCurrentSession().getTransaction().commit();
>         } catch (Throwable ex) {
>
>           // very bad error here, occured...
>           if (sessionFactory.getCurrentSession().
>               getTransaction().isActive()) {
>                   sessionFactory.getCurrentSession().
>                   getTransaction().rollback();
>           }
>         }
> }
>
> public PhaseId getPhaseId() {
>         return PhaseId.INVOKE_APPLICATION;
>
> }
> }
>
> =========================
> HibernateUtil.java
> ==========================
> public class HibernateUtil {
>     private static final SessionFactory sessionFactory;
>
>     static {
>         try {
>             // Create the SessionFactory from hibernate.cfg.xml
>             sessionFactory = new
> Configuration().configure().buildSessionFactory();
>
>         } catch (Throwable ex) {
>             // Make sure you log the exception, as it might be swallowed
>             System.err.println("Initial SessionFactory creation failed." +
> ex);
>             throw new ExceptionInInitializerError(ex);
>         }
>     }
>
>     public static SessionFactory getSessionFactory() {
>         return sessionFactory;
>     }
>
> }
>
>
> Can you tell me if I am missing anything here?
>
> Thank you.
>
> Regards
> Ayub
>
> On Thu, Sep 11, 2008 at 9:45 PM, Hazem Saleh <ha...@apache.org> wrote:
>
>> Hi Ayub,
>>
>> I tried this code before and it worked fine.
>> You should check your hibernate configuration, and make sure that:
>> 1. You are registered your PLs.
>> 2. <property name="current_session_context_class">thread</property>.
>>
>> Another better idea, I got later, you can use only one PL on the
>> (INVOKE_APPLICATION) phase to implement the OSPVP.
>>
>> Thank you.
>>
>>
>> On Thu, Sep 11, 2008 at 11:22 AM, Ayub Khan <ay...@gmail.com> wrote:
>>
>>> Hi,
>>> I have impleted the phaseListener using the idea presented on
>>> http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>>>
>>> I am getting "org.hibernate.HibernateException: createQuery is not valid
>>> without active transaction" error message.
>>>
>>> Below is the code of phase listeners:
>>>
>>> public class RestoreViewPhaseListener implements PhaseListener {
>>>
>>>   public void afterPhase(PhaseEvent event) {
>>>   }
>>>
>>>   public void beforePhase(PhaseEvent event) {
>>> SessionFactory sessionFactory =
>>>
>>>               HibernateUtil.getSessionFactory();
>>> sessionFactory.getCurrentSession().beginTransaction();
>>>
>>>
>>>   }
>>>
>>>   public PhaseId getPhaseId() {
>>>     return PhaseId.RESTORE_VIEW;
>>>   }
>>>
>>> }
>>>
>>> Please let me where to open and close session and transaction.
>>>
>>> Thanks
>>> Ayub
>>>
>>>
>>>
>>> --
>>> -- "Two roads diverged in a wood
>>> And I took the one less traveled by
>>> And that has made all the difference.
>>>
>>> Robert Frost....."
>>>
>>
>>
>>
>> --
>> Hazem Ahmed Saleh Ahmed
>>
>> Web blog: http://www.jroller.com/page/HazemBlog
>>
>> [Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
>> http://code.google.com/p/gmaps4jsf/
>>
>
>
>
> --
> -- "Two roads diverged in a wood
> And I took the one less traveled by
> And that has made all the difference.
>
> Robert Frost....."
>



-- 
Hazem Ahmed Saleh Ahmed

Web blog: http://www.jroller.com/page/HazemBlog

[Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
http://code.google.com/p/gmaps4jsf/

Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Ayub Khan <ay...@gmail.com>.
Hi Hazem,
I have implemented as per your suggestion using a single PL.

The PL is registered in facesConfig.xml and the hibernateconfig file has

 <property name="current_session_context_class">thread</property>

still I am getting

 "org.hibernate.LazyInitializationException: could not initialize proxy - no
Session" error

Any clues ?
==========================================
=========================================
public class RestoreViewPhaseListener implements PhaseListener {

 public void beforePhase(PhaseEvent event) {
         SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
         sessionFactory.getCurrentSession().beginTransaction();

}

public void afterPhase(PhaseEvent event) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        try {
            sessionFactory.getCurrentSession().getTransaction().commit();
        } catch (Throwable ex) {

          // very bad error here, occured...
          if (sessionFactory.getCurrentSession().
              getTransaction().isActive()) {
                  sessionFactory.getCurrentSession().
                  getTransaction().rollback();
          }
        }
}

public PhaseId getPhaseId() {
        return PhaseId.INVOKE_APPLICATION;

}
}

=========================
HibernateUtil.java
==========================
public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new
Configuration().configure().buildSessionFactory();

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." +
ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}


Can you tell me if I am missing anything here?

Thank you.

Regards
Ayub

On Thu, Sep 11, 2008 at 9:45 PM, Hazem Saleh <ha...@apache.org> wrote:

> Hi Ayub,
>
> I tried this code before and it worked fine.
> You should check your hibernate configuration, and make sure that:
> 1. You are registered your PLs.
> 2. <property name="current_session_context_class">thread</property>.
>
> Another better idea, I got later, you can use only one PL on the
> (INVOKE_APPLICATION) phase to implement the OSPVP.
>
> Thank you.
>
>
> On Thu, Sep 11, 2008 at 11:22 AM, Ayub Khan <ay...@gmail.com> wrote:
>
>> Hi,
>> I have impleted the phaseListener using the idea presented on
>> http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>>
>> I am getting "org.hibernate.HibernateException: createQuery is not valid
>> without active transaction" error message.
>>
>> Below is the code of phase listeners:
>>
>> public class RestoreViewPhaseListener implements PhaseListener {
>>
>>   public void afterPhase(PhaseEvent event) {
>>   }
>>
>>   public void beforePhase(PhaseEvent event) {
>> SessionFactory sessionFactory =
>>
>>               HibernateUtil.getSessionFactory();
>> sessionFactory.getCurrentSession().beginTransaction();
>>
>>
>>   }
>>
>>   public PhaseId getPhaseId() {
>>     return PhaseId.RESTORE_VIEW;
>>   }
>>
>> }
>>
>> Please let me where to open and close session and transaction.
>>
>> Thanks
>> Ayub
>>
>>
>>
>> --
>> -- "Two roads diverged in a wood
>> And I took the one less traveled by
>> And that has made all the difference.
>>
>> Robert Frost....."
>>
>
>
>
> --
> Hazem Ahmed Saleh Ahmed
>
> Web blog: http://www.jroller.com/page/HazemBlog
>
> [Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
> http://code.google.com/p/gmaps4jsf/
>



-- 
-- "Two roads diverged in a wood
And I took the one less traveled by
And that has made all the difference.

Robert Frost....."

Re: Implementing Hibernate Open session per view in Apache MyFaces

Posted by Hazem Saleh <ha...@apache.org>.
Hi Ayub,

I tried this code before and it worked fine.
You should check your hibernate configuration, and make sure that:
1. You are registered your PLs.
2. <property name="current_session_context_class">thread</property>.

Another better idea, I got later, you can use only one PL on the
(INVOKE_APPLICATION) phase to implement the OSPVP.

Thank you.

On Thu, Sep 11, 2008 at 11:22 AM, Ayub Khan <ay...@gmail.com> wrote:

> Hi,
> I have impleted the phaseListener using the idea presented on
> http://www.jroller.com/HazemBlog/entry/implementing_hibernate_open_session_per
>
> I am getting "org.hibernate.HibernateException: createQuery is not valid
> without active transaction" error message.
>
> Below is the code of phase listeners:
>
> public class RestoreViewPhaseListener implements PhaseListener {
>
>   public void afterPhase(PhaseEvent event) {
>   }
>
>   public void beforePhase(PhaseEvent event) {
> SessionFactory sessionFactory =
>
>               HibernateUtil.getSessionFactory();
> sessionFactory.getCurrentSession().beginTransaction();
>
>
>   }
>
>   public PhaseId getPhaseId() {
>     return PhaseId.RESTORE_VIEW;
>   }
>
> }
>
> Please let me where to open and close session and transaction.
>
> Thanks
> Ayub
>
>
>
> --
> -- "Two roads diverged in a wood
> And I took the one less traveled by
> And that has made all the difference.
>
> Robert Frost....."
>



-- 
Hazem Ahmed Saleh Ahmed

Web blog: http://www.jroller.com/page/HazemBlog

[Web 2.0] GMaps Integration with JSF + Apache Tomahawk + JBoss a4j:
http://code.google.com/p/gmaps4jsf/