hi all,
in my app i have created a Persistence layer which abstract the way i persist objects (i use both castor and hibernate).
When running the persistence layer within a J2EE app (and in an application server) everything works perfectly fine.'
When, however, i am running my tests 'off-line' in Maven, whenever i call a PersistenceManager operation, i got an exception that the Session is disconnected.
My wild guess is because the SessionFactory should be bound to a JNDI name...
however, when i run tests NOT using PersistenceManager, session works just fine.
Can anyone tell me what is wrong?
thanx in advance and regards
marco
here is my PersistenceManager code...
/**
* Constructor
*/
HibernatePersistenceManager() throws Exception{
_log.debug("Creating HibernatePM..");
initHibernate();
}
/**
* Initializes Hibernate with the config file found at
* <code>configFilePath</code>.
*/
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(data);
t.commit();
s.close();
} catch(Exception e) {
_log.error("Exception in creating CastorEntry\n" + e);
throw new PersistenceException(e);
}
}
public void update(Entry data) throws PersistenceException{
try {
Session s = openSession();
HibernateEntry entry = (HibernateEntry)data;
Transaction t = s.beginTransaction();
s.saveOrUpdate(entry);
t.commit();
s.close();
} catch(Exception e) {
_log.error("Exception in creating CastorEntry\n" + e);
throw new PersistenceException(e);
}
}
public void delete(Entry data) throws PersistenceException{
try {
Session s = openSession();
HibernateEntry entry = (HibernateEntry)data;
Transaction t = s.beginTransaction();
s.delete(entry);
t.commit();
s.close();
} catch(Exception e) {
_log.error("Exception in creating CastorEntry\n" + e);
throw new PersistenceException(e);
}
}
public Collection query(String query, Object[] paramValues) throws PersistenceException{
Collection returnResults = new Vector();
Query entryQuery;
List results = new ArrayList();
try {
Session s = openSession();
_log.debug("----HibPersMgr. getting query...");
entryQuery = s.getNamedQuery(query);
if(paramValues != null) {
for(int i=0; i < paramValues.length; i++){
entryQuery.setParameter(i,paramValues[i]);
}
}
_log.debug("---- obtained query" + query);
results = entryQuery.list();
_log.debug("---- query finished. got:" + results.size() + " elements!");
// To be done
} catch(Exception e) {
_log.error("Exception in querying HibernateEntry\n" + e);
throw new PersistenceException(e);
}
return results;
}
|