Skip to content

Support for multiple DataSources or Environment  #709

Open
@robotdan

Description

@robotdan

Description

Mybatis supports multiple environments which should allow you to bind a mapper to more than one DataSource. This would allow me to decide at runtime which connection pool I want to use based upon the request.

From what I can tell, MyBatis Spring does support this concept. The MyBatis Guice module assumes a single DataSource or single Environment.

What I'd like to be able to do is bind one to many DataSource instances, by way of DataSourceProvider , EnvironmentProvider, or something else.

Example usage at runtime:

private final UserMapper primaryUserMapper;

private final UserMapper secondaryUserMapper;

@Inject
public ExampleService(@Named("Primary") UserMapper primaryUserMapper, 
                      @Named("Secondary") UserMapper secondaryUserMapper) {
  this.primaryUserMapper = primaryUserMapper;
  this.secondaryUserMapper = secondaryUserMapper;
}

I have find a few SO articles that claim to have pulled it off using a PrivateModule, but I have yet to get them to work for my use case.

Here is one of the examples:

Have you considered adding this support, or would you be open to accepting a PR to add this support? If you would be open to accepting a PR, do you have any opinion or suggestion on what would need to change to the current module?

I am still working to get more of a complete solution working if I can pull it off, I would be open to either trying to retro fit it to the existing module.

I a partial solution by just injecting the SQLSessionManager and opening a new connection based upon a specific DataSource but this is not idea as I have to manage all of the transaction handling myself and I can no longer just simply inject a mapper into a service.

Related documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions