项目中遇到了一个问题,明明都调用了hibernateTemplate.flush可是还是没有保存数据,看了hibernateTemplate.flush的源代码:
public void flush()
throws DataAccessException
{
/* 786*/ execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException
{
/* 788*/ session.flush();
/* 789*/ return null;
}
}
的确调用了session.flush();
这样的话问题不是在spring 方面,继续看session.flush();
public void flush() throws HibernateException { /* 978*/ errorIfClosed(); /* 979*/ checkTransactionSynchStatus(); /* 980*/ if(persistenceContext.getCascadeLevel() > 0) /* 981*/ throw new HibernateException("Flush during cascade is dangerous"); /* 983*/ FlushEventListener flushEventListener[] = listeners.getFlushEventListeners(); /* 984*/ for(int i = 0; i < flushEventListener.length; i++) /* 985*/ flushEventListener[i].onFlush(new FlushEvent(this)); }
发现其中有句话:checkTransactionSynchStatus();
这个方法打印出来的log是:successfully registered Synchronization
我的推断是在有事务的时候只有注册这个改变而没有真正的去数据库执行sql,在事务提交的时候才会真正的提交