/** * Determine the DataSource of the given SessionFactory. * @param sessionFactory the SessionFactory to check * @return the DataSource, or {@code null} if none found * @see ConnectionProvider */ public static DataSource getDataSource(SessionFactory sessionFactory) { if (sessionFactory instanceof SessionFactoryImplementor) { try { ConnectionProvider cp = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry().getService( ConnectionProvider.class); if (cp != null) { return cp.unwrap(DataSource.class); } } catch (UnknownServiceException ex) { if (logger.isDebugEnabled()) { logger.debug("No ConnectionProvider found - cannot determine DataSource for SessionFactory: " + ex); } } } return null; }
private Object openSession() { LOG.debug("Openning new session for actual session factory, currently active open sessions: {}", currentOpenedSessions); Session realSession = this.realSessionFactory.openSession(); currentOpenedSessions++; SessionObserver observer = null; try { observer = ((SessionFactoryImplementor) realSessionFactory).getServiceRegistry().getService(SessionObserver.class); } catch (UnknownServiceException e) { // Do nothing } SessionProxyHandler proxyHandler = new SessionProxyHandler(this.realSessionFactory, realSession, observer); Session proxy = (Session) Proxy.newProxyInstance(Session.class.getClassLoader(), new Class<?>[] { Session.class, SessionImplementor.class }, proxyHandler); if (observer != null) { observer.sessionCreated(proxy); } return proxy; }
@Override public <R extends Service> R getService(Class<R> serviceRole) { final ServiceBinding<R> serviceBinding = locateServiceBinding( serviceRole ); if ( serviceBinding == null ) { throw new UnknownServiceException( serviceRole ); } R service = serviceBinding.getService(); if ( service == null ) { service = initializeService( serviceBinding ); } return service; }