Another approach is to use UnitOfWorkAwareProxyFactory to create the command objects and annotate the command object methods with @UnitOfWork. In general, UnitOfWorkAwareProxyFactory can create proxy objects will all the UnitOfWork configuration as used in resources.
On Thursday, March 15, 2018 at 6:34:22 AM UTC-7 [email protected] wrote: > I eventually made it work and I will share the solution I adopted. > > The code I worked on implemented ConfiguredCommand inside a Dropwizard > application. In order to connect to the DB, its run method configured > Hibernate. This was done in 3 steps: 1) set up of the HibernateFactory and > create an HibernateBundle, 2) configure Hibernate properties > (configuration.getDataSourceFactory().getProperties().put(..)) and 3) > initialization by using the hibernateBundle run method which received a > newly created Environment object. The command's code then had to manage the > transactions and commits explicitly instead of using @UnitOfWork. > > It worth noting that another HibernateBundle was created by the > application itself before running the command code. Either this or the > initialization of Hibernate in the cli's run method altogether might have > been the cause of the inability to use the @UnitOfWork annotation. > > What I figured out is that Dropwizard provides another abstract class, > EnvironmentCommand, whose constructor takes an Application instance other > than the command name and description (as for the ConfiguredCommand). Its > javadoc drily describes: "A command which executes with a configured {@link > Environment}." . By extending such a class and passing to it the > Application instance of which the command is part, the @UnitOfWork is able > to use the HibernateBundle. As usual, this is possible only if the classes > using such annotation are instantiated by a UnitOfWorkAwareProxyFactory. > > My understanding is that the UnitOfWork and its factory uses the > environment of the main application and wouldn't recognize other > environement and bundles instantiated within the command. As a final note, > I am not sure whether the EnvironmentCommand comes with components > unnecessary for the execution of our command and if it were possible and > recommendable to twist the previous ConfiguratedCommand approach so that > the UnitOfWork used the new Environment and bundle. > -- 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]. To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/d7c8efe1-b0fd-42d3-8cce-a1a3940df403n%40googlegroups.com.
