Given that separately-built `DateSource`s have separate pools, I don't think you'll get away from having to force the creation a single `ManagedDataSource` that is shared across bundles. You could create a `MemoizingDataSourceFactory` using something like Guava's `Suppliers.memoize(Supplier)` <https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/Suppliers.html#memoize(com.google.common.base.Supplier)>, but it would be functionally equivalent to what you have in your example.
On Thu, Aug 11, 2016 at 8:50 AM, 'Robert Gacki' via dropwizard-user < [email protected]> wrote: > Hi, > > I am using the *HibernateBundle* in my application. But that bundle is > not the only component that should use database connections to the > database. And I don't want to have a pooled DS per component in my > application. I want Hibernate to use the same pooled DS as other components > that require access to the same database. > > Example: > public class MyApplication extends Application<MyConfiguration> { > > private final HibernateBundle<MyConfiguration> hibernateBundle; > private final SomeOtherBundle<MyConfiguration> someOtherBundle; > > public MyApplication() { > this.hibernateBundle = new ScanningHibernateBundle< > MyConfiguration>(ENTITIES_PATH) { > @Override > public PooledDataSourceFactory getDataSourceFactory(final > MyConfiguration configuration) { > return configuration.getDataSourceFactory(); > } > }; > this.someOtherBundle = new SomeOtherBundle() { > @Override > public PooledDataSourceFactory getDataSourceFactory(final > MyConfiguration configuration) { > return configuration.getDataSourceFactory(); > } > }; > } > > @Override > public void initialize(final Bootstrap<HotelBookingConfiguration> > bootstrap) { > super.initialize(bootstrap); > bootstrap.addBundle(hibernateBundle); > bootstrap.addBundle(someOtherBundle); > } > > } > > Lets say, I configure 100 connections for the pool. Now my application > would use 200 connections. Of course, that's bad because I don't need all > these connections. > > I am considering to have my own implementation of > a PooledDataSourceFactory, a wrapper for the original PooledDataSourceFactory > implementation. The wrapper keeps the first created DS and returns it for > subsequent requests. The ugly part is that the wrapper must also control > the lifecycle of the DS. > > Example: > public class SharedDataSourceFactory implements PooledDataSourceFactory, > Managed { > > private final DataSourceFactory wrapped = new DataSourceFactory(); > > private ManagedDataSource dataSource; > private final Object monitor = new Object(); > > // ... Delegations > > @Override > public ManagedDataSource build(MetricRegistry metricRegistry, String > name) { > synchonized (monitor) { > if (this.dataSource == null) { > this.dataSource = wrapped.build(metricRegistry, "shared"); > } > return this.dataSource; > } > } > > // ... Managed overhead to clean-up the 'dataSource' instance. > > } > > public class MyConfiguration extends Configuration { > > private final SharedDataSourceFactory dataSourceFactory = new > SharedDataSourceFactory(); > > } > > public class MyApplication extends Application<MyConfiguration> { > > private final HibernateBundle<MyConfiguration> hibernateBundle; > private final SomeOtherBundle<MyConfiguration> someOtherBundle; > > public MyApplication() { > final PooledDataSourceFactory > > this.hibernateBundle = new ScanningHibernateBundle< > MyConfiguration>(ENTITIES_PATH) { > @Override > public PooledDataSourceFactory getDataSourceFactory(final > MyConfiguration configuration) { > return configuration.getDataSourceFactory(); > } > }; > this.someOtherBundle = new SomeOtherBundle() { > @Override > public PooledDataSourceFactory getDataSourceFactory(final > MyConfiguration configuration) { > return configuration.getDataSourceFactory(); > } > }; > } > > @Override > public void run(final MyConfiguration configuration, > final Environment environment) throws Exception { > environment.lifecycle().manage(configuration.getDataSourceFactory()); > } > > @Override > public void initialize(final Bootstrap<MyConfiguration> bootstrap) { > super.initialize(bootstrap); > bootstrap.addBundle(hibernateBundle); > bootstrap.addBundle(someOtherBundle); > } > > } > > This feels like a hack to me. > > Did anyone face this problem already? What is the best approach? > > > Thanks, > Robert > > > -- > You received this message because you are subscribed to the Google Groups > "dropwizard-user" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- Evan Meagher -- You received this message because you are subscribed to the Google Groups "dropwizard-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
