-->
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: session and transaction synchronizing issue while exceptions
PostPosted: Wed Jan 17, 2007 12:09 pm 
Beginner
Beginner

Joined: Tue Sep 19, 2006 5:46 am
Posts: 21
Location: india
Hi all,

I am having 2 issues

a) While testing the application I getting a "out of memory" error.


b) and If an exception occurs at the middle of the application then the application just hangs till we restart the App server.

I thing its because of the session and transaction not synchronized. do anybody have an idea how to solve it and one more thing do we have to close the session explicitly after a commit() or rollback() while using getCurrentSession() or will it be closed automatically .

Thanks In Advance,

Joseph.C

_________________
JosephC


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 12:57 am 
Newbie

Joined: Thu Jan 18, 2007 12:55 am
Posts: 10
are you using thread local session ,
your session factory should be built once during the lifetime of the application
are you using singelton for session factory creationg
if you are creating session factory for every session, this may cause it.

could you post the stack trace


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 23, 2007 4:57 am 
Beginner
Beginner

Joined: Tue Sep 19, 2006 5:46 am
Posts: 21
Location: india
I am using a singleton SessionFactory..
here is the sample

private static SessionFactory sessionFactory;
private Map Cache;

public SessionFactory getSessionFactory() {

if(sessionFactory==null){
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
Cache.put("SessionFactory",sessionFactory);
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
}
}
return sessionFactory;
}

_________________
JosephC


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 23, 2007 6:24 pm 
Newbie

Joined: Thu Jan 18, 2007 12:55 am
Posts: 10
Is your Hibernat session a thread local session. Session is retrieved using
sessionfactory.getCurrentSession() or openSession() so if your session is not put in a thread local state there will be a new session for every session you get until you close it. Could you tell me how are you retrieving your session and starting your transaction

do
Some thing like this

private static final ThreadLocal threadLocal = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {

sessionFactory = getSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

I would recommend not to have public method to get SessionFactory because it is only built once for the lifetime of an application, just keep the session retrieving method public.

and you can also have one method to close the thread local session like
this

public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

I hope this helps


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 27, 2007 1:44 am 
Beginner
Beginner

Joined: Tue Sep 19, 2006 5:46 am
Posts: 21
Location: india
I am getting the session like this. see that I am not closing the session after transaction . Will that be the cause for the issue..? will I have to close it explicitly.


//we are getting the sessionFactory

private final SessionFactory sessionFactory = getSessionFactory();


public SessionFactory getSessionFactory() {

HibernateUtils hibernateUtils = HibernateUtils.getInstance();
return hibernateUtils.getSessionFactory();
}


// getting session from sessionFactory


public void updateFileValue(FileReportValue persistentInstance) {
log.debug("update FileReportValue instance");
try {
sessionFactory.getCurrentSession().beginTransaction();
sessionFactory.getCurrentSession().update(persistentInstance);
sessionFactory.getCurrentSession().getTransaction().commit();
log.debug("update successful");
} catch (RuntimeException re) {
sessionFactory.getCurrentSession().getTransaction().rollback();
log.error("update failed", re);
throw re;
}
}

_________________
JosephC


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.