-->
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.  [ 2 posts ] 
Author Message
 Post subject: Session is closed! Cannot reopen.
PostPosted: Wed Jul 01, 2009 12:22 am 
Newbie

Joined: Wed Jul 01, 2009 12:11 am
Posts: 1
I'm new to Hibernate and trying to figure things out. I've done a lot of searching, but haven't been able to find any adequate answers to help me. So any assistance would be great.

Getting: Exception in thread "main" org.hibernate.SessionException: Session is closed!

When trying the createAccount() method, it uses an isValidated() method which checks to see if an email already exists in the DB... it begins a transaction, queries the DB, and commits. If it passes validation, the createAccount() method will begin its own transaction, update the db, and commit its changes. I run into the problem where the create account method cannot begin its own transaction, and I get the exception that the session is closed.

I'm assuming I'm just using the factories/sessions incorrectly and I just need to be pointed towards the right structure of setting up my db transactions.


Code below:


Code:
private static final Session session;
   
static
{
   session = HibernateUtil.getSessionFactory().getCurrentSession();
}

protected static void endTransaction()
{
   session.getTransaction().commit();
}

protected static void beginTransaction()
{
   session.beginTransaction();
}
   
protected static Query createQuery(String str)
{
   return session.createQuery(str);
}

protected static void save(Object o)
{
   session.save(o);
}


Create Account
Code:
   public Account createAccount(String firstName, String lastName, String email, String alias, String password) throws EmailNotAvailableException
   {
      Account toCreate = new Account();
      toCreate.setFirstName(firstName);
      toCreate.setLastName(lastName);
      toCreate.setEmail(email);
      toCreate.setAlias(alias);
      
      boolean isValidated = this.validate(toCreate);
      
      if(isValidated)
      {
         // Set up salt and encrypted password
         PasswordEncryption util = new PasswordEncryption();
         String salt = util.generateSalt();
         String encryptedPassword = util.encrypt(password, salt);
         
         toCreate.setSalt(salt);
         toCreate.setPassword(encryptedPassword);
         
         // Add account to database
         beginTransaction();
         save(toCreate);
         endTransaction();
      }
      
      return toCreate;
   }


Validate
Code:
   private boolean validate(Account account) throws EmailNotAvailableException
   {
      boolean toReturn = true;
      
      if(this.getAccountByEmail(account.getEmail()) != null)
      {
         toReturn = false;
         
         throw new EmailNotAvailableException(new Throwable());
      }
      
      
      return toReturn;
   }


GetAccountByEmail
Code:
   public Account getAccountByEmail(String email)
   {
      Account toReturn = null;
      beginTransaction();
      
      Query response = createQuery("from Account account where account.email='" + email + "'");
      List<Account> results = (List<Account>)response.list();
      
      if(!results.isEmpty())
      {
         toReturn = results.get(0);
      }
      
      endTransaction();
      return toReturn;
   }


Top
 Profile  
 
 Post subject: Re: Session is closed! Cannot reopen.
PostPosted: Wed Jul 01, 2009 7:51 am 
Newbie

Joined: Mon Mar 26, 2007 3:33 am
Posts: 18
I think problem with your HibernateUtil.getCurrentSession which is in static block.
create it as method e.g;

Session getCurrentSession(){
return HibernateUtil.getCurrentSession;
}

and in beginTransaction call this method to get the session.
It should work.


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