[ 
https://issues.apache.org/jira/browse/ONAMI-117?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14952047#comment-14952047
 ] 

Stephan Classen commented on ONAMI-117:
---------------------------------------

Sorry, I was a little busy.

Your example cannot work. Because you bind two instances of the 
AllPersistenceServices. Each one of them in a private module. But you don't 
expose them to the public. Therefore you cannot access them outside of the 
private modules.

Your use case is currently not supported directly using onami persist.
Onami persist assumes that all persistence units are defined in a single module 
in the application.

It is possible to define multiple persistence modules like you are planing to 
do using private modules. But this requires additional work in the application.
What you basically need to do is:
- bind each persistence module in a private module
- bind the instances you need in public (i.e. AllPersistenceServices, 
AllUnitsOfWork, PersistenceFilter) with an annotation (within the private 
module)
- expose the instances with annotation from the private modules to the public
- in the public use the annotated instances to start/stop persistence service, 
filter requests, control the units of work.
The main downside is that the public needs to know which annotated instances 
are available.

There is a second way to allow different application components to define there 
own persistence units.
you would need to allow the application components to register a callback which 
will be executed when the injector is created.
The following is pseudo code to show the basic idea. no claim that it compiles 
or is of any use.


{code:java}
public class MultiPuModule extends PersistenceModule {

    private final List<PuCallback> callbacks;

    public void registerPuCallback(PuCallback callback) {
        callbacks.add(callback);
    }

    protected void configurePersistence() {
        for (PuCallback c in callbacks) {
            c.configurePersistence(this);
        }
    }

    // expose protected super method as public method
    public UnannotatedPersistenceUnitBuilder 
bindContainerManagedPersistenceUnit(EntityManagerFactory emf) {
        super.bindContainerManagedPersistenceUnit(emf);
    }
}

public interface PuCallback {
    void configurePersistence(MultiPuModule puModule);
}
{code}

all that is left open is how the callback gets to the MultiPuModule. Either the 
MultiPuModule gets passed to every component to allow the component to register 
the callback.
Or the component offers a getter and the central setup code will fetch all 
callbacks and pass them to the MultiPuModule.

I hope to point you in the right direction. If you have further questions I am 
more than happy to answer them.

> Multiple JTA datasource in standalone application
> -------------------------------------------------
>
>                 Key: ONAMI-117
>                 URL: https://issues.apache.org/jira/browse/ONAMI-117
>             Project: Apache Onami
>          Issue Type: Question
>          Components: persist
>    Affects Versions: persist-1.0.1
>            Reporter: Nicolas MARQUES
>
> I use multiple datasource in a JTA environnement (with 
> BitronixTransactionManager). 
> To do that, i create a privateModule for each datasource environment with a 
> persistentModule installed.
> I followed the given example  :
> {code}        bindContainerManagedPersistenceUnit(entityManagerFactory)
> .annotatedWith(clazz)
> .useGlobalTransactionWithJndiName(JTATransactionManagerServiceImpl.JNDI_USER_TRANSACTION);
> {panel}
> In my main class, the problem is that i cannot access to the 
> PersistentService since there is not an only one neither to  
> AllPersistenceServices class.
> {code}
> Exception in thread "main" com.google.inject.ConfigurationException: Guice 
> configuration errors:
> 1) Unable to create binding for 
> org.apache.onami.persist.AllPersistenceServices. It was already configured on 
> one or more child injectors or private modules
>     bound at 
> org.apache.onami.persist.PersistenceModule.configurePersistenceUnits(PersistenceModule.java:80)
>   If it was in a PrivateModule, did you forget to expose the binding?
>   while locating org.apache.onami.persist.AllPersistenceServices
> 1 error
>       at 
> com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
>       at 
> com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961)
>       at 
> com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
>       at nico.jta.JPAClient.main(JPAClient.java:49)
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to