Has anyone every experienced their session factory (I happen to use PostgreSQL) get hosed when a bad SQL throws an error. It seems if I have a typo or otherwise invalid SQL it throws an error and all my connections are placed in an error state.
If I restart the session factory, i.e. pool it fixes it. I swear I am ending my transactions properly but... here is my query code and HibernateUtil
public static List sqlQuery(String sql, ServiceCall call) throws Exception {
try {
List qrylist = null;
org.hibernate.Session session = HibernateUtil.getNewSession();
try {
Query query = session.createSQLQuery(sql);
qrylist = query.list();
session.clear();
} catch (Exception ex) {
Log.error(getDebug(sql, qualifications));
HibernateUtil.abort();
throw ex;
} finally {
session.close();
}
return qrylist;
} catch (Exception ex) {
throw ex;
}
}
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
private static Configuration configuration = null;
private volatile static Object lock = new Object();
static {
buildSessionFactory();
}
private static synchronized void buildSessionFactory() {
try {
if (configuration == null) {
synchronized (lock) {
if (configuration == null) {
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}
}
}
} catch (Throwable ex) {
ex.printStackTrace(System.err);
Log.fatal(ex);
}
}
private static SessionFactory getSessionFactory() throws Exception {
if (sessionFactory == null) {
throw new NullPointerException();
} else {
return sessionFactory;
}
}
public static Session getNewSession() throws Exception {
try {
return getSessionFactory().openSession();
} catch (Exception ex) {
throw ex;
}
}
public static synchronized void abort() throws Exception {
try {
Log.error("abort!");
System.exit(1);
getSessionFactory().getCurrentSession().close();
sessionFactory = configuration.buildSessionFactory();
} catch (Exception ex) {
throw ex;
}
}
|