@Override protected void tearDown() throws Exception { TransactionSynchronizationManager.unbindResource(sessionFactory); SessionFactoryUtils.releaseSession(session, sessionFactory); super.tearDown(); }
/** * Close the session and shut down the database. This method should be called once the tests are * complete. It should ordinarily be called from an <code>@AfterClass</code> method of the test * class. */ public void cleanUp() { /* * If the database is not shutdown, the changes will not be * persisted to the data file. It's useful to be able to inspect * the loaded data directly sometimes, so it's important to do * this. (The HSLQDB documentation suggests that comitted changes * will never be lost even if the database is not shut down. That * does not appear to be true.) */ session.createSQLQuery("shutdown").executeUpdate(); SessionFactoryUtils.releaseSession(session, sessionFactory); session = null; }
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"); }
/** * Close the given Hibernate Session, created via this DAO's SessionFactory, if it isn't bound to * the thread (i.e. isn't a transactional Session). * * <p>Typically used in plain Hibernate code, in combination with {@link #getSession} and {@link * #convertHibernateAccessException}. * * @param session the Session to close * @see org.springframework.orm.hibernate3.SessionFactoryUtils#releaseSession */ protected final void releaseSession(Session session) { SessionFactoryUtils.releaseSession(session, getSessionFactory()); }