本文整理汇总了Java中org.springframework.orm.hibernate3.SessionHolder类的典型用法代码示例。如果您正苦于以下问题:Java SessionHolder类的具体用法?Java SessionHolder怎么用?Java SessionHolder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SessionHolder类属于org.springframework.orm.hibernate3包,在下文中一共展示了SessionHolder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: invoke
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
@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();
}
}
示例2: postHandle
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
/**
* Flush the Hibernate {@code Session} before view rendering, if necessary.
* <p>Note that this just applies in {@link #isSingleSession() single session mode}!
* <p>The default is {@code FLUSH_NEVER} to avoid this extra flushing,
* assuming that service layer transactions have flushed their changes on commit.
* @see #setFlushMode
*/
@Override
public void postHandle(WebRequest request, ModelMap model) throws DataAccessException {
if (isSingleSession()) {
// Only potentially flush in single session mode.
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
logger.debug("Flushing single Hibernate Session in OpenSessionInViewInterceptor");
try {
flushIfNecessary(sessionHolder.getSession(), false);
}
catch (HibernateException ex) {
throw convertHibernateAccessException(ex);
}
}
}
示例3: afterCompletion
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
/**
* Unbind the Hibernate {@code Session} from the thread and close it (in
* single session mode), or process deferred close for all sessions that have
* been opened during the current request (in deferred close mode).
* @see org.springframework.transaction.support.TransactionSynchronizationManager
*/
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
if (!decrementParticipateCount(request)) {
if (isSingleSession()) {
// single session mode
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
logger.debug("Closing single Hibernate Session in OpenSessionInViewInterceptor");
SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(getSessionFactory());
}
}
}
示例4: afterCompletion
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
/**
* Unbind the Hibernate {@code Session} from the thread and close it (in
* single session mode), or process deferred close for all sessions that have
* been opened during the current request (in deferred close mode).
* @see org.springframework.transaction.support.TransactionSynchronizationManager
*/
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
if (!decrementParticipateCount(request)) {
if (isSingleSession()) {
// single session mode
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
logger.debug("Closing single Hibernate Session in OpenSessionInViewInterceptor");
SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(getSessionFactory());
}
}
}
示例5: openReadOnlyConnection
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
private void openReadOnlyConnection(final HttpServletRequest request) {
if (noTransaction(request)) {
return;
}
logDebug(request, "Opening read-only transaction for include");
final Connection connection = (Connection) TransactionSynchronizationManager.getResource(connectionProvider);
final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
final Session session = holder.getSession();
session.setFlushMode(FlushMode.MANUAL);
session.setDefaultReadOnly(true);
session.reconnect(connection);
TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);
}
示例6: unbindSession
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
private void unbindSession() {
if (sessionFactory == null) {
throw new IllegalStateException("No sessionFactory property provided");
}
SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
try {
if (!FlushMode.MANUAL.equals(sessionHolder.getSession().getFlushMode())) {
sessionHolder.getSession().flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
}
示例7: preHandle
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
/**
* Open a new Hibernate {@code Session} according to the settings of this
* {@code HibernateAccessor} and bind it to the thread via the
* {@link TransactionSynchronizationManager}.
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession
*/
@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 ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) ||
SessionFactoryUtils.isDeferredCloseActive(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 {
if (isSingleSession()) {
// single session mode
logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor");
Session session = SessionFactoryUtils.getSession(
getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
applyFlushMode(session, false);
SessionHolder sessionHolder = new SessionHolder(session);
TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);
AsyncRequestInterceptor asyncRequestInterceptor =
new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
}
else {
// deferred close mode
SessionFactoryUtils.initDeferredClose(getSessionFactory());
}
}
}
示例8: doGetActivePersistenceContext
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
@Override
protected Session doGetActivePersistenceContext(Object testObject) {
SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(getPersistenceUnit(testObject));
if (sessionHolder != null && sessionHolder.getSession() != null && sessionHolder.getSession().isOpen()) {
return sessionHolder.getSession();
}
return null;
}
示例9: tearDown
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
/**
* @throws Exception
*/
@After
public void tearDown() throws Exception {
if (sessionOwner && session != null) {
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(s);
}
if (wiser != null) {
wiser.stop();
}
}
示例10: setUp
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
public void setUp()
throws Exception
{
SessionFactory sf = (SessionFactory) this.context.getBean("sessionFactory");
//Session s = SessionFactoryUtils.getSession(sessionFactory, true);
// open and bind the session for this test thread.
Session s = sf.openSession();
TransactionSynchronizationManager.bindResource(sf, new SessionHolder(s));
}
示例11: tearDown
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
public void tearDown()
throws Exception
{
SessionFactory sf = (SessionFactory) this.context.getBean("sessionFactory");
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sf);
//SessionFactoryUtils.closeSessionIfNecessary(s, sf);
SessionFactoryUtils.releaseSession(s, sf);
}
示例12: openSession
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
public static boolean openSession(SessionFactory sessionFactory) {
boolean participate = TransactionSynchronizationManager.hasResource(sessionFactory);
if (!participate) {
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(org.hibernate.FlushMode.ALWAYS);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
return participate;
}
示例13: openSession
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
@Before
public void openSession() throws Exception {
session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.MANUAL);//MANUAL
TransactionSynchronizationManager.bindResource(sessionFactory,new SessionHolder(session));
}
示例14: preHandle
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
/**
* Open a new Hibernate {@code Session} according to the settings of this
* {@code HibernateAccessor} and bind it to the thread via the
* {@link TransactionSynchronizationManager}.
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession
*/
public void preHandle(WebRequest request) throws DataAccessException {
String participateAttributeName = getParticipateAttributeName();
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
if (asyncManager.hasConcurrentResult()) {
if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
return;
}
}
if ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) ||
SessionFactoryUtils.isDeferredCloseActive(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 {
if (isSingleSession()) {
// single session mode
logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor");
Session session = SessionFactoryUtils.getSession(
getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
applyFlushMode(session, false);
SessionHolder sessionHolder = new SessionHolder(session);
TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);
AsyncRequestInterceptor asyncRequestInterceptor =
new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
}
else {
// deferred close mode
SessionFactoryUtils.initDeferredClose(getSessionFactory());
}
}
}
示例15: postHandle
import org.springframework.orm.hibernate3.SessionHolder; //导入依赖的package包/类
/**
* Flush the Hibernate {@code Session} before view rendering, if necessary.
* <p>Note that this just applies in {@link #isSingleSession() single session mode}!
* <p>The default is {@code FLUSH_NEVER} to avoid this extra flushing,
* assuming that service layer transactions have flushed their changes on commit.
* @see #setFlushMode
*/
public void postHandle(WebRequest request, ModelMap model) throws DataAccessException {
if (isSingleSession()) {
// Only potentially flush in single session mode.
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
logger.debug("Flushing single Hibernate Session in OpenSessionInViewInterceptor");
try {
flushIfNecessary(sessionHolder.getSession(), false);
}
catch (HibernateException ex) {
throw convertHibernateAccessException(ex);
}
}
}