-->
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: PROBLEM:Session was already closed
PostPosted: Mon Sep 13, 2010 7:44 am 
Newbie

Joined: Mon Sep 13, 2010 7:37 am
Posts: 1
when run my first hibernate code with annotation gives an exception

Quote:
Exception in thread "main" org.hibernate.SessionException: Session was already closed
at org.hibernate.impl.SessionImpl.close(SessionImpl.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy7.close(Unknown Source)
at com.hibernate.example.Example.main(Example.java:25)



this is my code
Code:
public static void main(String[] args) {
      SessionFactory session = HibernateUtil.getSessionFactory();
      Session sess = session.getCurrentSession();
      Transaction trans = sess.beginTransaction();

      Employee e1 = new Employee();
      e1.setId(new Integer(5));
      e1.setName("Aric");
      
      sess.save(e1);
      trans.commit();
      System.out.println("Record Inserted");
      sess.close();

   }


please help me


Top
 Profile  
 
 Post subject: Re: PROBLEM:Session was already closed
PostPosted: Mon Sep 13, 2010 8:02 am 
Newbie

Joined: Fri Feb 19, 2010 4:26 am
Posts: 8
Hi Tharaka,

Before asking session.getCurrentSession() you need to perform session.openSession() first.

Hope this resolve your issue.


Top
 Profile  
 
 Post subject: Re: PROBLEM:Session was already closed
PostPosted: Mon Sep 13, 2010 5:48 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
@King
That's incorrect. You can get the currentSession even if there is not one opened, it will open it for you and store in the context you configured it to be. That's whole point of using it.

@Tharaka
Your error message comes from the line:
Code:
session.close();
, you need to remove that. Hibernate session will auto close when you invoke
Code:
trans.commit();
. So you are closing a session that's already closed, thus the error msg.

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


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.