-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Obtaining a session
PostPosted: Fri Dec 24, 2004 12:44 pm 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
in p 33 and p 35 code starts with Session s = getSessionFactory().openSession();
further on i read about creating SessionFactory and later (p129)
i read Session s = sessions.openSession();
i also read (cant remember where) creating a session is inexpensive.
This said, i've a method that starts with:

Code:
    public void guardaConvencao() throws HibernateException, ParseException {

        sessao = Sessao.currentSession();
        Transaction t = sessao.beginTransaction();

        String nomeConvencao = rg.jTextFieldCv.getText();
//...

        Convencao cv = new Convencao();

        cv.setNomeConvencao(nomeConvencao);
//...

        sessao.save(cv);
        t.commit();
        /**
         * se fecho a sessão, quando clico em inserir novamente dá erro ao dizer
         * q a sessão está fechada
         *
         * resolução: na classe sessão fiz:
         * if (s == null || !s.isOpen()) {
         */
        sessao.close();
    }

this method is called when one clicks submit in a gui button.
So far so good, but if user submits again, i get an error stating session is closed!
In order to turn this around i changed HibernateUtil method like this:
Code:
if (s == null || !s.isOpen()) {
and now works fine

i've the sensation this isnt abslolutely correct, but i wonder where in book i can solve this in another way
thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 24, 2004 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I've no idea what you are asking for.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 24, 2004 12:51 pm 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
i changed this class
Code:
/*
* Created on 18/Dez/2004
*
* TODO
*/
package utilitarios;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class Sessao {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        }
        catch (HibernateException ex) {
            throw new RuntimeException(" problema de Configuração: "
                    + ex.getMessage(), ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null || !s.isOpen()) { // HERE I CHANGED
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null) {
            s.close();
        }
    }
}

what i ask is
1) if what i did is aceptable
2) where can i find in book a way to reopen a session - i'm using a standalone app - no web involved, just one single user
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 24, 2004 1:05 pm 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
maybe this test clarifies matters (why i had to alter HibernateUtil class)
Code:
    public void testSessaoAberta () throws HibernateException {
        Session sessao = Sessao.currentSession();
        Transaction t = sessao.beginTransaction();
        t.commit();
        sessao.close();
        sessao = Sessao.currentSession();
        boolean esperado = false;
        boolean obtido= sessao.isOpen();
        assertEquals("a sessao está fechada...",esperado,obtido );
    }

because i want the session to be open for next user input


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.