-->
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.  [ 3 posts ] 
Author Message
 Post subject: SessionException Committing a transaction throws an Exceptio
PostPosted: Wed Aug 24, 2005 5:30 am 
Newbie

Joined: Thu Jul 07, 2005 1:47 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5
Name and version of the database you are using: Oracle 9i

Hi,

during committing of a transactioin I got the following exception.


org.hibernate.SessionException: Session is closed
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:131)
at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:163)
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:142)
at de.bund.bva.fpb.server.basis.db.FPHibernateUtil.rollbackTransaction(FPHibernateUtil.java:213)
at de.bund.bva.fpb.server.basis.db.FPHibernateUtil.commitTransaction(FPHibernateUtil.java:159)



What could be the problem? In the activities I did before the last transaction (which causes the exception) the session was not closed and I did not call any "sessio..close".

I do not know how the session management of Hibernate works? But I do not if I have to call session.close after each transaction. We ware are working in a non-EJB-Environment with complex GUI workflows.

Thank you very much in advance,

KP


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 9:57 pm 
Regular
Regular

Joined: Sat Aug 28, 2004 4:15 pm
Posts: 61
The session management of Hibernate is very flexible. You decide that for yourself given your application's requirements.

The most basic approach is simply session per transaction. This means if you start a transaction you start a session and when you commit a transaction or rollback you end the session. The session life is likely bound to a database connection, though not always.

With Hibernate 3 this model is very easily supported.

You really should have a look at the following chapter of the reference guide. From there you'll be able to better decide which model fits with your application.

http://www.hibernate.org/hib_docs/v3/re ... tions.html

_________________
Joe W


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 7:12 pm 
Newbie

Joined: Thu May 05, 2005 5:57 pm
Posts: 7
Location: California, USA
I was seeing exactly the same problem in a non-managed environment using HiberateUtil.

It turned out that the session was indeed closed elsewhere in the code. But in my class, I have a DAO object that would start a new transaction. And the new transaction would start a new session. So it shouldn't have mattered that another session was closed earlier.

Well, I was wrong. The HibernateUtil we used, although slightly modified from the one from the Hibernate In Action book and from the one listed in the online doc, does not clear out the Transaction object in the ThreadLocal object when closing a session:

Code:
    // Method is simplified for readibility
    public static void closeSession() {
   Session s = (Session) _Session.get();

        if (s != null) {
            s.close();
       _Session.set(null);
        }
    }

Then when beginTransaction() is called, it checks to see if the Transaction in the ThreadLocal is null, and if it is, gets a new session and begins a new transaction:

Code:
    // Method is simplified for readibility
    public static void beginTransaction() {
        Transaction tx = (Transaction) _Transaction.get();
       
        if (tx == null) {
            tx = getSession().beginTransaction();
            _Transaction.set(tx);
        }
    }


Well, but tx is not null since closeSession() call earlier didn't set it to be null. So the application didn't get a new session nor begin a new transaction.

Then when I did a tx.commit(), I got exactly the same error as you did even though my current session is well and alive but the transaction itself was from the previous session.

I am not sure if it's the most elegant, but setting the Transaction object to be null in my closeConnection() method solved this problem:

Code:
    public static void closeSession() {
   Session s = (Session) _Session.get();

        if (s != null) {
            s.close();
       _Session.set(null);
       _Transaction.set(null);
        }
    }


Not sure if your exception was caused by the same nature but it is likely if you happen to use the HibernateUtil from the book or from the online doc, which does not nullify the Transaction object.

Hope this helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.