ninjapower, I tried the code you posted but I still get the same result, meaning the save gets committed even though is not the end of the conversation (no flush() called).
Thanks for the tip about the articles, but believe me I have read them many times and I have gone through the whole caveatemptor sample.
I used your ThreadLocalSessionContextNoAutoClose.java class.
I also used the following filter
Code:
public class ThreadLocalSessionContextNoAutoCloseFilter implements Filter
{
private static Log log = LogFactory
.getLog(ThreadLocalSessionContextNoAutoCloseFilter.class);
private SessionFactory sf;
public static final String END_OF_CONVERSATION_FLAG = "end_of_conversation";
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
try
{
log.debug("Starting a database transaction");
sf.getCurrentSession().beginTransaction();
// Do the work...
chain.doFilter(request, response);
// End or continue the long-running conversation?
if (request.getAttribute(END_OF_CONVERSATION_FLAG) != null)
{
log.debug("Flushing Session");
sf.getCurrentSession().flush();
log.debug("Committing the database transaction");
sf.getCurrentSession().getTransaction().commit();
log.debug("<<< End of conversation");
}
else
{
log.debug("Committing database transaction");
sf.getCurrentSession().getTransaction();
sf.getCurrentSession().getTransaction().commit();
log.debug("> Returning to user in conversation");
}
}
catch (StaleObjectStateException staleEx)
{
log
.error("This interceptor does not implement optimistic concurrency control!");
log
.error("Your application will not work until you add compensation actions!");
throw staleEx;
}
catch (Throwable ex)
{
// Rollback only
try
{
if (sf.getCurrentSession().getTransaction().isActive())
{
log.debug("Trying to rollback database transaction after exception");
sf.getCurrentSession().getTransaction().rollback();
}
}
catch (Throwable rbEx)
{
log.error("Could not rollback transaction after exception!", rbEx);
}
finally
{
log.error("Cleanup after exception!");
// Cleanup
log.debug("Closing and unbinding Session from thread");
sf.getCurrentSession().close(); // Unbind is automatic here
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
}
}
public void init(FilterConfig filterConfig) throws ServletException
{
log
.debug("Initializing filter, obtaining Hibernate SessionFactory from HibernateUtil");
sf = HibernateUtil.getSessionFactory();
}
public void destroy()
{
}
}
and this is the configuration I am using
Code:
<property name="current_session_context_class">ThreadLocalSessionContextNoAutoClose</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
as I debug, the insert query in the console shows up right after the save command.
Any details you can think of ninjapower, that I may be missing? (Yes I am using getCurrentSession()