/** * Build the Hibernate {@code SessionFactory} through background bootstrapping, * using the given executor for a parallel initialization phase * (e.g. a {@link org.springframework.core.task.SimpleAsyncTaskExecutor}). * <p>{@code SessionFactory} initialization will then switch into background * bootstrap mode, with a {@code SessionFactory} proxy immediately returned for * injection purposes instead of waiting for Hibernate's bootstrapping to complete. * However, note that the first actual call to a {@code SessionFactory} method will * then block until Hibernate's bootstrapping completed, if not ready by then. * For maximum benefit, make sure to avoid early {@code SessionFactory} calls * in init methods of related beans, even for metadata introspection purposes. * @since 4.3 * @see #buildSessionFactory() */ public SessionFactory buildSessionFactory(AsyncTaskExecutor bootstrapExecutor) { Assert.notNull(bootstrapExecutor, "AsyncTaskExecutor must not be null"); return (SessionFactory) Proxy.newProxyInstance(this.resourcePatternResolver.getClassLoader(), new Class<?>[] {SessionFactoryImplementor.class, InfrastructureProxy.class}, new BootstrapSessionFactoryInvocationHandler(bootstrapExecutor)); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if (method.getName().equals("equals")) { // Only consider equal when proxies are identical. return (proxy == args[0]); } else if (method.getName().equals("hashCode")) { // Use hashCode of EntityManagerFactory proxy. return System.identityHashCode(proxy); } else if (method.getName().equals("getProperties")) { return getProperties(); } else if (method.getName().equals("getWrappedObject")) { // Call coming in through InfrastructureProxy interface... return getSessionFactory(); } // Regular delegation to the target SessionFactory, // enforcing its full initialization... return method.invoke(getSessionFactory(), args); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } }
/** * Build the Hibernate {@code SessionFactory} through background bootstrapping, * using the given executor for a parallel initialization phase * (e.g. a {@link org.springframework.core.task.SimpleAsyncTaskExecutor}). * <p>{@code SessionFactory} initialization will then switch into background * bootstrap mode, with a {@code SessionFactory} proxy immediately returned for * injection purposes instead of waiting for Hibernate's bootstrapping to complete. * However, note that the first actual call to a {@code SessionFactory} method will * then block until Hibernate's bootstrapping completed, if not ready by then. * For maximum benefit, make sure to avoid early {@code SessionFactory} calls * in init methods of related beans, even for metadata introspection purposes. * @since 4.3 * @see #buildSessionFactory() */ public SessionFactory buildSessionFactory(AsyncTaskExecutor bootstrapExecutor) { Assert.notNull(bootstrapExecutor, "AsyncTaskExecutor must not be null"); return (SessionFactory) Proxy.newProxyInstance(this.resourcePatternResolver.getClassLoader(), new Class<?>[] {SessionFactoryImplementor.class, InfrastructureProxy.class}, new BootstrapSessionFactoryInvocationHandler(bootstrapExecutor)); }
/** * Build the Hibernate {@code SessionFactory} through background bootstrapping, * using the given executor for a parallel initialization phase * (e.g. a {@link org.springframework.core.task.SimpleAsyncTaskExecutor}). * <p>{@code SessionFactory} initialization will then switch into background * bootstrap mode, with a {@code SessionFactory} proxy immediately returned for * injection purposes instead of waiting for Hibernate's bootstrapping to complete. * However, note that the first actual call to a {@code SessionFactory} method will * then block until Hibernate's bootstrapping completed, if not ready by then. * For maximum benefit, make sure to avoid early {@code SessionFactory} calls * in init methods of related beans, even for metadata introspection purposes. * @since 4.3 * @see #buildSessionFactory() */ public SessionFactory buildSessionFactory(AsyncTaskExecutor bootstrapExecutor) { Assert.notNull(bootstrapExecutor, "AsyncTaskExecutor must not be null"); return (SessionFactory) Proxy.newProxyInstance(this.resourcePatternResolver.getClassLoader(), new Class<?>[] {SessionFactoryImplementor.class, InfrastructureProxy.class}, new BootstrapSessionFactoryInvocationHandler(bootstrapExecutor)); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if (method.getName().equals("equals")) { // Only consider equal when proxies are identical. return (proxy == args[0]); } else if (method.getName().equals("hashCode")) { // Use hashCode of EntityManagerFactory proxy. return System.identityHashCode(proxy); } else if (method.getName().equals("getProperties")) { return getProperties(); } else if (method.getName().equals("getWrappedObject")) { // Call coming in through InfrastructureProxy interface... return getSessionFactory(); } // Regular delegation to the target SessionFactory, // enforcing its full initialization... return method.invoke(getSessionFactory(), args); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } }