Hi, I just got hibernate working but I have a problem. It only works for a while then I get the "Cannot open connection".
I can only assume that the sessions are not closing and finally it gets too many?
I made a simplified example to illustrate.
Code:
public class HQLGroupByExample {
public static void main(String[] args) {
for (int i=0; i<125; i++){
func();
}
}
public static void func(){
Session session = null;
try {
// This step will read hibernate.cfg.xml and prepare hibernate for
// use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
//Group By Clause Example
Transaction tx = session.beginTransaction();
String SQL_QUERY = "select sum(insurance.investementAmount),insurance.insuranceName "
+ "from Insurance insurance group by insurance.insuranceName";
Query query = session.createQuery(SQL_QUERY);
for (Iterator it = query.iterate(); it.hasNext();) {
Object[] row = (Object[]) it.next();
// System.out.println("Invested Amount: " + row[0]);
System.out.println("Insurance Name: " + row[1]);
}
tx.commit();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.close();
}
}
The loop works for maybe a hundred runs or so and then it doesn't.
Anyone knows how to make it work?