In Saas-based environment, Hibernate session management isolates multiple tenants

Source: Internet
Author: User

----------------------------- Tenantmasterfactory. Java ---------------------------

Package com.nen.cn. util;

Import java. util. hashmap;
Import java. util. iterator;
Import java. util. Map;
Import java. util. properties;

Import org. hibernate .*;
Import org. hibernate. cfg. configuration;

@ Suppresswarnings ("static-access ")
Public class tenantmasterfactory {
 
Private Static Map <string, sessionfactory> sessionmapdfactory = new hashmap <string, sessionfactory> ();
Private Static Map <string, session> sessionmap = new hashmap <string, session> ();
Private Static configuration CFG = NULL;
Private Static Properties PPS = NULL;
 
Tenantmasterfactory (){
}
 
/**
* New sessionfactorys for the tenants
*/
Static {
PPS = new getproperties (). getproperties ("src/tenantmanagerfile. properties ");
Try {
For (iterator iter = PPS. keyset (). iterator (); ITER. hasnext ();){
String element = (string) ITER. Next ();
Sessionfactory = configuartion (PPS. getproperty (string. valueof (element). Trim ());
Sessionmapdfactory. Put (string. valueof (element), sessionfactory );
}
} Catch (exception e ){
System. Err. println ("% error creating sessionfactory % ");
E. printstacktrace ();
}
}
 
Private Static sessionfactory configuartion (string Catalog ){
CFG = new configuration ();
Cfg. Configure ("/" + catalog + "hibernate. cfg. xml ");
Return cfg. buildsessionfactory ();
}
 
Public session getsession (string Catalog ){
Session session = sessionmap. Get (Catalog );
If (session = NULL |! Session. isopen ()){
If (sessionmapdfactory. Get (Catalog) = NULL | sessionmapdfactory. Get (Catalog). isclosed ()){
Sessionfactory = configuartion (Catalog );
Sessionmapdfactory. Put (catalog, sessionfactory );
}
Session = sessionmapdfactory. Get (Catalog). opensession ();
Sessionmap. Put (catalog, session );
}
System. Out. println (sessionmapdfactory. Get (Catalog ));
Return session;
}
}

------------------------------ Getproperties. Java ------------------------

Package com.nen.cn. util;
Import java. Io. fileinputstream;
Import java. Io. inputstream;
Import java. util. properties;

Public class getproperties {

Private Static Properties PPS = NULL;
 
Public static properties getproperties (string filename ){
PPS = new properties ();
Try {
Fileinputstream Ss = new fileinputstream (filename );
If (SS! = NULL ){
PPS. Load (inputstream) SS );
}
SS. Close ();
} Catch (exception e ){
System. Out. println ("the file loader error ....");
}
Return PPS;
}

}

--------------------------- Tenantmanagerfile. properties )---------------------------

1 = wis1
2 = wis2
3 = wis3
4 = wis4
5 = wis5
6 = wis6
7 = wis7
8 = wis8
9 = wis9
10 = wis10

Bytes --------------------------------------------------------------------------------------------------

Session session = Dao. getsession (Catalog );

Catalog = tenantmanagerfile. Properties

Tenantmanagerfile. properties post item = database name

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.