@Override
    public void run(T configuration, Environment environment) throws Exception {
        final String name = name();
        final String primaryName = name + PRIMARY;
        final String readerName = name + READER;
        final PooledDataSourceFactory primaryConfig = getDataSourceFactory(configuration);
        final SessionFactory primary = requireNonNull(sessionFactoryFactory.build(this, environment, primaryConfig,
            entities, primaryName));
        final PooledDataSourceFactory readerConfig = getReadSourceFactory(configuration);
        final SessionFactory reader = requireNonNull(sessionFactoryFactory.build(this, environment, readerConfig,
            entities, readerName));

        final DualSessionFactory factory = new DualSessionFactory(primary, reader);
        registerUnitOfWorkListenerIfAbsent(environment).registerSessionFactory(name, factory);

        final ExecutorService exec = environment.getHealthCheckExecutorService();
        environment.healthChecks().register(primaryName,
                                            new SessionFactoryHealthCheck(
                                                    exec,
                                                    primaryConfig.getValidationQueryTimeout().orElse(Duration.seconds(5)),
                                                    primary,
                                                    primaryConfig.getValidationQuery()));
        environment.healthChecks().register(readerName,
                                            new SessionFactoryHealthCheck(
                                                    exec,
                                                    readerConfig.getValidationQueryTimeout().orElse(Duration.seconds(5)),
                                                    reader,
                                                    readerConfig.getValidationQuery()));

        this.sessionFactory = factory;
    }
@Test
    public void registersACustomNameOfHealthCheckAndDBPoolMetrics() throws Exception {
        final String name = "custom-hibernate";

        final HibernateBundle<Configuration> customBundle = new HibernateBundle<Configuration>(entities, factory) {
            @Override
            public DataSourceFactory getDataSourceFactory(Configuration configuration) {
                return dbConfig;
            }

            @Override
            public DataSourceFactory getReadSourceFactory(Configuration configuration) {
                return readConfig;
            }

            @Override
            protected String name() {
                return name;
            }
        };
        when(factory.build(eq(customBundle),
                any(Environment.class),
                any(DataSourceFactory.class),
                anyList(),
                eq(name + PRIMARY))).thenReturn(sessionFactory);
        when(factory.build(eq(customBundle),
                any(Environment.class),
                any(DataSourceFactory.class),
                anyList(),
                eq(name + READER))).thenReturn(readFactory);

        customBundle.run(configuration, environment);

        final ArgumentCaptor<SessionFactoryHealthCheck> captor =
                ArgumentCaptor.forClass(SessionFactoryHealthCheck.class);
        verify(healthChecks).register(eq(name + READER), captor.capture());
    }