Hi, I think jta-data-source needs an XADataSource and not a simple javax.sql.DataSource (I am not sure in that). Another problem is if the bundle that contains the DataSource service starts later than the aries jpa bundle and the resolvation of your persistence bundle you hibernate will not be able to start. The reason is the following:
- createEntityManagerFactory is called - If schema export is set to update or create or the Dialect is not defined Hibernate tries to connect to the database during the entity manager creation - As there is no datasource at that time only a DelayedLookupDataSource provided by Aries Hibernate will not be able to connect to the database and the entity manager will not be created. It is not Karaf and it does not use JNDI but I created a sample project that: - Uses Aries, Hibernate, JPA and H2 database - Runs a simple test during the integration-test phase of maven You can check it out from https://guest:[email protected]/svn/everit-osgi/trunk/testing/samples/hibernate/ If you are interested about the dependencies go to the core module and run the following command: "mvn package org.everit.osgi.testing:everit-osgi-plugin:linkFolder" This will create a folder bundleDependencies in the target and you can find a shortcut for all of the dependencies that were used during the test. You must run this command with Java 1.7 behind as it uses the NIO.2 API that was introduced in 1.7. Also in windows you need to run the command line as an administrator (due to some reason in Windows shortcut creation can be done only by admins). I think the sample is pretty self explaining but please let me know if not and I will extend it. You can find a tutorial that explains the testing mechanism at http://everit.org/osgi/testing/step-by-step-guide.html I hope I could help a bit. Balazs Zsoldos Software Architect Mobile: +36-70/594-92-34 Everit Kft. https://www.everit.biz On Sat, Apr 28, 2012 at 1:20 AM, Diane Andrus <[email protected]> wrote: > Hi, > I'm trying to get a JTA transaction working in Karaf on a very simple > model. I was saving & retrieving the model via my hibernate mapping > at some point, but now I am not. > I started with this tutorial (which seems very good in getting the > concept of jpa & osgi) > http://jaxenter.com/tutorial-using-jpa-in-an-osgi-environment-36661.html, > but, it doesn't use a DataSource and JTA transactions, and make Aries > manage my transactions. I didn't think it would be so hard. > > Most of this is pretty verbatim from the Aries JPA home page. > > There's this cryptic remark, that I'm not quite sure what it means.... > "To access to the datasource, you must provide within the or > depending if you use a JTA transaction manager or not. > > <persistence-unit name="ReportIncident" transaction-type="JTA"> > > <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> > > <jta-data-source>osgi:service/javax.sql.DataSource/( > osgi.jndi.service.name=jdbc/reportincidentdb)</jta-data-source>" > > Is there a JTA transaction manager bundle that I have to deploy as > well? I'm using karaf, and I just installed the jpa, jndi, & > transaction features. I have some commons bundles & the derby driver > and I believe all of the dependencies that I need running....but I am > missing some secret sauce here. I'm using hibernate as my JPA provider > (which is probably the problem....some missing library or crazy > classloading issue) > > My current error: > 2012-04-27 18:30:10,518 | INFO | FelixStartLevel | > ConnectionProviderFactory | ection.ConnectionProviderFactory > 173 | 65 - hibernate-wrapper - 1.0.0.SNAPSHOT | Initializing > connection provider: > org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider > 2012-04-27 18:30:10,522 | INFO | FelixStartLevel | > ctedDataSourceConnectionProvider | ctedDataSourceConnectionProvider > 62 | 65 - hibernate-wrapper - 1.0.0.SNAPSHOT | Using provided > datasource > > 2012-04-27 18:30:10,534 | DEBUG | lixDispatchQueue | container > | ? ? | 53 - > org.apache.aries.jpa.container - 0.3.0 | FrameworkEvent ERROR > java.lang.IllegalStateException: The bundle > com.mycompany.PersistenceModel_1.0.0.SNAPSHOT is not started. > at > org.apache.aries.jpa.container.unit.impl.DelayedLookupDataSource.getDs(DelayedLookupDataSource.java:50)[53:org.apache.aries.jpa.container:0.3.0] > at > org.apache.aries.jpa.container.unit.impl.DelayedLookupDataSource.getConnection(DelayedLookupDataSource.java:74)[53:org.apache.aries.jpa.container:0.3.0] > at > org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:71)[65:gov.navair.dep.hibernate-wrapper:1.0.0.SNAPSHOT] > at > org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:113)[65:gov.navair.dep.hibernate-wrapper:1.0.0.SNAPSHOT] > at > org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2836)[65:gov.navair.dep.hibernate-wrapper:1.0.0.SNAPSHOT] > at > org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2832)[65:gov.navair.dep.hibernate-wrapper:1.0.0.SNAPSHOT] > at > org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)[65:gov.navair.dep.hibernate-wrapper:1.0.0.SNAPSHOT] > at > org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)[65:gov.navair.dep.hibernate-wrapper:1.0.0.SNAPSHOT] > at > org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)[65:gov.navair.dep.hibernate-wrapper:1.0.0.SNAPSHOT] > at > org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.createEntityManagerFactories(EntityManagerFactoryManager.java:263)[53:org.apache.aries.jpa.container:0.3.0] > at > org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.bundleStateChange(EntityManagerFactoryManager.java:153)[53:org.apache.aries.jpa.container:0.3.0] > at > org.apache.aries.jpa.container.impl.PersistenceBundleManager.modifiedBundle(PersistenceBundleManager.java:283)[53:org.apache.aries.jpa.container:0.3.0] > at > org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:453)[karaf.jar:2.2.5] > at > org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:237)[karaf.jar:2.2.5] > at > org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:413)[karaf.jar:2.2.5] > at > org.apache.felix.framework.util.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:807)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:729)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:610)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:3761)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.Felix.access$600(Felix.java:80)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.Felix$FelixResolver.fireResolvedEvents(Felix.java:4299)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.Felix$FelixResolver.resolve(Felix.java:4065)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.Felix.resolveBundle(Felix.java:3439)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.Felix.startBundle(Felix.java:1734)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1163)[org.apache.felix.framework-3.0.9.jar:] > at > org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:264)[org.apache.felix.framework-3.0.9.jar:] > > > > And it's crazy weird, because then my model bundle status is ACTIVE, > even though it failed to really start. > So, of course, my service won't start, b/c model bundle never finished > it's JPA stuff and it never got Aries to export an Entity Manager > Factory for it.... > I've actually lost track of everything I've tried. I started out with > wanting to have a clean model bundle, not dependent on jaxb or jpa > stuff, but, that didn't seem to work. So, I'm at the point where I > just want a JTA transaction working with Aries taking care of > transactions for me. That would be my goal for now. I hope its some > simple thing I missed. Any help would be appreciated...Please > > Thanks, > > Diane > > ========== > Bundle #1: Datasource > I pretty much copied by datasource config from the main page of Aries JPA. > <!-- DataSource Derby --> > <bean id="dataSourceDerby" > class="org.apache.commons.dbcp.BasicDataSource" > destroy-method="close"> > <property name="driverClassName" > value="org.apache.derby.jdbc.EmbeddedDriver" /> > <property name="url" value="jdbc:derby:memory:pu_apps;create=true" /> > <property name="username" value="" /> > <property name="password" value="" /> > </bean> > > <!-- Expose DataSource as JNDI reference --> > <service ref="dataSourceDerby" interface="javax.sql.DataSource"> > <service-properties> > <entry key="osgi.jndi.service.name" value="jdbc/derbyds"/> > </service-properties> > </service> > > ========== > Bundle #2: Persistence Model > java class: com/mycompany/persistencemodel/App - simple POJO > public class App { > > private Integer appId; > private String name; > private String status; > private String version; > private String description; > > ....default constructor & generated getter & setter for each property.... > } > > META-INF/persistence.xml > <persistence-unit name="pu_apps" transaction-type="JTA"> > <provider>org.hibernate.ejb.HibernatePersistence</provider> > <jta-data-source>osgi:service/javax.sql.DataSource/( > osgi.jndi.service.name=jdbc/derbyds)</jta-data-source> > <mapping-file>mappings/app.hbm.xml</mapping-file> > > <properties> > <property name="javax.persistence.jdbc.driver" > value="org.apache.derby.jdbc.EmbeddedDriver"/> > <!-- for hibernate --> > <property name="hibernate.dialect" > value="org.hibernate.dialect.DerbyDialect"/> > <property name="hibernate.hbm2ddl.auto" value="create"/> > <property name="hibernate.show_sql" value="true"/> > > </properties> > </persistence-unit> > (and yes...<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence> > in my pom.xml) > > mappings/app.hbm.xml > <hibernate-mapping> > <class name="com.mycompany.persistencemodel.App" table="APP"> > <id column="APP_ID" name="appId"> > <generator class="identity"> > </generator> > </id> > <property column="NAME" name="name"/> > <property column="STATUS" name="status"/> > <property column="VERSION" name="version"/> > <property column="DESCRIPTION" name="description"/> > </class> > </hibernate-mapping> > > ========== > Bundle #3 > hibernate wrapper from > http://jaxenter.com/tutorial-using-jpa-in-an-osgi-environment-36661.html > > ========== > Bundle #4 > Persistence Service > <bean id="defaultPersistenceService" > class="com.mycompany.service.impl.DefaultPersistenceService"> > <tx:transaction method="*" value="Required" /> > <property name="bc" ref="blueprintBundleContext"/> > <jpa:context property="entityManager" unitname="pu_apps" /> > </bean> > > <service > id="persistenceService" > interface="com.mycompany.service.PersistenceService" > ref="defaultPersistenceService"> > </service> >
