List of usage examples for org.hibernate SessionFactory getCurrentSession
Session getCurrentSession() throws HibernateException;
From source file:org.openmrs.BaseOpenmrsObjectTest.java
License:Mozilla Public License
/** * @see BaseOpenmrsObject#equals(Object) *//*from w w w. ja v a2 s . com*/ @Test public void equals_shouldReturnTrueIfHibernateProxyOfSomeObjectComparedToAnotherHibernateProxyOfTheSameObject() { SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory"); Session session = sessionFactory.getCurrentSession(); Assert.assertTrue(session.load(Patient.class, 2).equals((session.load(Patient.class, 2)))); }
From source file:org.openmrs.module.auditlog.AuditLogHelper.java
License:Open Source License
public boolean isAudited(Class<?> clazz) { //We need to stop hibernate auto flushing which might happen as we fetch //the GP values, Otherwise if a flush happens, then the interceptor //logic will be called again which will result in an infinite loop/stack overflow if (exceptionsTypeCache == null || auditingStrategyCache == null) { SessionFactory sf = DAOUtils.getSessionFactory(); FlushMode originalFlushMode = sf.getCurrentSession().getFlushMode(); sf.getCurrentSession().setFlushMode(FlushMode.MANUAL); try {/*w w w . j a v a 2 s.c om*/ return isAuditedInternal(clazz); } finally { //reset sf.getCurrentSession().setFlushMode(originalFlushMode); } } return isAuditedInternal(clazz); }
From source file:org.openmrs.module.auditlog.AuditLogHelper.java
License:Open Source License
/** * Checks if the specified type is implicitly audit * //from ww w .j ava2 s . c om * @should return true if a class is implicitly audited * @should return false if a class is not implicitly marked as audited * @should return false if a class is already explicitly marked already as audited * @should return true if a class is implicitly audited and strategy is all except * @should return false if a class is not implicitly audited and strategy is all except * @should return false if a class is already explicitly audited and strategy is all except */ public boolean isImplicitlyAudited(Class<?> clazz) { //We need to stop hibernate auto flushing which might happen as we fetch //the GP values, Otherwise if a flush happens, then the interceptor //logic will be called again which will result in an infinite loop/stack overflow if (implicitlyAuditedTypeCache == null) { SessionFactory sf = DAOUtils.getSessionFactory(); FlushMode originalFlushMode = sf.getCurrentSession().getFlushMode(); sf.getCurrentSession().setFlushMode(FlushMode.MANUAL); try { return isImplicitlyAuditedInternal(clazz); } finally { //reset sf.getCurrentSession().setFlushMode(originalFlushMode); } } return isImplicitlyAuditedInternal(clazz); }
From source file:org.openmrs.module.conceptpubsub.api.db.hibernate.interceptor.LocalMappingHibernateInterceptor.java
License:Open Source License
private FlushMode setFlushMode(FlushMode flushMode) { //We need to get sessionFactory lazily here, because when the interceptor is instantiated Hibenate is not yet ready to work. SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory"); FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode(); sessionFactory.getCurrentSession().setFlushMode(flushMode); return previousFlushMode; }
From source file:org.openmrs.module.metadatamapping.api.db.hibernate.interceptor.LocalMappingHibernateInterceptor.java
License:Open Source License
/** * Gets the current hibernate session while taking care of the hibernate 3 and 4 differences. * //from w ww .ja v a2 s .co m * @return the current hibernate session. */ private org.hibernate.Session getCurrentSession() { SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory"); try { return sessionFactory.getCurrentSession(); } catch (NoSuchMethodError ex) { try { Method method = sessionFactory.getClass().getMethod("getCurrentSession", null); return (org.hibernate.Session) method.invoke(sessionFactory, null); } catch (Exception e) { throw new RuntimeException("Failed to get the current hibernate session", e); } } }
From source file:org.openmrs.module.soundex.advisor.PatientServiceAroundAdvisor.java
License:Open Source License
/** * Get the current Hibernate session./*from w ww . j av a2s .c o m*/ * @return the current hibernate session */ private static Session getCurrentSession() { SessionFactory sf = Context.getRegisteredComponents(SessionFactory.class).get(0); return sf.getCurrentSession(); }
From source file:org.openmrs.test.BaseContextSensitiveTest.java
License:Mozilla Public License
/** * Get the database connection currently in use by the testing framework. * <p>//from ww w. j ava 2 s.c om * Note that if you commit a transaction, any changes done by a test will not be rolled back and * you will need to clean up yourself by calling for example {@link #deleteAllData()}. * * @return Connection jdbc connection to the database */ @SuppressWarnings("deprecation") public Connection getConnection() { SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory"); return sessionFactory.getCurrentSession().doReturningWork(new ReturningWork<Connection>() { @Override public Connection execute(Connection connection) throws SQLException { return connection; } }); }
From source file:org.openplans.delayfeeder.LoadFeeds.java
License:Open Source License
public static void main(String args[]) throws HibernateException, IOException { if (args.length != 1) { System.out.println("expected one argument: the path to a csv of agency,route,url"); }/*w w w . j a v a 2s .co m*/ GenericApplicationContext context = new GenericApplicationContext(); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context); xmlReader.loadBeanDefinitions("org/openplans/delayfeeder/application-context.xml"); xmlReader.loadBeanDefinitions("data-sources.xml"); SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory"); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); FileReader fileReader = new FileReader(new File(args[0])); BufferedReader bufferedReader = new BufferedReader(fileReader); while (bufferedReader.ready()) { String line = bufferedReader.readLine().trim(); if (line.startsWith("#")) { continue; } if (line.length() < 3) { //blank or otherwise broken line continue; } String[] parts = line.split(","); String agency = parts[0]; String route = parts[1]; String url = parts[2]; Query query = session.createQuery("from RouteFeed where agency = :agency and route = :route"); query.setParameter("agency", agency); query.setParameter("route", route); @SuppressWarnings("rawtypes") List list = query.list(); RouteFeed feed; if (list.size() == 0) { feed = new RouteFeed(); feed.agency = agency; feed.route = route; feed.lastEntry = new GregorianCalendar(); } else { feed = (RouteFeed) list.get(0); } if (!url.equals(feed.url)) { feed.url = url; feed.lastEntry.setTimeInMillis(0); } session.saveOrUpdate(feed); } tx.commit(); }
From source file:org.ow2.bonita.env.descriptor.HibernateSessionDescriptor.java
License:Open Source License
public Object construct(WireContext wireContext) { Environment environment = Environment.getCurrent(); if (environment == null) { throw new WireException("no environment"); }/*www . ja v a2s .c o m*/ // get the hibernate-session-factory SessionFactory sessionFactory = null; if (factoryName != null) { sessionFactory = (SessionFactory) wireContext.get(factoryName); } else { sessionFactory = environment.get(SessionFactory.class); } if (sessionFactory == null) { String message = ExceptionManager.getInstance().getFullMessage("bp_HSD_1", (factoryName != null ? "'" + factoryName + "'" : "by type ")); throw new WireException(message); } // open the hibernate-session Session session = null; if (useCurrent) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("getting current hibernate session"); } session = sessionFactory.getCurrentSession(); } else if (connectionName != null) { Connection connection = (Connection) wireContext.get(connectionName); if (LOG.isLoggable(Level.FINE)) { LOG.fine("creating hibernate session with connection " + connection); } session = sessionFactory.openSession(connection); } else { if (LOG.isLoggable(Level.FINE)) { LOG.fine("creating hibernate session"); } session = sessionFactory.openSession(); } StandardTransaction standardTransaction = environment.get(StandardTransaction.class); if (standardTransaction != null) { HibernateSessionResource hibernateSessionResource = new HibernateSessionResource(session); standardTransaction.enlistResource(hibernateSessionResource); } return session; }
From source file:org.owasp.dependencytrack.util.session.RunWithSession.java
License:Open Source License
public static <T> T run(SessionFactory sessionFactory, DBSessionTaskReturning<T> runnable) { Session session = null;/*from ww w. j a v a 2 s . co m*/ try { session = sessionFactory.getCurrentSession(); } catch (Exception e) { } boolean wasClosed = session == null || !session.isOpen(); if (wasClosed) { session = sessionFactory.openSession(); } if (session != null) { try { return runnable.run(session); } finally { if (wasClosed && session.isOpen()) { session.close(); } } } return null; }