List of usage examples for org.hibernate FlushMode MANUAL
FlushMode MANUAL
To view the source code for org.hibernate FlushMode MANUAL.
Click Source Link
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
License:Apache License
/** * Close the given Session or register it for deferred close. * @param session the Hibernate Session to close * @param sessionFactory Hibernate SessionFactory that the Session was created with * (may be {@code null})//from w w w .ja v a 2 s .c o m * @see #initDeferredClose * @see #processDeferredClose */ static void closeSessionOrRegisterDeferredClose(Session session, SessionFactory sessionFactory) { Map<SessionFactory, Set<Session>> holderMap = deferredCloseHolder.get(); if (holderMap != null && sessionFactory != null && holderMap.containsKey(sessionFactory)) { logger.debug("Registering Hibernate Session for deferred close"); // Switch Session to FlushMode.MANUAL for remaining lifetime. session.setFlushMode(FlushMode.MANUAL); Set<Session> sessions = holderMap.get(sessionFactory); sessions.add(session); } else { closeSession(session); } }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInterceptor.java
License:Apache License
/** * Open a Session for the SessionFactory that this interceptor uses. * <p>The default implementation delegates to the {@link SessionFactory#openSession} * method and sets the {@link Session}'s flush mode to "MANUAL". * @return the Session to use// w ww.jav a2 s. c o m * @throws DataAccessResourceFailureException if the Session could not be created * @see org.hibernate.FlushMode#MANUAL */ protected Session openSession() throws DataAccessResourceFailureException { try { Session session = getSessionFactory().openSession(); session.setFlushMode(FlushMode.MANUAL); return session; } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInViewTests.java
License:Apache License
@Test public void testOpenSessionInterceptor() throws Exception { final SessionFactory sf = mock(SessionFactory.class); final Session session = mock(Session.class); OpenSessionInterceptor interceptor = new OpenSessionInterceptor(); interceptor.setSessionFactory(sf);//from w ww . j a v a 2 s.c om Runnable tb = new Runnable() { @Override public void run() { assertTrue(TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, SessionFactoryUtils.getSession(sf, false)); } }; ProxyFactory pf = new ProxyFactory(tb); pf.addAdvice(interceptor); Runnable tbProxy = (Runnable) pf.getProxy(); given(sf.openSession()).willReturn(session); given(session.isOpen()).willReturn(true); tbProxy.run(); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInViewTests.java
License:Apache License
@Test public void testOpenSessionInViewInterceptorWithSingleSession() throws Exception { SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(sf);/*ww w . j a v a 2 s . c o m*/ given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); given(session.getSessionFactory()).willReturn(sf); given(session.isOpen()).willReturn(true); interceptor.preHandle(this.webRequest); assertTrue(TransactionSynchronizationManager.hasResource(sf)); // check that further invocations simply participate interceptor.preHandle(this.webRequest); assertEquals(session, SessionFactoryUtils.getSession(sf, false)); interceptor.preHandle(this.webRequest); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.preHandle(this.webRequest); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.postHandle(this.webRequest, null); assertTrue(TransactionSynchronizationManager.hasResource(sf)); interceptor.afterCompletion(this.webRequest, null); assertFalse(TransactionSynchronizationManager.hasResource(sf)); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInViewTests.java
License:Apache License
@Test public void testOpenSessionInViewInterceptorAsyncScenario() throws Exception { // Initial request thread final SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(sf);/* ww w .j a va 2 s. com*/ given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); interceptor.preHandle(this.webRequest); assertTrue(TransactionSynchronizationManager.hasResource(sf)); AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response); WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request); asyncManager.setTaskExecutor(new SyncTaskExecutor()); asyncManager.setAsyncWebRequest(asyncWebRequest); asyncManager.startCallableProcessing(new Callable<String>() { @Override public String call() throws Exception { return "anything"; } }); interceptor.afterConcurrentHandlingStarted(this.webRequest); assertFalse(TransactionSynchronizationManager.hasResource(sf)); // Async dispatch thread interceptor.preHandle(this.webRequest); assertTrue("Session not bound to async thread", TransactionSynchronizationManager.hasResource(sf)); interceptor.postHandle(this.webRequest, null); assertTrue(TransactionSynchronizationManager.hasResource(sf)); verify(session, never()).close(); interceptor.afterCompletion(this.webRequest, null); assertFalse(TransactionSynchronizationManager.hasResource(sf)); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInViewTests.java
License:Apache License
@Test public void testOpenSessionInViewInterceptorWithSingleSessionAndJtaTm() throws Exception { final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); Session session = mock(Session.class); TransactionManager tm = mock(TransactionManager.class); given(tm.getTransaction()).willReturn(null); given(tm.getTransaction()).willReturn(null); OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(sf);/*w w w .j av a 2 s. c om*/ given(sf.openSession()).willReturn(session); given(sf.getTransactionManager()).willReturn(tm); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); interceptor.preHandle(this.webRequest); assertTrue(TransactionSynchronizationManager.hasResource(sf)); // Check that further invocations simply participate interceptor.preHandle(this.webRequest); assertEquals(session, SessionFactoryUtils.getSession(sf, false)); interceptor.preHandle(this.webRequest); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.preHandle(this.webRequest); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.postHandle(this.webRequest, null); assertTrue(TransactionSynchronizationManager.hasResource(sf)); interceptor.afterCompletion(this.webRequest, null); assertFalse(TransactionSynchronizationManager.hasResource(sf)); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInViewTests.java
License:Apache License
@Test public void testOpenSessionInViewInterceptorAndDeferredClose() throws Exception { SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(sf);/*from www. j a v a 2 s . c o m*/ interceptor.setSingleSession(false); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); interceptor.preHandle(this.webRequest); org.hibernate.Session sess = SessionFactoryUtils.getSession(sf, true); SessionFactoryUtils.releaseSession(sess, sf); // check that further invocations simply participate interceptor.preHandle(this.webRequest); interceptor.preHandle(this.webRequest); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.preHandle(this.webRequest); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInViewTests.java
License:Apache License
@Test public void testOpenSessionInViewFilterWithSingleSession() throws Exception { final SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); given(session.close()).willReturn(null); final SessionFactory sf2 = mock(SessionFactory.class); Session session2 = mock(Session.class); given(sf2.openSession()).willReturn(session2); given(session2.getSessionFactory()).willReturn(sf2); given(session2.close()).willReturn(null); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc);// w w w. j av a 2s .c o m wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); wac.getDefaultListableBeanFactory().registerSingleton("mySessionFactory", sf2); wac.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory"); filterConfig2.addInitParameter("flushMode", "AUTO"); final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); filter.init(filterConfig); final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter(); filter2.init(filterConfig2); final FilterChain filterChain = new FilterChain() { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { assertTrue(TransactionSynchronizationManager.hasResource(sf)); servletRequest.setAttribute("invoked", Boolean.TRUE); } }; final FilterChain filterChain2 = new FilterChain() { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException { assertTrue(TransactionSynchronizationManager.hasResource(sf2)); filter.doFilter(servletRequest, servletResponse, filterChain); } }; FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2); assertFalse(TransactionSynchronizationManager.hasResource(sf)); assertFalse(TransactionSynchronizationManager.hasResource(sf2)); filter2.doFilter(this.request, this.response, filterChain3); assertFalse(TransactionSynchronizationManager.hasResource(sf)); assertFalse(TransactionSynchronizationManager.hasResource(sf2)); assertNotNull(this.request.getAttribute("invoked")); verify(session).setFlushMode(FlushMode.MANUAL); verify(session2).setFlushMode(FlushMode.AUTO); wac.close(); }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInViewTests.java
License:Apache License
@Test public void testOpenSessionInViewFilterAsyncScenario() throws Exception { final SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); // Initial request during which concurrent handling starts.. given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc);//from ww w.j av a 2s .c o m wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); wac.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); final AtomicInteger count = new AtomicInteger(0); final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); filter.init(filterConfig); final FilterChain filterChain = new FilterChain() { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { assertTrue(TransactionSynchronizationManager.hasResource(sf)); count.incrementAndGet(); } }; AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response); WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request); asyncManager.setTaskExecutor(new SyncTaskExecutor()); asyncManager.setAsyncWebRequest(asyncWebRequest); asyncManager.startCallableProcessing(new Callable<String>() { @Override public String call() throws Exception { return "anything"; } }); assertFalse(TransactionSynchronizationManager.hasResource(sf)); filter.doFilter(this.request, this.response, filterChain); assertFalse(TransactionSynchronizationManager.hasResource(sf)); assertEquals(1, count.get()); verify(session, never()).close(); // Async dispatch after concurrent handling produces result ... this.request.setAsyncStarted(false); assertFalse(TransactionSynchronizationManager.hasResource(sf)); filter.doFilter(this.request, this.response, filterChain); assertFalse(TransactionSynchronizationManager.hasResource(sf)); assertEquals(2, count.get()); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); wac.close(); }
From source file:org.springframework.orm.hibernate3.support.OpenSessionInViewTests.java
License:Apache License
@Test public void testOpenSessionInViewFilterWithSingleSessionAndPreBoundSession() throws Exception { final SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc);//from ww w . jav a 2 s.c o m wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); wac.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory"); OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(sf); interceptor.preHandle(this.webRequest); final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); filter.init(filterConfig); final FilterChain filterChain = new FilterChain() { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { assertTrue(TransactionSynchronizationManager.hasResource(sf)); servletRequest.setAttribute("invoked", Boolean.TRUE); } }; assertTrue(TransactionSynchronizationManager.hasResource(sf)); filter.doFilter(this.request, this.response, filterChain); assertTrue(TransactionSynchronizationManager.hasResource(sf)); assertNotNull(this.request.getAttribute("invoked")); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); wac.close(); }