Java Code Examples for org.springframework.orm.hibernate4.SessionHolder

The following examples show how to use org.springframework.orm.hibernate4.SessionHolder. 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: 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 2
@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 3
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
		Session session = openSession();
		SessionHolder sessionHolder = new SessionHolder(session);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
Example 4
/**
 * Unbind the Hibernate {@code Session} from the thread and close it).
 * @see org.springframework.transaction.support.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 5
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
		Session session = openSession();
		SessionHolder sessionHolder = new SessionHolder(session);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
Example 6
/**
 * Unbind the Hibernate {@code Session} from the thread and close it).
 * @see org.springframework.transaction.support.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 7
Source Project: wetech-cms   Source File: UserService1Test.java    License: MIT License 5 votes vote down vote up
@Before
	public void setUp() throws SQLException, IOException {
		//此时最好不要使用Spring的Transactional来管理,因为dbunit是通过jdbc来处理connection,再使用spring在一些编辑操作中会造成事务shisu
		Session s = sessionFactory.openSession();
		TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
//		this.backupAllTable();
//		IDataSet ds = createDateSet("t_user");
//		DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds);
	}
 
Example 8
Source Project: wetech-cms   Source File: UserService1Test.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	//this.resumeTable();
}
 
Example 9
Source Project: wetech-cms   Source File: TopicDaoTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws SQLException, IOException, DatabaseUnitException {
	//此时最好不要使用Spring的Transactional来管理,因为dbunit是通过jdbc来处理connection,再使用spring在一些编辑操作中会造成事务shisu
	Session s = sessionFactory.openSession();
	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
	this.backupAllTable();
	IDataSet ds = createDateSet("topic");
	DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds);
}
 
Example 10
Source Project: wetech-cms   Source File: TopicDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	this.resumeTable();
}
 
Example 11
Source Project: wetech-cms   Source File: AttachmentDaoTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws SQLException, IOException, DatabaseUnitException {
	//此时最好不要使用Spring的Transactional来管理,因为dbunit是通过jdbc来处理connection,再使用spring在一些编辑操作中会造成事务shisu
	Session s = sessionFactory.openSession();
	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
	this.backupAllTable();
	IDataSet ds = createDateSet("topic");
	DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds);
}
 
Example 12
Source Project: wetech-cms   Source File: AttachmentDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	this.resumeTable();
}
 
Example 13
Source Project: wetech-cms   Source File: ChannelAndTopicTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
}
 
Example 14
Source Project: wetech-cms   Source File: CmsLinkDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
}
 
Example 15
Source Project: wetech-cms   Source File: GroupDaoTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws SQLException, IOException, DatabaseUnitException {
	Session s = sessionFactory.openSession();
	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
	this.backupAllTable();
	IDataSet ds = createDateSet("t_user");
	DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds);
}
 
Example 16
Source Project: wetech-cms   Source File: GroupDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws Exception {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	this.resumeTable();
}
 
Example 17
Source Project: wetech-cms   Source File: ChannelDaoTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws SQLException, IOException, DatabaseUnitException {
	//此时最好不要使用Spring的Transactional来管理,因为dbunit是通过jdbc来处理connection,再使用spring在一些编辑操作中会造成事务shisu
	Session s = sessionFactory.openSession();
	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
	this.backupAllTable();
	IDataSet ds = createDateSet("topic");
	DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds);
}
 
Example 18
Source Project: wetech-cms   Source File: ChannelDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	this.resumeTable();
}
 
Example 19
Source Project: wetech-cms   Source File: IndexPicDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
}
 
Example 20
Source Project: wetech-cms   Source File: KeywordDaoTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws SQLException, IOException, DatabaseUnitException {
	//此时最好不要使用Spring的Transactional来管理,因为dbunit是通过jdbc来处理connection,再使用spring在一些编辑操作中会造成事务shisu
	Session s = sessionFactory.openSession();
	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
	this.backupAllTable();
	IDataSet ds = createDateSet("topic");
	DatabaseOperation.TRUNCATE_TABLE.execute(dbunitCon, ds);
	DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds);
}
 
Example 21
Source Project: wetech-cms   Source File: KeywordDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	this.resumeTable();
}
 
Example 22
Source Project: wetech-cms   Source File: RoleDaoTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws SQLException, IOException, DatabaseUnitException {
	Session s = sessionFactory.openSession();
	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
	this.backupAllTable();
	IDataSet ds = createDateSet("t_user");
	DatabaseOperation.CLEAN_INSERT.execute(dbunitCon, ds);
}
 
Example 23
Source Project: wetech-cms   Source File: RoleDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession();
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	this.resumeTable();
}
 
Example 24
Source Project: wetech-cms   Source File: UserDaoTest.java    License: MIT License 5 votes vote down vote up
@Before
	public void setUp() throws SQLException, IOException, DatabaseUnitException {
		//此时最好不要使用Spring的Transactional来管理,因为dbunit是通过jdbc来处理connection,再使用spring在一些编辑操作中会造成事务shisu
		Session s = sessionFactory.openSession();
		TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
//		this.backupAllTable();
//		IDataSet ds = createDateSet("t_user");
//		DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds);
	}
 
Example 25
Source Project: wetech-cms   Source File: UserDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws DatabaseUnitException, SQLException, IOException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	//this.resumeTable();
}
 
Example 26
Source Project: wetech-cms   Source File: UserDaoTest.java    License: MIT License 5 votes vote down vote up
@After
public void tearDown() throws FileNotFoundException, DatabaseUnitException, SQLException {
	SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	Session s = holder.getSession(); 
	s.flush();
	TransactionSynchronizationManager.unbindResource(sessionFactory);
	this.resumeTable();
}
 
Example 27
Source Project: SpringCloud   Source File: TestUserDao.java    License: Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() throws FileNotFoundException, DatabaseUnitException, SQLException {
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    Session s = holder.getSession();
    s.flush();
    TransactionSynchronizationManager.unbindResource(sessionFactory);
    this.resumeTable();
}
 
Example 28
Source Project: lemon   Source File: SpringSessionContext.java    License: Apache License 2.0 5 votes vote down vote up
public Session currentSession() throws HibernateException {
    Object value = TransactionSynchronizationManager
            .getResource(this.sessionFactory);

    if (value instanceof Session) {
        return (Session) value;
    } else if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        Session session = sessionHolder.getSession();

        if (TransactionSynchronizationManager.isSynchronizationActive()
                && !sessionHolder.isSynchronizedWithTransaction()) {
            TransactionSynchronizationManager
                    .registerSynchronization(new SpringSessionSynchronization(
                            sessionHolder, this.sessionFactory));
            sessionHolder.setSynchronizedWithTransaction(true);

            // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
            // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
            FlushMode flushMode = session.getFlushMode();

            if (FlushMode.isManualFlushMode(flushMode)
                    && !TransactionSynchronizationManager
                            .isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.AUTO);
                sessionHolder.setPreviousFlushMode(flushMode);
            }
        }

        return session;
    } else {
        throw new HibernateException("No Session found for current thread");
    }
}
 
Example 29
Source Project: lams   Source File: AsyncRequestInterceptor.java    License: GNU General Public License v2.0 4 votes vote down vote up
public AsyncRequestInterceptor(SessionFactory sessionFactory, SessionHolder sessionHolder) {
	this.sessionFactory = sessionFactory;
	this.sessionHolder = sessionHolder;
}
 
Example 30
public AsyncRequestInterceptor(SessionFactory sessionFactory, SessionHolder sessionHolder) {
	this.sessionFactory = sessionFactory;
	this.sessionHolder = sessionHolder;
}