I did read
http://hibernate.org/42.html
But I still have questions...
I created the servlet filter to open a session when I receive a webservice request. This filter is opening and closing the session. I don't get Lazy initialization problem anymore.
To close the session I'm calling the disconnect method instead of close because if I call close method I get an exception that is kind of the same problem as the lazy initialization since the session is closed.
Code:
public void commit(Object transaction) {
Transaction t = (Transaction) transaction;
commit(t);
}
public void commit(Transaction transaction) {
try {
if (transaction != null && !transaction.wasCommitted()
&& !transaction.wasRolledBack()) {
transaction.commit();
transaction = null;
}
} catch (Exception e) {
if (transaction != null) {
try {
rollback(transaction);
} catch (HibernateException he) {
}
}
e.printStackTrace();
}
}
public Object start(Object session) {
Session s = (Session) session;
return startTrans(s);
}
public Transaction startTrans(Session session) {
return session.beginTransaction();
}
public Session openSession() {
return sessionFactory.openSession();
//return sessionFactory.getCurrentSession();
}
public void close(Session session) {
if (session != null) {
try {
session.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public boolean find(EntityObject entityObj){
boolean achou = false;
Session session = openSession(entityObj);
Transaction transaction = startTrans(session);
try {
EntityId id = new EntityId(entityObj);
Serializable idObj=id.getKey();
session.load(entityObj, idObj);
commit(transaction);
achou = true;
} catch (Exception ex) {
//System.out.println(ex.getMessage());
ex.printStackTrace();
rollback(transaction);
} finally {
close(session);
}
return achou;
}
This code is not closing the connections to the database, the pool size is 5 and after few minutes of webservice calls I get about 80 connections. The DriverManagerConnectionProvider show the following information:
14:48:46,437 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 77
14:48:46,437 DEBUG DriverManagerConnectionProvider:99 - using pooled JDBC connection, pool size: 0
14:48:46,515 DEBUG DriverManagerConnectionProvider:129 - returning connection to pool, pool size: 1
Basically I want to know when I should close the session. Should I really close it all? And why the connections are not being closed?
EDIT: If i use getCurrentSession() from the SessionFactory will it be the same session I opened on my servletfilter?
EDIT2: I'm calling session.disconnect() (returning null) but session.isConnected() is still returning true