0
votes

I would like to use Aries JPA on Karaf with DS (declarative service) but I need more control over the creation of EM.

I would like to grab instance of the EntityManagerFacroty or Builder so I can create my own EM on the fly.

The reason is I have a factory service that creates different EM depend on which DataSource the client needs to connect to so I cannot hard code the name of the connection in my service.

something like:

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

and I can use it to make factories and EM on demand.

also any tips for setup of eclipse link as a provider on Karaf: * Derby * MySql

thanks.

made some progress, if a bundle include a persistence unit with this unit name then aries will inject the EMFB to your component.

I am now trying to make a new connection to Derby/Mysql from this component using eclipse link properties but getting driver missing error.

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

I am using these config properties to create a new EntityManagerFactory and test that I can create an EntityManager like so:

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);
}

I am getting an error that either mysql or derby (I try with derby properties) are not defined.

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]

I installed the karaf features for pax-jdbc pax-jdbc-config pax-jdbc-mysql pax-jdbc-derby also have jpa, eclipselink

    <feature>jpa</feature>
    <feature>eclipselink</feature>
   ...
1
There is active work going on in OSGi enRoute to provide a good example in this area that is aligned with the upcoming specification. See github.com/osgi/osgi.enroute.examples.jdbcPeter Kriens

1 Answers

0
votes

ok, found the issue!

it was the config properties.

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