我想使用DS(声明性服务)在Karaf上使用ARIES JPA,但我需要对Em。
的创建进行更多的控制我想抓住EntityManagerFacroty或Builder的实例,以便我可以即时创建自己的EM。
原因是我拥有一个工厂服务,该服务创建不同的em取决于客户端需要连接的数据源,因此我无法用服务中的连接名称进行硬编码。
类似:
@Reference(target = "(osgi.unit.name=my.factory)")
protected EntityManagerFactoryBuilder emfb;
我可以用它来生产工厂和按需工厂。
还将Eclipse链接设置为Karaf上的提供商:*德比* mysql
谢谢。
取得了一些进展,如果捆绑包包含具有此单元名称的持久性单元,则白羊座将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 PAX-JDBC-CONFIG PAX-JDBC-MYSQL PAX-JDBC-MYSQL PAX-JDBC-JDBC-derby安装了KARAF功能也有JPA,Eclipselink
<feature>jpa</feature>
<feature>eclipselink</feature>
...
好,找到了问题!
是配置属性。
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