/** * Obtain a Hibernate Session, either from the current transaction or a new one. The latter is * only allowed if "allowCreate" is true. * * <p><b>Note that this is not meant to be invoked from HibernateTemplate code but rather just in * plain Hibernate code.</b> Either rely on a thread-bound Session or use it in combination with * {@link #releaseSession}. * * <p>In general, it is recommended to use {@link #getHibernateTemplate() HibernateTemplate}, * either with the provided convenience operations or with a custom {@link * org.springframework.orm.hibernate3.HibernateCallback} that provides you with a Session to work * on. HibernateTemplate will care for all resource management and for proper exception * conversion. * * @param allowCreate if a non-transactional Session should be created when no transactional * Session can be found for the current thread * @return the Hibernate Session * @throws DataAccessResourceFailureException if the Session couldn't be created * @throws IllegalStateException if no thread-bound Session found and allowCreate=false * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean) */ protected final Session getSession(boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException { return (!allowCreate ? SessionFactoryUtils.getSession(getSessionFactory(), false) : SessionFactoryUtils.getSession( getSessionFactory(), this.hibernateTemplate.getEntityInterceptor(), this.hibernateTemplate.getJdbcExceptionTranslator())); }
@Transactional void clear(final String organismCommonName, final String analysisProgram) throws HibernateException, SQLException { Session session = SessionFactoryUtils.getSession(sessionFactory, false); session.doWork( new Work() { public void execute(Connection connection) throws SQLException { new ClearDomains(connection, organismCommonName, analysisProgram).clear(); } }); }
protected void setUp() throws Exception { super.setUp(); context = new ClassPathXmlApplicationContext("applicationContext.xml"); hibernateTemplate = (HibernateTemplate) context.getBean("hibernateTemplate"); sessionFactory = (SessionFactory) context.getBean("sessionFactory"); session = SessionFactoryUtils.getSession(sessionFactory, null, null); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); }
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; } }
public void staticIndex() { log.info("static index page"); CmsSite site = cmsSiteMng.findById(siteId); Session session = SessionFactoryUtils.getSession(sessionFactory, true); session = sessionFactory.openSession(); session.beginTransaction(); site = (CmsSite) session.get(site.getClass(), site.getId()); try { staticPageSvc.index(site); } catch (IOException e) { log.error("static index error!", e); } catch (TemplateException e) { log.error("static index error!", e); } session.flush(); session.close(); }
public void doStuff(String userName, String rollName, String albumName) { Session m_session = SessionFactoryUtils.getSession(m_sessionFactory, false); try { m_transaction = m_session.beginTransaction(); ImageGroupRepository gf = (ImageGroupRepository) m_appContext.getBean(GROUP_FACTORY_BEAN); UserRepository uf = (UserRepository) m_appContext.getBean(USER_FACTORY_BEAN); ImageGroup roll = gf.getRollByOwnerAndName(uf.getByScreenName(userName), rollName); ImageGroup album = new ImageGroup(albumName, roll.getOwner(), ImageGroup.Type.ALBUM); album.setDisplayName(roll.getDisplayName()); album.setDescription(roll.getDescription()); album.setSupergroup(roll.getSupergroup()); // XXX: not copying subgroups // this is probably a leaf group m_session.save(album); SortedSet frames = roll.getFrames(); Iterator iter = frames.iterator(); ImageFrame oldFrame, newFrame; while (iter.hasNext()) { oldFrame = (ImageFrame) iter.next(); newFrame = new ImageFrame(oldFrame.getImage()); newFrame.setPosition(oldFrame.getPosition()); newFrame.setCaption(oldFrame.getCaption()); newFrame.setImageGroup(album); m_session.save(newFrame); s_logger.debug("Saved frame " + newFrame.getPosition()); } m_session.save(album); m_transaction.commit(); SessionFactoryUtils.releaseSession(m_session, m_sessionFactory); s_logger.info("Saved new album: " + album.getName()); } catch (Exception e) { s_logger.error("Exception copying image frame data", e); try { m_transaction.rollback(); } catch (HibernateException e1) { s_logger.error("Exception rolling back transaction!", e1); } } }
/** @param args */ public static void main(String[] args) throws Exception { System.out.println("Starting up... " + new Date()); logger.info("Starting up..."); ApplicationContext context = new ClassPathXmlApplicationContext("rssAggregatorContext.xml"); SessionFactory sessionFactory = context.getBean(SessionFactory.class); Session session = SessionFactoryUtils.getSession(sessionFactory, true); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); RSSAggregatorService rssAggregatorService = context.getBean("rssAggregatorService", RSSAggregatorService.class); rssAggregatorService.checkActiveFeedsForUpdates(); // check the feeds? TransactionSynchronizationManager.unbindResource(sessionFactory); SessionFactoryUtils.releaseSession(session, sessionFactory); System.out.println("Finished at " + new Date()); logger.info("Finished"); }
@Transactional private void createOrganismIfNecessary( String organismCommonName, String organismGenus, String organismSpecies, String organismStrain) { Session session = SessionFactoryUtils.getSession(sessionFactory, false); Organism organism = organismDao.getOrganismByCommonName(organismCommonName); if (organism != null) { logger.debug(String.format("Organism '%s' already exists", organismCommonName)); return; } if (organismStrain != null) { organismSpecies += " " + organismStrain; } logger.debug( String.format( "Creating organism '%s' (%s %s)", organismCommonName, organismGenus, organismSpecies)); organism = new Organism(organismGenus, organismSpecies, organismCommonName, organismCommonName, null); session.persist(organism); session.flush(); }
/** * 获取hibernate session</br> 日期:2013-5-14 下午06:30:42 * * @return hibernate session对象 */ public Session getSession() { return SessionFactoryUtils.getSession(getSessionFactory(), true); }
protected final Session getSession() { return SessionFactoryUtils.getSession(sessionFactory, Boolean.FALSE); }
public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; this.session = SessionFactoryUtils.getSession(sessionFactory, true); }
public Session getSession() { boolean allowCreate = true; return SessionFactoryUtils.getSession(sessionFactory, allowCreate); }
@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()); } }
/* (non-Javadoc) * @see org.jobcenter.internalservice.SetUpHibernateSessionForNonWebRequestProcessing#openSession() */ public void openSession() { Session session = SessionFactoryUtils.getSession(sessionFactory, true); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); }