/* (non-Javadoc)
   * @see org.jobcenter.internalservice.SetUpHibernateSessionForNonWebRequestProcessing#closeSession()
   */
  public void closeSession() {

    if (sessionFactory != null) {

      SessionHolder sessionHolder =
          (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);

      SessionFactoryUtils.closeSession(sessionHolder.getSession());
    }
  }
 private void endSession(PhaseEvent event) {
   if (event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
     final SessionHolder sessionHolder =
         (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
     if (sessionHolder != null
         && !FlushMode.MANUAL.equals(sessionHolder.getSession().getFlushMode())) {
       sessionHolder.getSession().flush();
     }
     if (boundByMe.get()) unbindSession();
   }
 }
 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());
   }
 }
 private boolean bindSession() {
   if (sessionFactory == null) {
     throw new IllegalStateException("No sessionFactory property provided");
   }
   final Object inStorage = TransactionSynchronizationManager.getResource(sessionFactory);
   if (inStorage != null) {
     ((SessionHolder) inStorage).getSession().flush();
     return false;
   } else {
     Session session = SessionFactoryUtils.getSession(sessionFactory, true);
     session.setFlushMode(FlushMode.AUTO);
     TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
     return true;
   }
 }
 private void closeAfterTimeout() {
   if (this.timeoutInProgress) {
     logger.debug("Closing Hibernate Session after async request timeout");
     SessionFactoryUtils.closeSession(sessionHolder.getSession());
   }
 }
  @Override
  protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
    try {
      logger.debug("running offer update rules job:" + Calendar.getInstance().getTimeInMillis());

      // bind session to thread
      Session session = null;
      try {
        session = SessionFactoryUtils.getSession(sessionFactory, false);
      }
      // If not already bound the Create and Bind it!
      catch (java.lang.IllegalStateException ex) {
        session = SessionFactoryUtils.getSession(sessionFactory, true);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
      }
      session.setFlushMode(FlushMode.AUTO);

      // find awards whose offers are expired only on bl failure
      // really should fire working memory rules against rater that has expired
      // awards

      List<Offer> expired = awardManagerService.findExpiredAwardOffers();

      logger.debug("number of expired offers:" + expired.size());

      /*for (AwardOffer awardOffer : expired) {
      	//logger.debug(awardOffer.getOffer().getExpireDateMillis());

      	//delete any expired offer
      	//remove the offer from the award
      	Award a = awardOffer.getAward();
      	a.getOffers().remove(awardOffer);
      	awardManagerService.saveAward(a);

      	awardManagerService.deleteAwardOffer(awardOffer);

      	if(awardOffer.getAward() != null)
      	{
      		//dont throw if one has a problem
      		try {
      			updateAwardOfferMessageProducer
      			.generateMessage(
      					awardOffer.getAward(),
      					awardOffer.getAwardType(),
      					awardOffer.getAward().getOwner());
      		} catch (JsonGenerationException e) {
      			logger.error("problem udating expired offer", e);
      		} catch (JsonMappingException e) {
      			logger.error("problem udating expired offer", e);
      		} catch (JMSException e) {
      			logger.error("problem udating expired offer", e);
      		} catch (IOException e) {
      			logger.error("problem udating expired offer", e);
      		}
      	}


      }*/

    } catch (com.noi.utility.spring.service.BLServiceException e) {
      logger.error("problem updating expired offers", e);
      throw new JobExecutionException(e);
    } catch (Exception e) {
      logger.error("problem updating expired offers", e);
      throw new JobExecutionException(e);
    } finally {
      // unbind
      SessionHolder sessionHolder =
          (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
      if (!FlushMode.MANUAL.equals(sessionHolder.getSession().getFlushMode())) {
        sessionHolder.getSession().flush();
      }
      SessionFactoryUtils.closeSession(sessionHolder.getSession());
    }
  }