> Date: Fri, 15 Oct 2010 14:50:17 +0200
> Subject: Struts, Spring, Hibernate and multiple Sessionfactories
> From: eduard.neuw...@googlemail.com
> To: user@struts.apache.org
>
> Hello,
>
> I am looking for an elegant solution for the following situation.
>
> I have a multitenant struts/spring/hibernate application. Every tenant is
> stored in own database within the same RDBMS. All databases has the same
> schema.
MG>a database schema represents the entire structure (tables and relationships
of those tables) of the Database
MG>there is no need for multiple databases when your database schema contains
the persisted entities (tables)
At the spring layer every tenant is represented as a datasource.
MG>perhaps there is some confusion as to the meaning of the term datasource ?
MG>In hibernate a datasource is the URL string which provides connection
attributes to your database e.g.
MG>Configuration.setProperty("hibernate.connection.datasource",
"java:comp/env/jdbc/test")
> Because I use the Hibernate I have to create multiple SessionFactories to
MG> Hibernate SessionFactory Documentation
MG>Usually an application will create
* a single <tt>Configuration</tt>,
* build a single instance of<tt>SessionFactory</tt>
* and then instantiate <tt>Session</tt>s in threads servicing client requests.
* The <tt>Configuration</tt> is meant
* only as an initialization-time object.
* <tt>SessionFactory</tt>s are
* immutable and do not retain any association back to the
* <tt>Configuration</tt>.<br>
* <br>
* A new <tt>Configuration</tt> will use the properties specified in
* <tt>hibernate.properties</tt> by default.
MG> Configuration->SessionFactory->1..n->Session
MG>using Hibernate as your implementing template and assuming you have a valid
java.sql.Connection all you have to do is create
MG>an EmptyInterceptor object constructed from the parameters of the available
UserSession
MG>then call SessionFactory.openSession to obtain the session
/**
* Open a {...@link Session}, utilizing the specfied JDBC {...@link
Connection} and
* specified {...@link Interceptor}.
* <p>
* Note that the second-level cache will be disabled if you supply a JDBC
* connection. Hibernate will not be able to track any statements you might
* have executed in the same transaction. Consider implementing your own
* {...@link org.hibernate.connection.ConnectionProvider} instead as a
highly
* recommended alternative.
*
* @param connection a connection provided by the application.
* @param interceptor a session-scoped interceptor
*
* @return The created session.
*/
public org.hibernate.classic.Session openSession(Connection connection,
Interceptor interceptor);
OR in the case of Spring
public static Session getSession(SessionFactory sessionFactory, boolean
allowCreate)
>
> How can I provide the correct Seesionfactory to the UserSession in struts.
> Once I have is the tenantId stored in UserSession itself.
>
> Please help
> Eduard