1

I need when I click on "Log out" to close the session of the user connected, so for That I was thinking about improving my bean and improving my closeSession() method which I will invoke it when I click on "Log out".

this is my profile.xhtml :

<h:html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
<h:head>

</h:head>
<h:body>
<div>
      <h:form>
        <p:row>
            <p:column>
                    <p:menuButton value="#{user.name}">
                        <p:menuitem value="log out" action="#{user.closeSession}"/>
                    </p:menuButton>
            </p:column>
        </p:row>
      </h:form>
</div>

</h:body>
</h:html>

This is my Bean User :

@ManagedBean(name="user")
@RequestScoped
public class User {

    private int id;
    private String name;
    @Column(unique=true)
    private String email;
    private String password;
    private String confirmationPass;
//  private image 


    public User() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public String getConfirmationPass() {
        return confirmationPass;
    }

    public void setConfirmationPass(String confirmationPass) {
        this.confirmationPass = confirmationPass;
    }


    public User(int id, String name, String email, String password,
            String confirmationPass) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
        this.confirmationPass = confirmationPass;
    }

    public User(int id, String name, String email, String password) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public User(String name, String email, String password) {
        super();
        this.name = name;
        this.email = email;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", Name=" + name + ", email=" + email
                + ", password=" + password + "]";
    }

    public String save(){


        UserBusiness userBusiness = new UserBusinessImp();
        userBusiness.add(new User(name, email,password));

        return "profile";

    }

    public void closeSession(){

    //I would like to close the session of the user when he click on Log out

    }

And my business Layer consitute of :

UserBusiness interface :

public interface UserBusiness {

    **public void add(User user);**
}

And UserBusinessImp which implements UserBusiness interface :

public class UserBusinessImp implements UserBusiness{

    public void add(User user) {
        UserDao userDao = new UserDaoMysql();
        userDao.insert(user);
    }
}

And DAO layer constitute of :

UserDao interface :

public interface UserDao {

    public void insert(User user);
}

And UserDaoMysql class which implements UserDao interface :

public class UserDaoMysql implements UserDao {

    private Session session;


    private void openSession(){
        SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
        session = sessionFactory.openSession();
        session.beginTransaction();
    }

    private void closeSession(){
        session.getTransaction().commit();
        session.close();
    }

    public void insert(User user) {

        try {
        openSession();
        User p = new User(user.getName(), user.getEmail(), user.getPassword());
        session.save(p);
        System.out.println("sauvegarde reussi");
        closeSession();
        } 
        catch (ConstraintViolationException e) {
            System.out.println("> email is already used");
            FacesMessage msg = new FacesMessage("Your email is already used !");  
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
}

This is one class util which called HibernateUtil :

public class HibernateUtil {

    private static SessionFactory sessionFactory = buildSessionFactory();
    private static ServiceRegistry serviceRegistry;

    private static SessionFactory buildSessionFactory() {
        try {

             Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            return sessionFactory;


        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

Remark : I am using MySQL as a database.

Anyone could help me please ! Thanks in advance.

  • Where is Stacktrace ? – Shahzeb Jul 28 '15 at 00:46
  • there is no Stacktrace because the closeSession() method is empty do you have any idea ? –  Jul 28 '15 at 01:05
  • Okay you don't need to close hibernate session like this. Infect you do not need to worry about it at all at that point. – Shahzeb Jul 28 '15 at 01:29
  • ok how Can I do it ? –  Jul 28 '15 at 01:32
  • Close which session? The browser HTTP session or the hibernate session? – kolossus Jul 28 '15 at 02:53
  • @kolossus : The session of the user connected sure hibernate session , you know first the user log in or sign up and then when he sign up he open the session and saved all his data like email and password later he can log out by click on Log Out menuItem so I think mu must close his session , right ? –  Jul 28 '15 at 03:04
  • 1
    Your current design is the problem: your classes should be designed in such a way as to allow a top-level component, like the managed bean (and I'm skeptical about even that; it still feels like a code-smell) to close the hibernate session. The way it's written now, your `UserDaoMysql` is walled off from the `User` class, where you should have been able to close the hibernate session from. You need to redesign your classes. I also don't recommend your managed bean doubling as your hibernate entity – kolossus Jul 28 '15 at 03:22
  • @kolossus :Yes I need to impliment a method in UserDaoMysql as you said but what can do in this method for call it when I click on menuItem, can you give me an example of close Session method ? –  Jul 28 '15 at 03:36
  • Like kolossus said: _"I also don't recommend your managed bean doubling as your hibernate entity"_: that is what I told you two questions ago… See I was trying to help – Kukeltje Jul 28 '15 at 07:58
  • Thanks @BalusC your comment is useful –  Jul 28 '15 at 11:54

0 Answers0