-->
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.  [ 1 post ] 
Author Message
 Post subject: Is this proper use of Hibernate in a J2EE app?
PostPosted: Wed May 05, 2004 5:24 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:31 am
Posts: 42
hi all,
i am about to use Hibernate as persistence layer for my application.
My app is a Struts-based application which is made of a set of Action class.
There is a 'Persistence Layer' in my app, represented by a PersistenceManager interface, which has ServletContext scope (only 1 instance will be created during whole application).
My concern were Hibernate Session handling... so i wanted to post my code to see if it was a proper approach in a J2EE application..
I am using ThreadLocal inside my PersistenceManager, and i am opening and closing session every time the PersistenceManager is used to insert/delete/update/or retrieve data..
can anyone tell me if there's something missing or something to be modified?

thanx in advance and regards
marco

Here is the code

class HibernatePersistenceManager implements PersistenceManager {

private String _configFilePath = "/hibernate.cfg.xml";
private SessionFactory _factory = null;
private static final ThreadLocal session = new ThreadLocal();

HibernatePersistenceManager() throws Exception{
_log.debug("Creating HibernatePM..");

}

private void initHibernate() throws Exception {
Configuration configuration = null;
URL configFileURL = null;
ServletContext context = null;

try {
configFileURL = HibernatePersistenceManager.class.getResource(_configFilePath);
_log.debug("Initializing Hibernate from "
+ _configFilePath + "...");


configuration = (new Configuration()).configure(configFileURL);
_factory = configuration.buildSessionFactory();

_log.debug("Simple Test....creatingp ersistent expense.....");
} catch (Throwable t) {
_log.error("Exception while initializing Hibernate.");
_log.error("Rethrowing exception...", t);
throw (new Exception(t));
}
}


public void insert(Entry data) throws PersistenceException {
try {
Session s = openSession();
HibernateEntry entry = (HibernateEntry)data;
Transaction t = s.beginTransaction();
s.save(entry);
t.commit();
closeSessions();
} catch(Exception e) {
_log.error("Exception in creating CastorEntry\n" + e);
throw new PersistenceException(e);
}
}


/**
* @return an Hibernate Session
*/
Session openSession() throws Exception {
Session s = (Session)session.get();
if(s== null) {
s = _factory.openSession();
session.set(s);
}
return s;
}

/**
* Closes an Hibernate Session
*/
void closeSession() throws Exception {
Session s = (Session)session.get();
session.set(null);
if(s != null)
s.close();
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.