1

大家好,我正在编写一个OSGi负责对用户进行身份验证的捆绑软件。出于设计原因,我有义务对多个数据库模式执行查询(这些模式可以动态创建或删除)。我MySQL作为存储引擎运行。

不知何故,我需要能够为这些模式创建按需实体管理器,但我的尝试并没有成功。这是我尝试过的让我更接近我需要的方法:

使用 JTA 数据源创建持久性单元 (Eclipselink),实际上可以建立到默认模式的数据库连接。但是,当我尝试覆盖任何属性时,例如javax.persistence.jdbc.url. 但是,它始终指向默认模式。

我相信我没有正确覆盖该属性,或者无法将 JTA 数据源属性从 修改EntityManagerEntityManager。以下是我创建 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();  

最后,他们都继续指向默认模式,所以我的问题是:

  1. 这是动态 EntityManager 处理的有效方法吗?如果是这样,我怎样才能有效地覆盖模式属性?
  2. 除了压倒一切还有其他选择吗?

我提前感谢您提供的任何指导。

4

2 回答 2

2

如果您想在 OSGi 中使用 EclipseLink,您必须使用包装 EclipseLink 的Gemini JPA项目,并为您的 PU 包创建和注册 EntityManagerFactory 和 EntityManagerFactoryBuilder 服务。如果想在 PU 之间共享 jdbc 连接,可以使用Gemini DBAccess提供的 JDBC 服务

于 2013-06-17T18:37:01.300 回答
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 服务(带有属性)来检查所有持久性单元是否存在。

于 2013-06-17T14:56:40.187 回答