org.springframework.orm.hibernate4.SessionHolder Java Examples

The following examples show how to use org.springframework.orm.hibernate4.SessionHolder. 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 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 #2
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 #3
Source File: GroupDaoTest.java    From wetech-cms with 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 #4
Source File: OpenSessionInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #5
Source File: GroupDaoTest.java    From wetech-cms with 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 #6
Source File: ChannelDaoTest.java    From wetech-cms with 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 #7
Source File: ChannelDaoTest.java    From wetech-cms with 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 #8
Source File: IndexPicDaoTest.java    From wetech-cms with 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 #9
Source File: KeywordDaoTest.java    From wetech-cms with 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 #10
Source File: KeywordDaoTest.java    From wetech-cms with 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 File: RoleDaoTest.java    From wetech-cms with 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 #12
Source File: RoleDaoTest.java    From wetech-cms with 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 File: UserDaoTest.java    From wetech-cms with 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 #14
Source File: UserDaoTest.java    From wetech-cms with 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 #15
Source File: UserDaoTest.java    From wetech-cms with 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 #16
Source File: TestUserDao.java    From SpringCloud with 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 #17
Source File: SpringSessionContext.java    From lemon with 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 #18
Source File: CmsLinkDaoTest.java    From wetech-cms with 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 #19
Source File: ChannelAndTopicTest.java    From wetech-cms with 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 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 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 #21
Source File: AttachmentDaoTest.java    From wetech-cms with 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 File: AttachmentDaoTest.java    From wetech-cms with 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 #23
Source File: TopicDaoTest.java    From wetech-cms with 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 File: TopicDaoTest.java    From wetech-cms with 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 #25
Source File: UserService1Test.java    From wetech-cms with 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 #26
Source File: UserService1Test.java    From wetech-cms with 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 #27
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 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 #28
Source File: OpenSessionInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #29
Source File: SpringSessionSynchronization.java    From lemon with Apache License 2.0 4 votes vote down vote up
public SpringSessionSynchronization(SessionHolder sessionHolder,
        SessionFactory sessionFactory) {
    this.sessionHolder = sessionHolder;
    this.sessionFactory = sessionFactory;
}
 
Example #30
Source File: AsyncRequestInterceptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public AsyncRequestInterceptor(SessionFactory sessionFactory, SessionHolder sessionHolder) {
	this.sessionFactory = sessionFactory;
	this.sessionHolder = sessionHolder;
}