Hello forum, I'm developing a web application with hibernate as persistence technology.
I have HibernateUtil.java in order to obtain sessions -->
Code:
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure("./hibernate/hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
and I have StatusDAO.java that gives support to database operations -->
Code:
...
public void persist(Status transientInstance) {
log.debug("persisting Status instance");
try {
System.out.println("1");
org.hibernate.Session s = HibernateUtil.currentSession();
System.out.println("2");
org.hibernate.Transaction tx = s.beginTransaction();
System.out.println("3");
s.save(transientInstance);
System.out.println("4");
tx.commit();
System.out.println("5");
s.flush();
System.out.println("6");
HibernateUtil.closeSession();
System.out.println("7");
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
...
And to save one status object -->
Code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
int action = Integer.parseInt(request.getParameter("action"));
RequestDispatcher dispatcher = null;
switch (action) {
case com.gmsoft.enumerates.ActionsEnum.NEW_STATUS:
String descripcio = request.getParameter("descripcio");
com.gmsoft.model.Status status = new Status(60,descripcio);
com.gmsoft.model.StatusDAO statusDAO = new com.gmsoft.model.StatusDAO();
statusDAO.persist(status);
dispatcher = getServletContext().getRequestDispatcher(com.gmsoft.webStructure.Directories.ADMIN + "admin.jsp");
break;
case com.gmsoft.enumerates.ActionsEnum.MODIFY_STATUS:
break;
case com.gmsoft.enumerates.ActionsEnum.DELETE_STATUS:
break;
}
if (dispatcher == null) {
dispatcher = getServletContext().getRequestDispatcher(com.gmsoft.webStructure.Directories.ERRORS + "general.jsp");
}else{
dispatcher.forward(request, response);
}
}catch (Exception ex) {
response.getWriter().println(ex.getMessage());
ex.printStackTrace(response.getWriter());
}
}
My problem is -->
the first time that I execute doPostmethod of my servlet it works correctly, so, I have saved one status instance. However if I execute doPost method once again, the transaction doesn't commit.
At first time the out result is -->
1
2
3
4
5
6
7
and at the second time the out result is -->
1
2
3
4
Can you say me why commit method doesn't work correctly?
Thanks in advance.