Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 2.1.8
Code between sessionFactory.openSession() and session.close():
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
DynaActionForm dynaForm = (DynaActionForm) form;
Transaction tx = null;
HibernateUtil util = null;
Session session = null;
ActionUtil aUtil = ActionUtil.getInstance();
try {
ViewBean bean = new ViewBean();
String author = (String) dynaForm.get(Constants.AUTHOR);
String title = (String) dynaForm.get(Constants.COMMENT_TITLE);
String comment = (String) dynaForm.get(Constants.COMMENT);
String categoryId = (String) dynaForm.get(Constants.CATEGORY);
String excluded = (String)dynaForm.get(Constants.EXCLUDED);
Collection products = aUtil
.getArrayToCollection((String[]) dynaForm
.get(Constants.PRODUCT));
Collection platforms = aUtil
.getArrayToCollection((String[]) dynaForm
.get(Constants.PRODUCT));
aUtil.setError(author, "Author is required", errors);
aUtil.setError(title, "Comment title is required", errors);
aUtil.setError(comment, "Comment is required", errors);
if (!errors.isEmpty()) {
saveErrors(request, errors);
return (mapping.findForward(Constants.FAILURE1));
}
Release newRelease = new Release();
newRelease.setAuthor(author);
newRelease.setComment(comment);
newRelease.setTitle(title);
if(excluded == null || excluded.trim().length() == 0)
newRelease.setExcluded(false);
else
newRelease.setExcluded(true);
util = HibernateUtil.getInstance();
session = util.getSession();
tx = util.startTransaction(session);
CategoryDAO cDAO = new CategoryDAO();
PlatformDAO platDAO = new PlatformDAO();
newRelease.setPlatforms(platDAO.getPlatformsByIds(platforms,session));
ProductDAO prodDAO = new ProductDAO();
newRelease.setProducts(prodDAO.getProductsByIds(products,session));
newRelease.setCreated(new Timestamp(System.currentTimeMillis()));
Category rCategory = cDAO.addReleaseToCategoryById(Long.parseLong(categoryId),newRelease,session);
bean.setReleaseCategoryName(rCategory.getName());
bean.setReleaseCategoryId(rCategory.getId());
bean.setReleaseComponentName(rCategory.getParentCategory().getName());
Collection release = new Vector();
release.add(newRelease);
bean.setRelease(release);
util.commitTransaction(tx);
util.closeSession(session);
request.setAttribute(Constants.REQUEST_BEAN, bean);
} catch (Exception e) {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
"message.detail", e.getMessage()));
saveErrors(request, errors);
util.rollbackTransaction(tx);
util.closeSession(session); return (mapping.findForward(Constants.FAILURE1));
}
return (mapping.findForward(Constants.SUCCESS1));
}//end save
Name and version of the database you are using: DB2 8.2
I do not know why I get the warning net.sf.hibernate.impl.SessionImpl
unclosed connection, forgot to call close() on your session? , I am closing the session. In my close session method, I have tried to different variations but I get the same warning. Here are the two versions I have used:
public void closeSession(Session s) {
try {
if (s != null){
if(s.connection() != null)
s.connection().close();
s.close();
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
} catch (SQLException e) {
throw new InfrastructureException(e);
}
}
and
public void closeSession(Session s) {
try {
if (s != null){
s.close();
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
} catch (SQLException e) {
throw new InfrastructureException(e);
}
}
I know the warning is outputed because the hibernate is doing the clean up for me. However; I am closing all the sessions, it bewilder me why? Anyone can give me a hint.
regards,