try { session = factorySupplier.get() .withOptions() .tenantIdentifier( tenant ) .openSession();
try { session = factorySupplier.get() .withOptions() .tenantIdentifier( tenant ) .openSession();
private void doInSession(String tenant, Consumer<Session> function) { Session session = null; Transaction txn = null; try { session = sessionFactory .withOptions() .tenantIdentifier( tenant ) .openSession(); txn = session.getTransaction(); txn.begin(); function.accept(session); txn.commit(); } catch (Throwable e) { if ( txn != null ) txn.rollback(); throw e; } finally { if (session != null) { session.close(); } } } //end::multitenacy-hibernate-session-example[]
private void doInSession(String tenant, Consumer<Session> function, boolean useTenantTimeZone) { Session session = null; Transaction txn = null; try { SessionBuilder sessionBuilder = sessionFactory .withOptions() .tenantIdentifier( tenant ); if ( useTenantTimeZone ) { sessionBuilder.jdbcTimeZone( timeZoneTenantMap.get( tenant ) ); } session = sessionBuilder.openSession(); txn = session.getTransaction(); txn.begin(); function.accept( session ); txn.commit(); } catch (Throwable e) { if ( txn != null ) { txn.rollback(); } throw e; } finally { if ( session != null ) { session.close(); } } } //end::multitenacy-hibernate-timezone-configuration-session-example[]
@Test public void testSessionInterceptor() { EntityManagerFactory entityManagerFactory = entityManagerFactory(); Serializable customerId = 1L; //tag::events-interceptors-session-scope-example[] SessionFactory sessionFactory = entityManagerFactory.unwrap( SessionFactory.class ); Session session = sessionFactory .withOptions() .interceptor(new LoggingInterceptor() ) .openSession(); session.getTransaction().begin(); Customer customer = session.get( Customer.class, customerId ); customer.setName( "Mr. John Doe" ); //Entity Customer#1 changed from [John Doe, 0] to [Mr. John Doe, 0] session.getTransaction().commit(); //end::events-interceptors-session-scope-example[] session.close(); }
Interceptor entityInterceptor = getEntityInterceptor(); Session newSession = (entityInterceptor != null ? obtainSessionFactory().withOptions().interceptor(entityInterceptor).openSession() : obtainSessionFactory().openSession()); if (logger.isDebugEnabled()) {
Interceptor entityInterceptor = getEntityInterceptor(); Session newSession = (entityInterceptor != null ? obtainSessionFactory().withOptions().interceptor(entityInterceptor).openSession() : obtainSessionFactory().openSession()); if (logger.isDebugEnabled()) {
private Session createSession(RequestContext context) { Session session; if (entityInterceptor != null) { session = sessionFactory.withOptions().interceptor(entityInterceptor).openSession(); } else { session = sessionFactory.openSession(); } session.setHibernateFlushMode(FlushMode.MANUAL); return session; }
@Override public SessionBuilder withOptions() { return delegate.withOptions(); }
@Override public SessionBuilder withOptions() { return delegate.withOptions(); }
public SessionBuilder withOptions() { return delegate.withOptions(); }
@Override public SessionBuilder withOptions() { return getImpl().withOptions(); }
@Override public SessionBuilder withOptions() { return getImpl().withOptions(); }
private static Session openSession(String tenantId) { return sessionFactory.withOptions() .tenantIdentifier( tenantId ).openSession(); } }
public Interceptions(SessionFactory sessionFactory, Set<Object> ignore) { oldStateSession = sessionFactory.withOptions().noInterceptor().openSession(); this.ignore = ignore; }
private Session createSession(RequestContext context) { Session session; if (entityInterceptor != null) { session = sessionFactory.withOptions().interceptor(entityInterceptor).openSession(); } else { session = sessionFactory.openSession(); } session.setHibernateFlushMode(FlushMode.MANUAL); return session; }
private Session openSessionWithTenantId(String tenantId) { return getSessionFactory().withOptions().tenantIdentifier( tenantId ).openSession(); }
public static <T> List<T> findIndexedResultsInTenant(EntityManagerFactory emf, Class<T> clazz, String key, String value, String tenantId) { SessionFactory sessionFactory = emf.unwrap( SessionFactory.class ); try ( Session session = sessionFactory.withOptions().tenantIdentifier( tenantId ).openSession() ) { return find( session, clazz, key, value ); } }
public static void withTxSession(boolean useJta, SessionFactory sessionFactory, ThrowingConsumer<Session, Exception> consumer) throws Exception { JtaPlatform jtaPlatform = useJta ? sessionFactory.getSessionFactoryOptions().getServiceRegistry().getService(JtaPlatform.class) : null; withTxSession(jtaPlatform, sessionFactory.withOptions(), consumer); }
public static <T> T withTxSessionApply(boolean useJta, SessionFactory sessionFactory, ThrowingFunction<Session, T, Exception> function) throws Exception { JtaPlatform jtaPlatform = useJta ? sessionFactory.getSessionFactoryOptions().getServiceRegistry().getService(JtaPlatform.class) : null; return withTxSessionApply(jtaPlatform, sessionFactory.withOptions(), function); }