This discussion is archived
2 Replies Latest reply: Nov 14, 2012 4:41 PM by 836522 RSS

JPA EntityManager shared across OSGi modules

836522 Newbie
Currently Being Moderated
Hi,

I'd like to create an OSGi module which would act as a 'JPA master' making JPA EntityManager available to other OSGi modules. The persistence.xml would be part of this module but it would do nothing more.

I'd like to then access EntityManager from other OSGi bundles, both JARs and WARs (JAR Bundles and WAR Bundles).

I'm running on GlassFish 3.1.2.2 and using EclipseLink as the JPA provider.

Any idea for best practices?

Thank you
  • 1. Re: JPA EntityManager shared across OSGi modules
    974289 Newbie
    Currently Being Moderated
    Yes, this is possible. Check out section #8.1 (Standalone Persistence Unit) of "OSGi Application Development using GlassFish Server" guide available at:
    http://glassfish.java.net/public/GF-OSGi-Features.pdf

    Sahoo
  • 2. Re: JPA EntityManager shared across OSGi modules
    836522 Newbie
    Currently Being Moderated
    Hi, thanks for the link.

    It says 'Refer to the examples to see it being used.' without a link so I read the section above that (8. JPA in OSGi Application)

    It says about some config entries in GlassFish, I enabled it but still no luck.

    Here's my /src/main/resources/META-INF/persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="cmsdbconnection">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
         <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    <property name="javax.persistence.jdbc.url" value="jdbc:mysql://172.16.145.139:3306/test_db"/>
    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    <property name="javax.persistence.jdbc.user" value="db_user"/>
    <property name="javax.persistence.jdbc.password" value="db_user"/>
    <property name="eclipselink.ddl-generation.table-creation-suffix" value="engine=InnoDB" />
    <property name="eclipselink.ddl-generation" value="create-tables" />
    <property name="eclipselink.target-server" value="=SunAS9" />
    </properties>
    </persistence-unit>

    </persistence>


    Maven pom.xml to generate MANIFEST.MF:

    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>2.4</version>
                        <extensions>true</extensions>
                        <configuration>
                             <archive>
                                  <manifestEntries>
                                       <Bundle-ManifestVersion>2</Bundle-ManifestVersion>
                                       <Bundle-SymbolicName>com.ifp.vault.service.persistence</Bundle-SymbolicName>
                                       <Bundle-Version>${project.version}</Bundle-Version>
                                       <Import-Package>org.osgi.framework,org.osgi.util.tracker,javax.persistence;jpa="2.0";version="1.1.0",org.eclipse.persistence.jpa</Import-Package>
                                       <JPA-PersistenceUnits>cmsdbconnection</JPA-PersistenceUnits>
                                       <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
                                  </manifestEntries>
                             </archive>
                        </configuration>
                   </plugin>


    Deploying in GlassFish succeeds, but I cannot obtain the EntityManagerFactory from other OSGi bundles.

    Should that be registered as an OSGi service anyway? Because I can't see that when listing all OSGi bundles.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points