Multi-tenant webapp using Spring MVC and Hibernate

2020-07-16 06:52发布

问题:

I have developed a small webapp using and SpringMVC(3.1.3.RELEASE) and Hibernate 4.2.0.Final.

I'm trying to convert it to be a multi-tenant application.

Similar topics have been covered in other threads, but I couldn't find a definitive solution to my problem.

What I am trying to achieve is to design a web app which is able to:

  1. Read a datasource configuration at startup (an XML file containing multiple datasource definitions, which is placed outside the WAR file and it's not the application-context or hibernate configuration file)

  2. Create a session factory for each one of them (considering that each datasource is a database with a different schema).

  3. How can i set my session factory scope as session? ( OR Can i reuse the same session factory ?) .

Example:

 Url for client a - URL: http://project.com/a/login.html
 Url for client b - URL: http://project.com/b/login.html

If client "a" make request,read the datasource configuration file and Create a session factory using that XML file for the client "a".

This same process will be repeating if the client "b" will send a request.

What I am looking, how to implement datasource creation upon customer subscription without editing the Spring configuration file. It needs to be automated.

Here is my code ,that i have done so far.

Please anyone tell me,What modifications i need to be made?

Please give an answer with some example code..I am quite new in spring and hibernate world.

Spring.xml

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"

        p:url="${jdbc.databaseurl}" 
p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

<bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />

JDBC.properties File

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost:3306/Logistics
jdbc.username=root
jdbc.password=rot@pspl#12

hibernate.cfg.xml File

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
    <mapping class="pepper.logis.organizations.model.Organizaions" />
    <mapping class="pepper.logis.assets.model.Assets" />

</session-factory>
</hibernate-configuration>

Thanks,

回答1:

First create a table for Tenant with tenant_id and associate it with all users.Now, you can fetch this details while the user logs in and set it in session.



回答2:

We are using AbstractRoutingDataSource to switch DataSource for every request on Spring Boot. I think it is Hot Swapable targets/datasource mentioned by @bhantol above.

It solves our problems but I don't think it is sound solution. I guess JNDI could be a better one than AbstractRoutingDataSource.



回答3:

Wondering what you ended up with.

Here are some ideas for you.

Option 1) Single Application Instance.

It is somewhat ambitious to to this using what you are actually trying to achieve.
The gist is to simply deploy the same exact application with different context root on the same JVM. You can still tune the JVM as a whole like you would have if you had a truely multi-tenant application. But this comes at the expense of duplication of classes, contexts, local caching, start up times etc.

But as of today the Spring Framework 4.0 does not provide much of an multi-tenancy support (other than Hot Swapable targets/datasource) etc. I am looking for a good framework but it may be a wash to move away from Spring at this time for me.

Option 2) Multiple deployments of same application (more practical as of today)

Just have your same exact application deploy to the same application server JVM instance or even different.

If you use the same instance you may now need to bootstrap your app to pickup a DataSource based on what the instance should serve e.g. client=a property would be enough to pickup a **a**DataSource" or **b**DataSource I myself ended up going this approach.

If you have a different application server instance you could just configure a different JNDI path and treat things generically. No need for client="a" property because you have liberty to define your datasource differently with the same name.