Java Code Examples for org.springframework.orm.hibernate5.SessionFactoryUtils

The following examples show how to use org.springframework.orm.hibernate5.SessionFactoryUtils. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: OpenSessionInterceptor.java    License: 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 Project: java-technology-stack   Source File: OpenSessionInterceptor.java    License: 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 Project: lams   Source File: OpenSessionInterceptor.java    License: 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
@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
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 Project: cosmo   Source File: CalendarDaoImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ContentItem findEventByIcalUid(String uid, CollectionItem calendar) {
    if (!(calendar instanceof HibCollectionItem)) {
        // External collections cannot be queried.
        return null;
    }
    try {
        TypedQuery<ContentItem> query = this.em.createNamedQuery("event.by.calendar.icaluid", ContentItem.class);
        query.setParameter("calendar", calendar);
        query.setParameter("uid", uid);
        List<ContentItem> resultList = query.getResultList();
        if (!resultList.isEmpty()) {
            return resultList.get(0);
        }
        return null;
    } catch (HibernateException e) {
        this.em.clear();
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}
 
Example 7
/**
 * 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
/**
 * 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 9
/**
 * 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
/**
 * 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 11
Source Project: gorm-hibernate5   Source File: GrailsHibernateTemplate.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
    if (ex instanceof JDBCException) {
        return convertJdbcAccessException((JDBCException) ex, jdbcExceptionTranslator);
    }
    if (GenericJDBCException.class.equals(ex.getClass())) {
        return convertJdbcAccessException((GenericJDBCException) ex, jdbcExceptionTranslator);
    }
    return SessionFactoryUtils.convertHibernateAccessException(ex);
}
 
Example 12
Source Project: dhis2-core   Source File: DhisTest.java    License: 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 13
private void unbindSession()
{
    SessionFactory sessionFactory = (SessionFactory) webApplicationContext.getBean( "sessionFactory" );

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

    SessionFactoryUtils.closeSession( sessionHolder.getSession() );
}
 
Example 14
Source Project: cosmo   Source File: CalendarDaoImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Set<Item> findEvents(CollectionItem collection, Date rangeStart, Date rangeEnd, String timezoneId,
        boolean expandRecurringEvents) {

    // Create a NoteItemFilter that filters by parent
    NoteItemFilter itemFilter = new NoteItemFilter();
    itemFilter.setParent(collection);

    // and EventStamp by timeRange
    EventStampFilter eventFilter = new EventStampFilter();

    if (timezoneId != null) {
        TimeZone timeZone = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(timezoneId);
        eventFilter.setTimezone(timeZone);
    }

    eventFilter.setTimeRange(rangeStart, rangeEnd);
    eventFilter.setExpandRecurringEvents(expandRecurringEvents);
    itemFilter.getStampFilters().add(eventFilter);

    try {
        return itemFilterProcessor.processFilter(itemFilter);
    } catch (HibernateException e) {
        this.em.clear();
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}
 
Example 15
Source Project: cosmo   Source File: UserDaoImpl.java    License: Apache License 2.0 5 votes vote down vote up
public void removeUser(String username) {
    try {
        User user = findUserByUsername(username);
        // delete user
        if (user != null) {
            removeUser(user);
        }
    } catch (HibernateException e) {
        this.em.clear();
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}
 
Example 16
Source Project: spring-analysis-note   Source File: AsyncRequestInterceptor.java    License: 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 17
Source Project: java-technology-stack   Source File: AsyncRequestInterceptor.java    License: 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 18
Source Project: SpringMango   Source File: MangoDaoImpl.java    License: Apache License 2.0 4 votes vote down vote up
public Connection getConnection() throws SQLException {
	return SessionFactoryUtils.getDataSource(sessionFactory).getConnection();
}
 
Example 19
Source Project: lams   Source File: AsyncRequestInterceptor.java    License: 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 20
private void closeAfterTimeout() {
	if (this.timeoutInProgress) {
		logger.debug("Closing Hibernate Session after async request timeout");
		SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
	}
}
 
Example 21
Source Project: dhis2-core   Source File: DbmsUtils.java    License: 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() );
}