-->
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.  [ 5 posts ] 
Author Message
 Post subject: HibernateUtil and sess.isOpen()
PostPosted: Wed Dec 29, 2004 8:58 am 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
in your HibernateUtil class you'v a method that states

Quote:
...If no Session is open, opens a new Session...

i came up with this jUnitTest:
Code:
    public void testHibernateUtilSessaoAberta() throws HibernateException {
       
        Session sessao = HibernateUtil.getSession();
        sessao.close();

        //sessao = HibernateUtil.getSessionFactory().openSession();
        sessao = HibernateUtil.getSession();

        boolean esperado = true;
        boolean obtido = sessao.isOpen();
        assertEquals("a sessao está aberta...", esperado, obtido);
    }

which fails...
if instead i comment getSession and uncomment getSessionFactory, etc, test is ok
My purpose is to see if after i close a session i'm able to reopen it, and how can that be done

My app is a standalone desktop app, with 2.1.7c and MySql - only one user at all.
Can someone pls help me?
thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 9:03 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No problems here, this code is really trivial. Please, post code examples in English next time.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 9:05 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Your usage of assertEuqals() and "true" should get a place in the code hall of fame :)

P.S. Use HibernateUtil.closeSession()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 9:21 am 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
Code:
    public void testHibernateUtilSessionIsOpen() throws HibernateException {
       
        Session s = HibernateUtil.getSession();
        HibernateUtil.closeSession();
        s = HibernateUtil.getSession();
        assertTrue(s.isOpen());
    }

runs ok
thank you

let me try to get help from you: i'm also testing if hibernate throws an exception, with this test:
Code:
    public void testThrowHibernateException() throws HibernateException {

        String a = null;
        String b = null;
        String c = null;
        Double d = null;
        Date e = null;
        String f = null;
        Double g = null;
        Double h = null;

        Da da = new Da();

        try {

            da.go(a,b,c,d,e,f,g,h);
           
            fail("should print: he");

        }
        catch (HibernateException e1) {
           
            System.out.println("yes, does throw HibernateException");
     
        }

    }

test runs ok, ecpect for one thing: if i run AllTests, i've to put this one in the very end, if not i get ugly msgs stating:
Quote:
net.sf.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)

what am i doing wrong?
thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 9:43 am 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
case solved
:)
your recomendation (HibernateUtil.closeSession()) solves my last prob too
thanks a lot


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.