----------------------------- 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