org.springframework.orm.hibernate5.SessionFactoryUtils Java Examples

The following examples show how to use org.springframework.orm.hibernate5.SessionFactoryUtils. 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: CalendarDaoImpl.java    From cosmo with 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 #3
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 #4
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 #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: 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 #7
Source File: UserDaoImpl.java    From cosmo with 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 #8
Source File: CalendarDaoImpl.java    From cosmo with 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 #9
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 #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: GrailsHibernateTemplate.java    From gorm-hibernate5 with 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 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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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() );
}
 
Example #19
Source File: MangoDaoImpl.java    From SpringMango with Apache License 2.0 4 votes vote down vote up
public Connection getConnection() throws SQLException {
	return SessionFactoryUtils.getDataSource(sessionFactory).getConnection();
}
 
Example #20
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 #21
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());
	}
}