List of usage examples for org.hibernate SessionFactory getCurrentSession
Session getCurrentSession() throws HibernateException;
From source file:org.horizontaldb.shard.hibernate.AbstractDaoEnricherTest.java
License:Apache License
@Test public void shouldSetAndUnsetDistinctSessionsForDaoUsingDistinctContexts() { AbstractDaoEnricher enricher = new AbstractDaoEnricher(txManager); ShardContext shardContext = new ShardContext("testClient"); ShardContext shardContext1 = new ShardContext("testClient1"); ShardContext shardContext2 = new ShardContext("testClient2"); SessionFactory mockSessionFactory = EasyMock.createMock(SessionFactory.class); Session mockSession = EasyMock.createMock("mockSession", Session.class); Session mockSession1 = EasyMock.createMock("mockSession1", Session.class); Session mockSession2 = EasyMock.createMock("mockSession2", Session.class); AbstractDao mockDao = EasyMock.createMock("mockDao", AbstractDao.class); // called during statistics logging expect(mockSession2.getSessionFactory()).andReturn(mockSessionFactory); expect(mockSession1.getSessionFactory()).andReturn(mockSessionFactory); expect(mockSession.getSessionFactory()).andReturn(mockSessionFactory); expect(mockSessionFactory.getStatistics()).andReturn(null).times(3); // called during statistics logging expect(txManager.getSessionFactory()).andReturn(mockSessionFactory).times(3); expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession); expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession1); expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession2); // setups/*from w w w .j av a 2 s . c o m*/ mockDao.setSession(mockSession); mockDao.setSession(mockSession1); mockDao.setSession(mockSession2); // tearDowns mockDao.setSession(mockSession1); mockDao.setSession(mockSession); mockDao.setSession(null); replay(txManager, mockSessionFactory, mockSession, mockSession1, mockSession2, mockDao); enricher.setup(mockDao, shardContext); enricher.setup(mockDao, shardContext1); enricher.setup(mockDao, shardContext2); enricher.tearDown(mockDao, shardContext2); enricher.tearDown(mockDao, shardContext1); enricher.tearDown(mockDao, shardContext); verify(txManager, mockSessionFactory, mockSession, mockSession1, mockSession2, mockDao); }
From source file:org.horizontaldb.shard.hibernate.AbstractDaoEnricherTest.java
License:Apache License
@Test public void shouldSetAndUnsetProperSessionsForDistinctDaosUsingDistinctContexts() { AbstractDaoEnricher enricher = new AbstractDaoEnricher(txManager); ShardContext shardContext = new ShardContext("testClient"); ShardContext shardContext1 = new ShardContext("testClient1"); ShardContext shardContext2 = new ShardContext("testClient2"); SessionFactory mockSessionFactory = EasyMock.createMock(SessionFactory.class); Session mockSession = EasyMock.createMock("mockSession", Session.class); Session mockSession1 = EasyMock.createMock("mockSession1", Session.class); Session mockSession2 = EasyMock.createMock("mockSession2", Session.class); AbstractDao mockDao = EasyMock.createMock("mockDao", AbstractDao.class); DepartmentDaoImpl mockDao1 = EasyMock.createMock("mockDao1", DepartmentDaoImpl.class); PersonDaoImpl mockDao2 = EasyMock.createMock("mockDao2", PersonDaoImpl.class); // called during statistics logging expect(mockSession2.getSessionFactory()).andReturn(mockSessionFactory); expect(mockSession1.getSessionFactory()).andReturn(mockSessionFactory); expect(mockSession.getSessionFactory()).andReturn(mockSessionFactory); expect(mockSessionFactory.getStatistics()).andReturn(null).times(3); // called during statistics logging expect(txManager.getSessionFactory()).andReturn(mockSessionFactory).times(3); expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession); expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession1); expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession2); // setups/*from w ww . j a v a2 s . c o m*/ mockDao.setSession(mockSession); mockDao1.setSession(mockSession); mockDao2.setSession(mockSession1); mockDao1.setSession(mockSession2); // tearDowns mockDao1.setSession(mockSession); mockDao2.setSession(null); mockDao1.setSession(null); mockDao.setSession(null); replay(txManager, mockSessionFactory, mockSession, mockSession1, mockSession2, mockDao, mockDao1, mockDao2); enricher.setup(mockDao, shardContext); enricher.setup(mockDao1, shardContext); enricher.setup(mockDao2, shardContext1); enricher.setup(mockDao1, shardContext2); enricher.tearDown(mockDao1, shardContext2); enricher.tearDown(mockDao2, shardContext1); enricher.tearDown(mockDao1, shardContext); enricher.tearDown(mockDao, shardContext); verify(txManager, mockSessionFactory, mockSession, mockSession1, mockSession2, mockDao, mockDao1, mockDao2); }
From source file:org.jasig.portlet.announcements.Exporter.java
License:Apache License
public static void main(String[] args) throws Exception { String dir = args[0];// w ww. j ava2 s.c om String importExportContext = args[1]; ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext); SessionFactory sessionFactory = context.getBean(SESSION_FACTORY_BEAN_NAME, SessionFactory.class); IAnnouncementService announcementService = context.getBean(ANNOUNCEMENT_SVC_BEAN_NAME, IAnnouncementService.class); Session session = sessionFactory.getCurrentSession(); Transaction transaction = session.beginTransaction(); JAXBContext jc = JAXBContext.newInstance(Topic.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); List<Topic> topics = announcementService.getAllTopics(); for (Topic topic : topics) { if (topic.getSubscriptionMethod() == 4) { continue; } session.lock(topic, LockMode.NONE); JAXBElement<Topic> je2 = new JAXBElement<Topic>(new QName("topic"), Topic.class, topic); String output = dir + File.separator + UUID.randomUUID().toString() + ".xml"; System.out.println("Exporting Topic " + topic.getId() + " to file " + output); try { marshaller.marshal(je2, new FileOutputStream(output)); } catch (Exception exception) { exception.printStackTrace(); } } transaction.commit(); }
From source file:org.jasig.portlet.data.Exporter.java
License:Apache License
public static void main(String[] args) throws Exception { String dir = args[0];// www. j av a2 s . com String importExportContext = args[1]; String sessionFactoryBeanName = args[2]; String modelClassName = args[3]; String serviceBeanName = args[4]; String serviceBeanMethodName = args[5]; ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext); SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class); Class<?> modelClass = Class.forName(modelClassName); Object service = context.getBean(serviceBeanName); Session session = sessionFactory.getCurrentSession(); Transaction transaction = session.beginTransaction(); JAXBContext jc = JAXBContext.newInstance(modelClass); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Method method = service.getClass().getMethod(serviceBeanMethodName); List<?> objects = (List<?>) method.invoke(service, null); for (Object o : objects) { session.lock(o, LockMode.NONE); JAXBElement je2 = new JAXBElement(new QName(modelClass.getSimpleName().toLowerCase()), modelClass, o); String output = dir + File.separator + UUID.randomUUID().toString() + ".xml"; try { marshaller.marshal(je2, new FileOutputStream(output)); } catch (Exception exception) { exception.printStackTrace(); } } transaction.commit(); }
From source file:org.jasig.portlet.data.Importer.java
License:Apache License
public static void main(String[] args) throws Exception { String dir = args[0];//from www . j a va 2 s . co m String importExportContext = args[1]; String sessionFactoryBeanName = args[2]; String modelClassName = args[3]; String serviceBeanName = args[4]; String serviceBeanMethodName = args[5]; ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext); SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class); Class<?> modelClass = Class.forName(modelClassName); Object service = context.getBean(serviceBeanName); JAXBContext jc = JAXBContext.newInstance(modelClass); File folder = new File(dir); File[] files = folder.listFiles(new ImportFileFilter()); for (File f : files) { StreamSource xml = new StreamSource(f.getAbsoluteFile()); Unmarshaller unmarshaller = jc.createUnmarshaller(); JAXBElement je1 = unmarshaller.unmarshal(xml, modelClass); Object object = je1.getValue(); Session session = sessionFactory.getCurrentSession(); Transaction transaction = session.beginTransaction(); Method method = service.getClass().getMethod(serviceBeanMethodName, modelClass); method.invoke(service, object); transaction.commit(); } }
From source file:org.javasoft.ciclope.servlets.utils.SessionUtils.java
public static Session getCiclopeSession() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.getCurrentSession(); Transaction t = s.getTransaction();//from w w w. j a va 2 s. c o m if (t.isActive()) { t.rollback(); } if (!s.isOpen()) { s = sf.getCurrentSession(); } return s; }
From source file:org.jboss.hibernate.session.HibernateContext.java
License:Open Source License
/** * Retreives an "unmanaged" session against the same underlying jdbc connnection as the session * currently bound to the current context for the given JNDI name. This is simply a convenience * method for SessionFactory.openSession({@link #getSession}.connection()). Unmanaged here means that * the returned session is not controlled by the code managing the actually bound session; callers * are required to cleanup these sessions manually using {@link #releaseUnmanagedSession}. * * @param name The "name" of the {@link org.hibernate.SessionFactory} * for which an unmanaged session is requested. * @return An unmanaged session.//from w w w . ja v a 2 s .c o m * @throws HibernateException If an error occurs opening the new Session. * @throws IllegalStateException If unable to locate a managed Session for the current context. * * @deprecated Given a SessionFactory, sf (usually obtained from JNDI), this method is equivalent to * <pre>sf.openSession( sf.getCurrentSession().connection() )</pre> */ public static Session getUnmanagedSession(String name) throws HibernateException, IllegalStateException { SessionFactory sf = locateSessionFactory(name); return sf.openSession(sf.getCurrentSession().connection()); }
From source file:org.jboss.pnc.datastore.repositories.DefaultSequenceHandlerRepository.java
License:Open Source License
@Override public Long getNextID(final String sequenceName) { ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() { @Override//from w ww. j a va 2s . c o m public Long execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection)); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(dialect.getSequenceNextValString(sequenceName)); resultSet = preparedStatement.executeQuery(); resultSet.next(); return resultSet.getLong(1); } catch (SQLException e) { throw e; } finally { if (preparedStatement != null) { preparedStatement.close(); } if (resultSet != null) { resultSet.close(); } } } }; Session session = (Session) entityManager.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); Long maxRecord = sessionFactory.getCurrentSession().doReturningWork(maxReturningWork); return maxRecord; }
From source file:org.jboss.pnc.datastore.repositories.DefaultSequenceHandlerRepository.java
License:Open Source License
@Override public void createSequence(final String sequenceName) { if (sequenceExists(sequenceName)) { return;//from w ww .j a v a2 s. com } Work work = new Work() { @Override public void execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection)); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection .prepareStatement(dialect.getCreateSequenceStrings(sequenceName, 1, 1)[0]); preparedStatement.execute(); } catch (SQLException e) { throw e; } finally { if (preparedStatement != null) { preparedStatement.close(); } if (resultSet != null) { resultSet.close(); } } } }; Session session = (Session) entityManager.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); sessionFactory.getCurrentSession().doWork(work); }
From source file:org.jboss.pnc.datastore.repositories.DefaultSequenceHandlerRepository.java
License:Open Source License
@Override public boolean sequenceExists(final String sequenceName) { ReturningWork<Boolean> work = new ReturningWork<Boolean>() { @Override/*from ww w . j av a 2s . co m*/ public Boolean execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection)); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(dialect.getQuerySequencesString()); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { if (sequenceName.equals(resultSet.getString(1))) { return true; } } } catch (SQLException e) { throw e; } finally { if (preparedStatement != null) { preparedStatement.close(); } if (resultSet != null) { resultSet.close(); } } return false; } }; Session session = (Session) entityManager.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); return sessionFactory.getCurrentSession().doReturningWork(work); }