本文介绍了使用Spring MVC和Hibernate 4.2.0.Final的多租户webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述

我使用SpringMVC(3.1.3.RELEASE)和Hibernate 4.2.0.Final开发了一个小webapp。我试图将它转换为成为一个多租户应用程序。



类似的话题已经被其他线程所覆盖,但我找不到解决我的问题的明确方案。



我想要实现的是设计一个web应用程序,它能够:


  1. 在启动时读取数据源配置(一个包含多个数据源定义的XML文件,它位于WAR文件之外,并且不是应用程序上下文或hibernate配置文件)


  2. 为每一个数据源创建一个会话工厂(考虑到每个数据源都是具有不同模式的数据库)。

  3. 如何设置会话工厂范围作为会议? (或者我可以重复使用相同的会话工厂吗?)。


    示例:

     客户端URL  -  URL:http://project.com/a/login.html 
    客户端b的URL - URL:http:// project.com/b/login.html

    如果客户端a发出请求,请阅读数据源配置文件并使用该XML文件为客户端a创建会话工厂。

    如果客户端b将发送请求,这个过程将重复。 / p>

    我在看什么,如何在客户订阅的情况下实现数据源创建,而无需编辑Spring配置文件。它需要自动化。



    这是我的代码,我已经完成了。



    请任何人告诉我,我需要做些什么修改?



    请给出一些示例代码的答案..我在春季和冬眠的世界中是相当新的。

    Spring.xml

     < bean id =dataSource class =org.apache.commons.dbcp.BasicDataSource
    destroy-method =closep: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 =dataSourceref =dataSource/>
    < property name =configLocation>
    < value> classpath:hibernate.cfg.xml< / value>
    < / property>

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

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





    JDBC.properties文件 p>

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



    hibernate.cfg.xml文件


     < ?xml version =1.0encoding =UTF-8?> 
    <!DOCTYPE hibernate-configuration PUBLIC
    - // Hibernate / Hibernate配置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>

    谢谢,

    解决方案

    首先使用tenant_id为Tenant创建一个表,并将其与所有用户相关联。现在,您可以在用户登录并设置会话期间获取此详细信息。


    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,

    解决方案

    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.

    这篇关于使用Spring MVC和Hibernate 4.2.0.Final的多租户webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:58