如何将Aries DS JPA与实体管理器工厂构建器一起使用

时间:2016-06-17 11:06:50

标签: jpa osgi karaf aries

我想在使用DS(声明服务)的Karaf上使用Aries JPA,但我需要更多地控制EM的创建。

我想获取EntityManagerFacroty或Builder的实例,以便我可以动态创建自己的EM。

原因是我有一个工厂服务,创建不同的EM取决于客户端需要连接到哪个DataSource,所以我不能硬编码我的服务中的连接名称。

类似的东西:

@Reference(target = "(osgi.unit.name=my.factory)")
    protected EntityManagerFactoryBuilder emfb;

我可以用它来按需制造工厂和EM。

还有关于设置作为Karaf提供商的eclipse链接的任何提示: *德比 * MySql

感谢。

取得了一些进展,如果捆绑包含一个具有此单位名称的持久性单元,那么aries会将EMFB注入到您的组件中。

我现在正尝试使用eclipse链接属性从此组件建立与Derby / Mysql的新连接,但是错误地导致驱动程序错误。

eclipselink.jdbc.write-connections.max=10
javax.persistence.jdbc.url=jdbc\:mysql\://192.168.99.101:3306/database
eclipselink.jdbc.write-connections.min=1
eclipselink.ddl-generation=create-or-extend-tables
javax.persistence.jdbc.user=*****
javax.persistence.jdbc.password=******
eclipselink.jdbc.read-connections.max=10
eclipselink.logging.level=INFO
eclipselink.jdbc.read-connections.min=1

我正在使用这些配置属性来创建一个新的EntityManagerFactory并测试我可以像这样创建一个EntityManager:

try {
    emf = emfb.createEntityManagerFactory(configuration);
    if (emf.isOpen()) {
       // test creating a new EM
        EntityManager em = emf.createEntityManager();
        LOGGER.info("EntityManager: [{}]", em);
        em.close();
    } else {
        LOGGER.error("EMF is closed");
    }
} catch (Exception ex) {
    LOGGER.error("Exception", ex);
}

我收到的错误是mysql或derby(我尝试使用derby属性)没有定义。

2016-06-23 20:17:48,643 | ERROR | nsole user karaf | FactorySessionProviderImpl       | 374 - factory - 4.1.1 | activate | Exception
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.99.101:3306/database?autoReconnect=true&autoReconnectForPools=true
Error Code: 0
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:815)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:305)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)
        at com.factory.FactorySessionProviderImpl.activate(FactorySessionProviderImpl.java:90)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_91]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_91]

我为pax-jdbc安装了karaf功能pax-jdbc-config pax-jdbc-mysql pax-jdbc-derby 也有jpa,eclipselink

    <feature>jpa</feature>
    <feature>eclipselink</feature>
   ...

1 个答案:

答案 0 :(得分:0)

好的,发现了这个问题!

这是配置属性。

javax.persistence.jdbc.driver=com.mysql.jdbc.Driver
eclipselink.target-database=MySql
javax.persistence.jdbc.url=jdbc\:mysql\://<address:ip>/datasource?autoReconnect=true&autoReconnectForPools=true
javax.persistence.jdbc.user=***
javax.persistence.jdbc.password=*** 
eclipselink.jdbc.write-connections.max=16
eclipselink.jdbc.write-connections.min=4
eclipselink.jdbc.read-connections.max=64
eclipselink.jdbc.read-connections.min=16
eclipselink.ddl-generation=create-or-extend-tables
eclipselink.logging.level=INFO