Package net.sf.hibernate

Examples of net.sf.hibernate.Session


    protected abstract ApplicationContext getContext();

    private void openSession() {
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
        Session hibSession = SessionFactoryUtils.getSession(sessionFactory, true);
        hibSession.setFlushMode(FlushMode.NEVER);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(hibSession));
    }
View Full Code Here


  public void beforeCommit(boolean readOnly) throws DataAccessException {
    if (!readOnly) {
      // read-write transaction -> flush the Hibernate Session
      SessionFactoryUtils.logger.debug("Flushing Hibernate Session on transaction synchronization");
      Session session = null;
      // Check whether there is a Hibernate Session for the current JTA
      // transaction. Else, fall back to the default thread-bound Session.
      if (this.jtaTransaction != null) {
        session = this.sessionHolder.getSession(this.jtaTransaction);
      }
      if (session == null) {
        session = this.sessionHolder.getSession();
      }
      // Further check: only flush when not FlushMode.NEVER
      if (!session.getFlushMode().equals(FlushMode.NEVER)) {
        try {
          session.flush();
        }
        catch (JDBCException ex) {
          if (this.jdbcExceptionTranslator != null) {
            throw this.jdbcExceptionTranslator.translate(
                "Hibernate transaction synchronization: " + ex.getMessage(), null, ex.getSQLException());
View Full Code Here

  public void beforeCompletion() {
    if (this.jtaTransaction != null) {
      // Typically in case of a suspended JTA transaction:
      // Remove the Session for the current JTA transaction, but keep the holder.
      Session session = this.sessionHolder.removeSession(this.jtaTransaction);
      if (session != null) {
        if (this.sessionHolder.isEmpty()) {
          // No Sessions for JTA transactions bound anymore -> could remove it.
          if (TransactionSynchronizationManager.hasResource(this.sessionFactory)) {
            // Explicit check necessary because of remote transaction propagation:
            // The synchronization callbacks will execute in a different thread
            // in such a scenario, as they're triggered by a remote server.
            // The best we can do is to leave the SessionHolder bound to the
            // thread that originally performed the data access. It will be
            // reused when a new data access operation starts on that thread.
            TransactionSynchronizationManager.unbindResource(this.sessionFactory);
          }
          this.holderActive = false;
        }
        // Do not close a pre-bound Session. In that case, we'll find the
        // transaction-specific Session the same as the default Session.
        if (session != this.sessionHolder.getSession()) {
          SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, this.sessionFactory);
        }
        else if (this.sessionHolder.getPreviousFlushMode() != null) {
          // In case of pre-bound Session, restore previous flush mode.
          session.setFlushMode(this.sessionHolder.getPreviousFlushMode());
        }
        return;
      }
    }
    // We'll only get here if there was no specific JTA transaction to handle.
View Full Code Here

  public void afterCompletion(int status) {
    if (!this.hibernateTransactionCompletion || !this.newSession) {
      // No Hibernate TransactionManagerLookup: apply afterTransactionCompletion callback.
      // Always perform explicit afterTransactionCompletion callback for pre-bound Session,
      // even with Hibernate TransactionManagerLookup (which only applies to new Sessions).
      Session session = this.sessionHolder.getSession();
      // Provide correct transaction status for releasing the Session's cache locks,
      // if possible. Else, closing will release all cache locks assuming a rollback.
      if (session instanceof SessionImplementor) {
        ((SessionImplementor) session).afterTransactionCompletion(status == STATUS_COMMITTED);
      }
View Full Code Here

  public Session getValidatedSession() {
    return getValidatedSession(DEFAULT_KEY);
  }

  public Session getValidatedSession(Object key) {
    Session session = (Session) this.sessionMap.get(key);
    // Check for dangling Session that's around but already closed.
    // Effectively an assertion: that should never happen in practice.
    // We'll seamlessly remove the Session here, to not let it cause
    // any side effects.
    if (session != null && !session.isOpen()) {
      this.sessionMap.remove(key);
      session = null;
    }
    return session;
  }
View Full Code Here

   * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
   */
  public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null");

    Session session = getSession();
    boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());
    if (existingTransaction) {
      logger.debug("Found thread-bound Session for HibernateTemplate");
    }

    FlushMode previousFlushMode = null;
    try {
      previousFlushMode = applyFlushMode(session, existingTransaction);
      Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));
      Object result = action.doInHibernate(sessionToExpose);
      flushIfNecessary(session, existingTransaction);
      return result;
    }
    catch (HibernateException ex) {
View Full Code Here

    this.exceptionConversionEnabled = exceptionConversionEnabled;
  }


  public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    Session session = getSession();
    boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());

    if (existingTransaction) {
      logger.debug("Found thread-bound Session for HibernateInterceptor");
    }
    else {
      TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));
    }

    FlushMode previousFlushMode = null;
    try {
      previousFlushMode = applyFlushMode(session, existingTransaction);
      Object retVal = methodInvocation.proceed();
      flushIfNecessary(session, existingTransaction);
      return retVal;
    }
    catch (HibernateException ex) {
      if (this.exceptionConversionEnabled) {
        throw convertHibernateAccessException(ex);
      }
      else {
        throw ex;
      }
    }
    finally {
      if (existingTransaction) {
        logger.debug("Not closing pre-bound Hibernate Session after HibernateInterceptor");
        if (previousFlushMode != null) {
          session.setFlushMode(previousFlushMode);
        }
      }
      else {
        TransactionSynchronizationManager.unbindResource(getSessionFactory());
        SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
View Full Code Here

          "running within DataSourceTransactionManager if told to manage the DataSource itself. " +
          "It is recommended to use a single HibernateTransactionManager for all transactions " +
          "on a single DataSource, no matter whether Hibernate or JDBC access.");
    }

    Session session = null;

    try {
      if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
        Interceptor entityInterceptor = getEntityInterceptor();
        Session newSession = (entityInterceptor != null ?
            getSessionFactory().openSession(entityInterceptor) : getSessionFactory().openSession());
        if (logger.isDebugEnabled()) {
          logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
        }
        txObject.setSessionHolder(new SessionHolder(newSession), true);
View Full Code Here

    }
    catch (HibernateException ex) {
      logger.info("Could not access JDBC Connection of Hibernate Session", ex);
    }

    Session session = txObject.getSessionHolder().getSession();
    if (txObject.isNewSessionHolder()) {
      if (logger.isDebugEnabled()) {
        logger.debug("Closing Hibernate Session [" + session + "] after transaction");
      }
      SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
    }
    else {
      if (logger.isDebugEnabled()) {
        logger.debug("Not closing pre-bound Hibernate Session [" + session + "] after transaction");
      }
      if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
        session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
      }
    }
    txObject.getSessionHolder().clear();
  }
View Full Code Here

        // Do not modify the Session: just set the participate flag.
        participate = true;
      }
      else {
        logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
        Session session = getSession(sessionFactory);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
      }
    }
    else {
      // deferred close mode
View Full Code Here

TOP

Related Classes of net.sf.hibernate.Session

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.