Package org.hibernate.classic

Examples of org.hibernate.classic.Session


   *
   * @param factory The factory for which to unbind the current session.
   * @return The bound session if one, else null.
   */
  public static Session unbind(SessionFactory factory) {
    Session existing = null;
    Map sessionMap = sessionMap();
    if ( sessionMap != null ) {
      existing = ( Session ) sessionMap.remove( factory );
      doCleanup();
    }
View Full Code Here


    }
    catch ( Throwable t ) {
      throw new HibernateException( "Problem locating/validating JTA transaction", t );
    }

    Session currentSession = ( Session ) currentSessionMap.get( txn );

    if ( currentSession == null ) {
      currentSession = buildOrObtainSession();

      try {
        txn.registerSynchronization( buildCleanupSynch( txn ) );
      }
      catch ( Throwable t ) {
        try {
          currentSession.close();
        }
        catch ( Throwable ignore ) {
          log.debug( "Unable to release generated current-session on failed synch registration", ignore );
        }
        throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" );
View Full Code Here

  /**
   * {@inheritDoc}
   */
  public final Session currentSession() throws HibernateException {
    Session current = existingSession( factory );
    if (current == null) {
      current = buildOrObtainSession();
      // register a cleanup synch
      current.getTransaction().registerSynchronization( buildCleanupSynch() );
      // wrap the session in the transaction-protection proxy
      if ( needsWrapping( current ) ) {
        current = wrap( current );
      }
      // then bind it
View Full Code Here

    return factory.getSettings().getConnectionReleaseMode();
  }

  protected Session wrap(Session session) {
    TransactionProtectionWrapper wrapper = new TransactionProtectionWrapper( session );
    Session wrapped = ( Session ) Proxy.newProxyInstance(
        Session.class.getClassLoader(),
            SESS_PROXY_INTERFACES,
            wrapper
      );
    // yick!  need this for proper serialization/deserialization handling...
View Full Code Here

    cleanupAnyOrphanedSession( factory );
    doBind( session, factory );
  }

  private static void cleanupAnyOrphanedSession(SessionFactory factory) {
    Session orphan = doUnbind( factory, false );
    if ( orphan != null ) {
      log.warn( "Already session bound on call to bind(); make sure you clean up your sessions!" );
      try {
        if ( orphan.getTransaction() != null && orphan.getTransaction().isActive() ) {
          try {
            orphan.getTransaction().rollback();
          }
          catch( Throwable t ) {
            log.debug( "Unable to rollback transaction for orphaned session", t );
          }
        }
        orphan.close();
      }
      catch( Throwable t ) {
        log.debug( "Unable to close orphaned session", t );
      }
    }
View Full Code Here

    sessionMap.put( factory, session );
  }

  private static Session doUnbind(SessionFactory factory, boolean releaseMapIfEmpty) {
    Map sessionMap = sessionMap();
    Session session = null;
    if ( sessionMap != null ) {
      session = ( Session ) sessionMap.remove( factory );
      if ( releaseMapIfEmpty && sessionMap.isEmpty() ) {
        context.set( null );
      }
View Full Code Here

  private String filename;
  private List queryStrings;

  public void doStart() {
    Session session = null;
    SessionFactory sessionFactory = null;
    Transaction transaction = null;
    try {    
      sessionFactory = getConfiguration().buildSessionFactory();
      session = sessionFactory.openSession();
      transaction = session.beginTransaction();
      // TODO: this is not the most efficient loop (opening/closing file)
      for (Iterator iter = queryStrings.iterator(); iter.hasNext();) {
        String query = (String) iter.next();
       
        List list = session.createQuery(query).list();
       
        if(getFileName()!=null) {
          PrintWriter pw = null;
          try {
            File file = new File( getOutputDirectory(), getFileName() );
            getTemplateHelper().ensureExistence( file );
            pw = new PrintWriter( new FileWriter( file, true ) );     
            getArtifactCollector().addFile( file, "query-output" );
           
            for (Iterator iter1 = list.iterator(); iter1.hasNext();) {
              Object element = iter1.next();
              pw.println(element);
            }
           
          }
          catch (IOException e) {
            throw new ExporterException("Could not write query output",e);
          } finally {
            if(pw!=null) {
              pw.flush();
              pw.close();
            }
          }
        }
      }
      transaction.commit();
    } catch(HibernateException he) {
      if(transaction!=null) {
        transaction.rollback();
      }
      throw new ExporterException("Error occured while trying to execute query", he);
    } finally {     
      if(session!=null) {
        session.close();       
      }
      if(sessionFactory!=null) {
        sessionFactory.close();
      }
     
View Full Code Here

  /**
   * {@inheritDoc}
   */
  public Session currentSession() {
    Session current = existingSession( factory );
    if ( current == null ) {
      throw new HibernateException( "No session currently bound to execution context" );
    }
    return current;
  }
View Full Code Here

   *
   * @param factory The factory for which to unbind the current session.
   * @return The bound session if one, else null.
   */
  public static Session unbind(SessionFactory factory) {
    Session existing = null;
    Map sessionMap = sessionMap();
    if ( sessionMap != null ) {
      existing = ( Session ) sessionMap.remove( factory );
      doCleanup();
    }
View Full Code Here

    final Object txnIdentifier = factory.getSettings().getTransactionManagerLookup() == null
        ? txn
        : factory.getSettings().getTransactionManagerLookup().getTransactionIdentifier( txn );

    Session currentSession = ( Session ) currentSessionMap.get( txnIdentifier );

    if ( currentSession == null ) {
      currentSession = buildOrObtainSession();

      try {
        txn.registerSynchronization( buildCleanupSynch( txnIdentifier ) );
      }
      catch ( Throwable t ) {
        try {
          currentSession.close();
        }
        catch ( Throwable ignore ) {
          log.debug( "Unable to release generated current-session on failed synch registration", ignore );
        }
        throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" );
View Full Code Here

TOP

Related Classes of org.hibernate.classic.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.