public void testInterceptorWithThreadBoundAndFlushAlways() { MockControl sfControl = MockControl.createControl(SessionFactory.class); SessionFactory sf = (SessionFactory) sfControl.getMock(); MockControl sessionControl = MockControl.createControl(Session.class); Session session = (Session) sessionControl.getMock(); session.getSessionFactory(); sessionControl.setReturnValue(sf, 1); session.isOpen(); sessionControl.setReturnValue(true, 1); session.getFlushMode(); sessionControl.setReturnValue(FlushMode.AUTO, 1); session.setFlushMode(FlushMode.ALWAYS); sessionControl.setVoidCallable(1); session.setFlushMode(FlushMode.AUTO); sessionControl.setVoidCallable(1); sfControl.replay(); sessionControl.replay(); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setSessionFactory(sf); interceptor.setFlushMode(HibernateInterceptor.FLUSH_ALWAYS); try { interceptor.invoke(new TestInvocation(sf)); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } finally { TransactionSynchronizationManager.unbindResource(sf); } sfControl.verify(); sessionControl.verify(); }
@Override public void validate(Object obj, Errors errors, boolean cascade) { final Session session = sessionFactory.getCurrentSession(); FlushMode previousMode = null; try { if (session != null) { previousMode = session.getFlushMode(); session.setFlushMode(FlushMode.MANUAL); } super.validate(obj, errors, cascade); } finally { if (session != null && previousMode != null) { session.setFlushMode(previousMode); } } }