Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hibernate 4 Multi Tenancy Feature Support [SPR-10823] #15449

Closed
spring-projects-issues opened this issue Aug 9, 2013 · 4 comments
Closed

Hibernate 4 Multi Tenancy Feature Support [SPR-10823] #15449

spring-projects-issues opened this issue Aug 9, 2013 · 4 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Aug 9, 2013

I. Benyaminov opened SPR-10823 and commented

At the moment there is no way to provide bean reference for MultiTenantConnectionProvider. This is necessary because an implementation of MultiTentnatConnectionProvider needs to be wired to a DataSource. The current version of LocalSessionFactoryBuilder sets only the dataSource property to a bean reference. Since currently any implementation of MultiTenantConnectionProvider is not Spring managed, users are forced to define another DataSource/Connection Pool with hard coded db properties or a non Spring way of accessing those properties


Affects: 3.2.4

Reference URL: http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch16.html#d5e4605

Issue Links:

Referenced from: commits 542b5b2

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Sep 3, 2013

Juergen Hoeller commented

Actually, just like "hibernate.tenant_identifier_resolver", the "hibernate.multi_tenant_connection_provider" property not only accepts a class name but also an instance. So you could pass in a MultiTenantConnectionProvider instance through a <map> specified against LocalSessionFactoryBean's "hibernateProperties", or call LocalSessionFactoryBuilder's getProperties().put(...) method accordingly.

That said, to make this first-class in Spring 4, I've added setMultiTenantConnectionProvider and setCurrentTenantIdentifierResolver methods to both LocalSessionFactoryBuilder and LocalSessionFactoryBean.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Sep 3, 2013

I. Benyaminov commented

I was not aware of the method you have mentioned. Thank you! I have actually done the same thing you have described as a temporary fix. Extended LocalSessionFactoryBuilder and wrote duplicate LocalSessionFactoryBean with a tiny change to use the custom LocalSessionFactoryBuilder.

My answer on stackoverflow: http://stackoverflow.com/questions/16213573/setting-up-a-multitenantconnectionprovider-using-hibernate-4-2-and-spring-3-1-1/18392586#18392586

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 25, 2013

andrew chen commented

Juergen Hoeller's suggestion seem to work for me:

<property name="hibernateProperties">
    <map>
        <!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
        <entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
        <entry key="hibernate.multiTenancy" value="SCHEMA"/>
        <entry key="hibernate.tenant_identifier_resolver" value="com.jazva.multitenant.dao.CurrentTenantIdentifierResolverImpl"/>
        <entry key="hibernate.multi_tenant_connection_provider" value-ref="multiTenantConnectionProvider"/>
    </map>
</property>

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented May 29, 2015

Amit commented

I tried following the suggest from @Juergen Hoeller with JPA but got an NPE (http://pastebin.com/E3RwNt1y). The config is - http://pastebin.com/ZNdkzkKT. It seems that the AbstractEntityManagerFactoryBean.getObject() is called before AbstractEntityManagerFactoryBean.afterPropertiesSet(). Any idea what could be wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants