jpa多数据源处理
项目中需要多个数据库,而且每个数据库的基本信息都是保存在一个公共库(public)的sysarea表中的:表格式如下
AREA_ID
AREA_NAMEEN
AREA_DATABASE
AREA_DBNAME
AREA_DBPWD
AREA_DBSERVE
1
数据库1
infodb1
root
111111
192.168.1.1
2
数据库1
infodb2
root
111111
192.168.1.2
persistence.xml文件如下:
[code="xml"]org.hibernate.ejb.HibernatePersistencejava:comp/env/jdbc/PUBLICDBorg.hibernate.ejb.HibernatePersistence
根据数据库名称取链接属性类:
[code="java"]publicclassAreaFactory{
privatestaticfinalMap>map=newHashMap>();
privatestaticfinalStringSHOWSQL=HttpContext.getConfig().getString(AppConfig.SHOWSQL);
privatestaticAreaFactoryareaFactory=newAreaFactory();
publicstaticAreaFactorygetInstance(){
returnareaFactory;
}
publicMapgetAreaMap(StringunitName){
Mapmp=map.get(unitName);
if(mp==null){
mp=newHashMap();
UnitDAOunitDAO=DAOFactory.getInstance().getUnitDAO();
SysAreasysArea=unitDAO.findArea(unitName);
if(sysArea!=null){
mp.put("hibernate.connection.url","jdbc:mysql://"+sysArea.getAreaDbserve()+":"+sysArea.getAreaPort()+"/"+sysArea.getAreaDatabase());
mp.put("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
mp.put("hibernate.connection.username",""+sysArea.getAreaDbname()+"");
mp.put("hibernate.connection.password",""+sysArea.getAreaDbpwd()+"");
mp.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
mp.put("hibernate.show_sql",SHOWSQL);
mp.put("hibernate.transaction.factory_class","org.hibernate.transaction.JDBCTransactionFactory");
mp.put("hibernate.query.substitutions","true=1,false=0");
map.put(unitName,mp);
}
}
returnmp;
}
}
DAOFactory.java类
[code="java"]publicclassDAOFactory{
privatefinalMapfactory=newHashMap();
privatestaticDAOFactoryinstance;
publicstaticDAOFactorygetInstance(){
if(instance==null){
instance=newDAOFactory();
}
returninstance;
}
publicUnitDAOgetUnitDAO(){
finalStringkey=UnitDAO.class.getSimpleName();
UnitDAOdao=(UnitDAO)factory.get(key);
if(dao==null){
dao=newUnitDAOImpl();
factory.put(key,dao);
}
returndao;
}
}
sysArea类
[code="java"]@Entity
@Table(name="sys_area")
publicclassSysAreaimplementsjava.io.Serializable{
privateintareaId;
privateStringareaNameen;
privateStringareaDatabase;
privateStringareaDbname;
privateStringareaDbpwd;
privateStringareaDbserve;
publicSysArea(){
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="AREA_ID",unique=true,nullable=false)
publicintgetAreaId(){
returnthis.areaId;
}
publicvoidsetAreaId(intareaId){
this.areaId=areaId;
}
@Column(name="AREA_NAMEEN",nullable=false,length=16)
publicStringgetAreaNameen(){
returnthis.areaNameen;
}
publicvoidsetAreaNameen(StringareaNameen){
this.areaNameen=areaNameen;
}
@Column(name="AREA_DATABASE",nullable=false,length=32)
publicStringgetAreaDatabase(){
returnthis.areaDatabase;
}
publicvoidsetAreaDatabase(StringareaDatabase){
this.areaDatabase=areaDatabase;
}
@Column(name="AREA_DBNAME",nullable=false,length=32)
publicStringgetAreaDbname(){
returnthis.areaDbname;
}
publicvoidsetAreaDbname(StringareaDbname){
this.areaDbname=areaDbname;
}
@Column(name="AREA_DBPWD",nullable=false,length=64)
publicStringgetAreaDbpwd(){
returnthis.areaDbpwd;
}
publicvoidsetAreaDbpwd(StringareaDbpwd){
this.areaDbpwd=areaDbpwd;
}
@Column(name="AREA_DBSERVE",nullable=false,length=256)
publicStringgetAreaDbserve(){
returnthis.areaDbserve;
}
publicvoidsetAreaDbserve(StringareaDbserve){
this.areaDbserve=areaDbserve;
}
}
SessionFactory类:
[code="java"]packagecom.og.cms.dao.impl;
importjava.util.HashMap;
importjava.util.Map;
importjavax.persistence.EntityManager;
importjavax.persistence.EntityManagerFactory;
importjavax.persistence.Persistence;
importjavax.persistence.PersistenceException;
importcom.og.cms.AppConfig;
importcom.og.cms.HttpContext;
publicclassSessionFactory{
privatestaticfinalMapmap=newHashMap();
privatestaticfinalMap>factory=newHashMap>();
privatestaticfinalStringPUBLICDB=HttpContext.getConfig().getString(AppConfig.PUBLICDB_SUBDIVISION);
static{
EntityManagerFactoryemf=map.get(PUBLICDB);
if(emf==null){
emf=Persistence.createEntityManagerFactory(PUBLICDB);
map.put(PUBLICDB,emf);
}
}
privateSessionFactory(){
}
publicstaticEntityManagerFactorygetFactory(StringunitName){
EntityManagerFactoryemf=map.get(unitName);
if(emf==null){
Mapmp=AreaFactory.getInstance().getAreaMap(unitName);
emf=Persistence.createEntityManagerFactory("hdbms",mp);
map.put(unitName,emf);
}
returnemf;
}
publicstaticEntityManagerget(finalStringunitName){
ThreadLocallocal=factory.get(unitName);
if(local==null){
local=newThreadLocal(){
protectedsynchronizedEntityManagerinitialValue(){
EntityManagerFactoryemf=getFactory(unitName);
returnemf.createEntityManager();
}
};
factory.put(unitName,local);
}
EntityManagerem=local.get();
if(em==null||!em.isOpen()){
em=getFactory(unitName).createEntityManager();
local.set(em);
}
returnem;
}
publicstaticvoidclose(finalStringunitName)throwsPersistenceException{
ThreadLocallocal=factory.get(unitName);
if(local==null)return;
EntityManagerem=local.get();
local.set(null);
if(em!=null)
em.close();
}
}
基本测试
:
[code="java"]classtestDao{
publicList>findAllInfoSoure(StringunitName)throwsPersistenceException{
List>list=null;
EntityManagerem=SessionFactory.get(unitName);
try{
list=em.createQuery("FROMxxxpoORDERBYpo.xxDESC").getResultList();
}
catch(PersistenceExceptionex){
thrownewPersistenceException();
}
finally{
if(em.isOpen())em.close();
}
returnlist;
}
}
其中:unitName代表是哪个表