How do I initialize multiple databases with hbm2dll using LocalContainerEntityManagerFactoryBean?
I am using different datasources per tenant in MultiTenancy. I have a default datasource with id "dataSource" in ApplicationContext.xml and I generate a datasource per tenant using the properties of "dataSource" but changing the url. I add them in a custom MapDataSourceLookup.
I have tried to configure JPA initialization using LocalContainerEntityManagerFactoryBean.
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory" >
<property name="persistenceUnitManager" ref="pum"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${database.dialect}" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.multi_tenant_connection_provider" value-ref="multitenancyConnectionProvider"/>
<entry key="hibernate.tenant_identifier_resolver" value-ref="tenantResolver"/>
</map>
</property>
</bean>
<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="dataSourceLookup" ref="dataSourceLookup"/>
<property name="persistenceXmlLocations">
<list>
<value>classpath:META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"
id="dataSource">
<property name="driverClass" value="${database.driverClassName}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="idleConnectionTestPeriod" value="${database.idleConnectionTestPeriod}" />
<property name="idleMaxAge" value="${database.idleMaxAge}" />
<property name="maxConnectionsPerPartition" value="${database.maxConnectionsPerPartition}" />
<property name="minConnectionsPerPartition" value="${database.minConnectionsPerPartition}" />
<property name="partitionCount" value="${database.partitionCount}" />
<property name="acquireIncrement" value="${database.acquireIncrement}" />
<property name="statementsCacheSize" value="${database.statementsCacheSize}" />
<property name="releaseHelperThreads" value="${database.releaseHelperThreads}" />
</bean>
I have a custom @component dataSourceLookup that is injected, and it's creating the different datasources in this way.
//Add new datasource with configuration from "dataSource" and new url per tenant
BoneCPDataSource customDataSource = new BoneCPDataSource(defaultDataSource.getConfig());
customDataSource.setJdbcUrl(props.getProperty("database.url"));
addDataSource(tenantId, customDataSource);
logger.info("Configuring tenant: " + tenantId + " Properties: " + customDataSource.toString());
This is my persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<!-- Multi-tenancy properties -->
<property name="hibernate.multiTenancy" value="DATABASE" />
</properties>
</persistence-unit>
Right now the only scheme that is being created is the default database. But the datasources which point to other databases are not being created. That produce this error when I try to access to an user since USER table doesn't exist:
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: USER