大家好,我正在编写一个负责验证用户身份的OSGi
软件包。出于设计原因,我有义务对多个数据库模式执行查询(可以动态创建或删除这些模式)。我正在运行MySQL
作为存储引擎。
不知何故,我需要能够为这些模式创建按需实体管理器,但我的尝试并没有成功。以下是我试过的让我更接近我需要的东西:
使用JTA数据源创建持久性单元(Eclipselink),该数据源实际上可以建立与默认架构的数据库连接。但是,当我尝试覆盖任何属性时,例如 javax.persistence.jdbc.url
。但是,它始终指向默认模式。
我认为我没有正确覆盖该属性,或者无法从EntityManager
到EntityManager
修改JTA数据源属性。以下是我创建EntityManagers的方法:
Map<String, String> dbProps = new HashMap<String, String>();
dbProps.put("javax.persistence.jdbc.url","jdbc:mysql://mydomain:3306/mydynamicdb);
EntityManagerFactory fact = Persistence.createEntityManagerFactory("myPersistenceUnit", dbProps);
EntityManager myEM = fact.createEntityManager();
最后,他们都继续选择默认架构,所以我的问题是:
我提前感谢您提供的任何指导。
答案 0 :(得分:2)
如果要在OSGi中使用EclipseLink,则必须使用包装EclipseLink的Gemini JPA项目,并为PU包创建并注册EntityManagerFactory和EntityManagerFactoryBuilder服务。 如果想要共享PU之间的jdbc连接,可以使用Gemini DBAccess
提供的JDBC服务答案 1 :(得分:1)
您应该能够获取EntityManagerFactory
作为OSGi服务,您可以(LDAP)使用服务属性osgi.unit.name
过滤正确的服务,如下所示:
ServiceReference[] refs = null;
String filter = "(osgi.unit.name=myPersistenceUnit)";
ServiceReference[] refs = ctx.getServiceReferences(EntityManagerFactory.class.getName(), filter);
//Should only be one reference, check (throw exception etc)
return (EntityManagerFactory)ctx.getService(refs[0]);
您可以通过在OSGi shell中列出EntityManagerFactory服务(带属性)来检查是否存在所有持久性单元。