protected SessionBuilder baseSessionBuilder() { final SessionBuilder builder = factory.withOptions(); final CurrentTenantIdentifierResolver resolver = factory.getCurrentTenantIdentifierResolver(); if ( resolver != null ) { builder.tenantIdentifier( resolver.resolveCurrentTenantIdentifier() ); } return builder; }
@Test public void testTransactionCommitWithEntityInterceptor() throws Exception { Interceptor entityInterceptor = mock(Interceptor.class); Connection con = mock(Connection.class); final SessionFactory sf = mock(SessionFactory.class); ImplementingSession session = mock(ImplementingSession.class); SessionBuilder options = mock(SessionBuilder.class); Transaction tx = mock(Transaction.class); given(sf.withOptions()).willReturn(options); given(options.interceptor(entityInterceptor)).willReturn(options); given(options.openSession()).willReturn(session); given(session.beginTransaction()).willReturn(tx); given(session.isOpen()).willReturn(true); given(session.isConnected()).willReturn(true); given(session.connection()).willReturn(con); HibernateTransactionManager tm = new HibernateTransactionManager(sf); tm.setEntityInterceptor(entityInterceptor); tm.setAllowResultAccessAfterCompletion(true); TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); } }); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session).close(); verify(tx).commit(); }
@Test public void testTransactionCommitWithEntityInterceptor() throws Exception { Interceptor entityInterceptor = mock(Interceptor.class); Connection con = mock(Connection.class); final SessionFactory sf = mock(SessionFactory.class); ImplementingSession session = mock(ImplementingSession.class); SessionBuilder options = mock(SessionBuilder.class); Transaction tx = mock(Transaction.class); given(sf.withOptions()).willReturn(options); given(options.interceptor(entityInterceptor)).willReturn(options); given(options.openSession()).willReturn(session); given(session.beginTransaction()).willReturn(tx); given(session.isOpen()).willReturn(true); given(session.isConnected()).willReturn(true); given(session.connection()).willReturn(con); HibernateTransactionManager tm = new HibernateTransactionManager(sf); tm.setEntityInterceptor(entityInterceptor); TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); Object result = tt.execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); } }); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session).close(); verify(tx).commit(); }
@Override public SessionBuilder clearEventListeners() { super.clearEventListeners(); return this; }
@Override public SessionBuilder owner(SessionOwner sessionOwner) { this.sessionOwner = sessionOwner; return this; }
@Override public SessionBuilder noInterceptor() { this.interceptor = EmptyInterceptor.INSTANCE; return this; }
@Override public SessionBuilder eventListeners(SessionEventListener... listeners) { Collections.addAll( this.listeners, listeners ); return this; }
@Override public SessionBuilder clearEventListeners() { listeners.clear(); return this; }
@Test public void testTransactionCommitWithEntityInterceptorBeanName() throws Exception { Interceptor entityInterceptor = mock(Interceptor.class); Interceptor entityInterceptor2 = mock(Interceptor.class); Connection con = mock(Connection.class); final SessionFactory sf = mock(SessionFactory.class); ImplementingSession session = mock(ImplementingSession.class); SessionBuilder options = mock(SessionBuilder.class); Transaction tx = mock(Transaction.class); given(sf.withOptions()).willReturn(options); given(options.interceptor(entityInterceptor)).willReturn(options); given(options.interceptor(entityInterceptor2)).willReturn(options); given(options.openSession()).willReturn(session); given(session.beginTransaction()).willReturn(tx); given(session.isOpen()).willReturn(true); given(session.isConnected()).willReturn(true); given(session.connection()).willReturn(con); BeanFactory beanFactory = mock(BeanFactory.class); given(beanFactory.getBean("entityInterceptor", Interceptor.class)).willReturn( entityInterceptor, entityInterceptor2); HibernateTransactionManager tm = new HibernateTransactionManager(sf); tm.setEntityInterceptorBeanName("entityInterceptor"); tm.setBeanFactory(beanFactory); TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); for (int i = 0; i < 2; i++) { tt.execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); } }); } assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session, times(2)).close(); verify(tx, times(2)).commit(); }
/** {@inheritDoc} */ @Override public SessionBuilder withOptions() { return null; }
/** * Adds the session owner to the session options * * @param sessionOwner The session owner. * * @return {@code this}, for method chaining */ public SessionBuilder owner(SessionOwner sessionOwner);