Java Code Examples for org.springframework.orm.hibernate5.SessionFactoryUtils#closeSession()

The following examples show how to use org.springframework.orm.hibernate5.SessionFactoryUtils#closeSession() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenSessionInterceptor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	SessionFactory sf = getSessionFactory();
	Assert.state(sf != null, "No SessionFactory set");

	if (!TransactionSynchronizationManager.hasResource(sf)) {
		// New Session to be bound for the current method's scope...
		Session session = openSession(sf);
		try {
			TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
			return invocation.proceed();
		}
		finally {
			SessionFactoryUtils.closeSession(session);
			TransactionSynchronizationManager.unbindResource(sf);
		}
	}
	else {
		// Pre-bound Session found -> simply proceed.
		return invocation.proceed();
	}
}
 
Example 2
Source File: OpenSessionInterceptor.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	SessionFactory sf = getSessionFactory();
	Assert.state(sf != null, "No SessionFactory set");

	if (!TransactionSynchronizationManager.hasResource(sf)) {
		// New Session to be bound for the current method's scope...
		Session session = openSession(sf);
		try {
			TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
			return invocation.proceed();
		}
		finally {
			SessionFactoryUtils.closeSession(session);
			TransactionSynchronizationManager.unbindResource(sf);
		}
	}
	else {
		// Pre-bound Session found -> simply proceed.
		return invocation.proceed();
	}
}
 
Example 3
Source File: OpenSessionInterceptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	SessionFactory sf = getSessionFactory();
	if (!TransactionSynchronizationManager.hasResource(sf)) {
		// New Session to be bound for the current method's scope...
		Session session = openSession();
		try {
			TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
			return invocation.proceed();
		}
		finally {
			SessionFactoryUtils.closeSession(session);
			TransactionSynchronizationManager.unbindResource(sf);
		}
	}
	else {
		// Pre-bound Session found -> simply proceed.
		return invocation.proceed();
	}
}
 
Example 4
Source File: OpenSessionInterceptor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	SessionFactory sf = getSessionFactory();
	if (!TransactionSynchronizationManager.hasResource(sf)) {
		// New Session to be bound for the current method's scope...
		Session session = openSession();
		try {
			TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
			return invocation.proceed();
		}
		finally {
			SessionFactoryUtils.closeSession(session);
			TransactionSynchronizationManager.unbindResource(sf);
		}
	}
	else {
		// Pre-bound Session found -> simply proceed.
		return invocation.proceed();
	}
}
 
Example 5
Source File: HibernatePersistenceContextInterceptor.java    From gorm-hibernate5 with Apache License 2.0 6 votes vote down vote up
public void destroy() {
    DeferredBindingActions.clear();
    if(!disconnected.isEmpty()) {
        disconnected.pop();
    }
    if (getSessionFactory() == null || decNestingCount() > 0 || getParticipate()) {
        return;
    }

    // single session mode
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
    LOG.debug("Closing single Hibernate session in GrailsDispatcherServlet");
    try {
        disconnected.clear();
        SessionFactoryUtils.closeSession(holder.getSession());
    }
    catch (RuntimeException ex) {
        LOG.error("Unexpected exception on closing Hibernate Session", ex);
    }
}
 
Example 6
Source File: OpenSessionInViewInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Unbind the Hibernate {@code Session} from the thread and close it).
 * @see TransactionSynchronizationManager
 */
@Override
public void afterCompletion(WebRequest request, @Nullable Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		SessionHolder sessionHolder =
				(SessionHolder) TransactionSynchronizationManager.unbindResource(obtainSessionFactory());
		logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
		SessionFactoryUtils.closeSession(sessionHolder.getSession());
	}
}
 
Example 7
Source File: OpenSessionInViewInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Unbind the Hibernate {@code Session} from the thread and close it).
 * @see TransactionSynchronizationManager
 */
@Override
public void afterCompletion(WebRequest request, @Nullable Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		SessionHolder sessionHolder =
				(SessionHolder) TransactionSynchronizationManager.unbindResource(obtainSessionFactory());
		logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
		SessionFactoryUtils.closeSession(sessionHolder.getSession());
	}
}
 
Example 8
Source File: OpenSessionInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unbind the Hibernate {@code Session} from the thread and close it).
 * @see TransactionSynchronizationManager
 */
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		SessionHolder sessionHolder =
				(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
		logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
		SessionFactoryUtils.closeSession(sessionHolder.getSession());
	}
}
 
Example 9
Source File: OpenSessionInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Unbind the Hibernate {@code Session} from the thread and close it).
 * @see TransactionSynchronizationManager
 */
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		SessionHolder sessionHolder =
				(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
		logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
		SessionFactoryUtils.closeSession(sessionHolder.getSession());

	}
}
 
Example 10
Source File: DhisTest.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Unbinds and closes the bound Hibernate Session from the current thread.
 */
private void unbindSession()
{
    SessionFactory sessionFactory = (SessionFactory) getBean( "sessionFactory" );

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource( sessionFactory );

    SessionFactoryUtils.closeSession( sessionHolder.getSession() );
}
 
Example 11
Source File: IntegrationTestBase.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void unbindSession()
{
    SessionFactory sessionFactory = (SessionFactory) webApplicationContext.getBean( "sessionFactory" );

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
        .unbindResource( sessionFactory );

    SessionFactoryUtils.closeSession( sessionHolder.getSession() );
}
 
Example 12
Source File: AsyncRequestInterceptor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void closeSession() {
	if (this.timeoutInProgress || this.errorInProgress) {
		logger.debug("Closing Hibernate Session after async request timeout/error");
		SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
	}
}
 
Example 13
Source File: AsyncRequestInterceptor.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void closeSession() {
	if (this.timeoutInProgress || this.errorInProgress) {
		logger.debug("Closing Hibernate Session after async request timeout/error");
		SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
	}
}
 
Example 14
Source File: AsyncRequestInterceptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void closeAfterTimeout() {
	if (this.timeoutInProgress) {
		logger.debug("Closing Hibernate Session after async request timeout");
		SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
	}
}
 
Example 15
Source File: AsyncRequestInterceptor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void closeAfterTimeout() {
	if (this.timeoutInProgress) {
		logger.debug("Closing Hibernate Session after async request timeout");
		SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
	}
}
 
Example 16
Source File: DbmsUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void unbindSessionFromThread( SessionFactory sessionFactory )
{
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource( sessionFactory );

    SessionFactoryUtils.closeSession( sessionHolder.getSession() );
}