srossouw wrote:
get a "too many connections" error.
Ah, OK, so it looks like you are creating a new connection each time rather than re-using old ones or something.
For a starter are you using connection pools at all ? How is hibernate obtaining the JDBC connection ? Can you post a copy of either hibernate.cfg.xml or applicationContext.xml (the spring config file)
Here's a good article that expands on how to get Spring and Hibernate working together
http://www-128.ibm.com/developerworks/w ... a-spring2/
Your code does look a bit odd in that it seems to be loading hte hibernate.cfg.xml everytime you do a query. This should be a one off task for the duration of the application. Perhaps the fact that you are doing this is contributing to you using up your connections.
If you want to continue to use Hibernate without Spring (which is what your code seems to be doing), then I suggest you look into building your session factory once rather than every time as well (does your code run slowly by any chance ?). Here's some example code:
public class HibernateUtil {
static {
try {
new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
/**
* Get session factory
* @param sessionFactoryJndiName factory name
* @return the Session Factory
* @throws NamingException cannot find a factory with the name
*/
public static SessionFactory getSessionFactory(String sessionFactoryJndiName)
throws NamingException {
SessionFactory sessionFactory = null;
Context ctx = new InitialContext();
sessionFactory = (SessionFactory) ctx.lookup(sessionFactoryJndiName);
return sessionFactory;
}
}