Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 4 MultiTenancy Spring 3 Schema Export Error

I'm trying to configure a Hibernate SessionFactory using multi-tenancy options and with automatic schema creation, however i'm stuck on this error:

java.lang.NullPointerException
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
    at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:498)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1780)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)

Here is my spring configuration:

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource1"/>
    <property name="hibernateProperties">
        <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
         <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
         <prop key="hibernate.show_sql">true</prop>
         <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>

         <prop key="hibernate.multiTenancy">DATABASE</prop>
         <prop key="hibernate.multi_tenant_connection_provider">org.springframework.webflow.samples.booking.SampleMultiTenantConnectionProvider</prop>
         <prop key="hibernate.tenant_identifier_resolver">org.springframework.webflow.samples.booking.SampleCurrentTenantIdentifierResolver</prop>
      </props>
    </property>
</bean>


<!-- Deploys a in-memory "booking" datasource populated -->
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:booking1" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:booking2" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

For now i'm using only DataSource 1, and the SampleConnectionProvider and TenantResolver always return the same tenant and datasource. However when Schema Export runs, it throws NullPointerException. The supplied ConnectionProvider to the SuppliedConnectionProviderHelper is null. It appears that he can't choose an appropriate ConnectionProvider when using multitenancy. The SuppliedConnectionProviderHelper is created on SchemaExport:

this.connectionHelper = new SuppliedConnectionProviderConnectionHelper(
            serviceRegistry.getService( ConnectionProvider.class )
    );

Can you use the hibernate.hbm2ddl.auto when using multitenancy? Already search in hibernate documentation but did not find anything.

Any help would be much appreciated!

Best regards

João Simas

like image 286
João Simas Avatar asked Mar 26 '13 12:03

João Simas


1 Answers

It looks like there is no support for multi-tenancy in SchemaExport. I looked at source code in other places connection provider is obtained based on multi-tenancy strategy. See line 581 here. I don't see that happen in SchemaExport. Also if there are multiple data sources, SchemaExport should automatically create schema in other data sources as well. I don't see SchemaExport do anything like that.

See and JIRA issue . The JIRA issue does not have exact problem you described but there is work around suggested for creating schema in comments.

This is just my static analysis. You should probably open issue or wait here for someone to post answer. As far as I know, this seems to be bug or unsupported feature.

like image 139
Adisesha Avatar answered Oct 10 '22 14:10

Adisesha