Bonjour.

j'ai une datasource définie dans OSGI par un bundle.
Pour y accèder dans un autre bundle osgi je fait un lookup avec le nom "osgi:services/javax.sql.DataSource/(name=myds)"
et j'obtiens bien un objet implémentant l'interface me permettant d'établir une connexion sur ma base et de m'en servir.

j'en conclus que ma datasource est correctement configuré.

dans un bundle je veux accèder à cette datasource via openJPA
et là impossible de me connecter.

j'ai fait un est junit hors osgi en définissant ma datasoure avec le nom "myds" tout ce passe parfaitement bien.
mais sous osgi je ne peux pas nommer ma datasource "myds". J'ai donc toujours dans y unit changé le nom de ma datasource par "osgi:services/javax.sql.DataSource/(name=myds)" et j'ai mis la même chose dans ma persistance unit et là ça ne marche plus.
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
this.ic = new InitialContext();
...
this.ds.setCreateDatabase("create");
ic.bind("myds", this.ds);
this.emf = Persistence.createEntityManagerFactory("unitname", System.getProperties());
this.em = this.emf.createEntityManager();
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
	<persistence-unit name="unitname" transaction-type="RESOURCE_LOCAL">
		<non-jta-data-source>myds</non-jta-data-source>

Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
this.ic = new InitialContext();
...
this.ds.setCreateDatabase("create");
ic.bind("osgi:services/javax.sql.DataSource/(name=myds)", this.ds);
this.emf = Persistence.createEntityManagerFactory("unitname", System.getProperties());
this.em = this.emf.createEntityManager();
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
	<persistence-unit name="unitname" transaction-type="RESOURCE_LOCAL">
		<non-jta-data-source>osgi:services/javax.sql.DataSource/(name=myds)</non-jta-data-source>
sous junit j'obtient l'erreur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
Could not open JPA EntityManager for transaction; nested exception is <openjpa-2.1.1-r422266:1148538 nonfatal general error> 
org.apache.openjpa.persistence.PersistenceException: There were errors initializing your configuration: 
<openjpa-2.1.1-r422266:1148538 fatal user error> 
org.apache.openjpa.util.UserException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.
et sous osgi l'erreur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 <openjpa-0.0.0-rnull fatal user error> org.apache.openjpa.persistence.ArgumentException: 
Could not invoke the static newInstance method on the named factory class "<<openjpa-0.0.0-rnull fatal user error> 
org.apache.openjpa.util.UserException: The named BrokerFactory "org.apache.openjpa.jdbc.kernel.JDBCBrokerFactory" is not valid.>".
pour résoudre le pb sous junit j'ai compllété mon fichier openjpa.xml
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 
	<persistence-unit name="">
		<properties>
			<property name="openjpa.Log" value="log4j" />
 
			<property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
			<property name="openjpa.ConnectionURL" value="jdbc:derby:target/mydb" />
			<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
			<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
			<property name="openjpa.jdbc.DBDictionary" value="derby" />
		</properties>
 
	</persistence-unit>
</persistence>
mais du coup il n'utilise plus ma datasource. Or sous OSGI je n'ai pas le choix la base est définie par une datasource dont à priori je ne connais pas la nature vu que ce sera l'utilisateur final qui la choisira

Comment définir le nom de la datasource sous osgi pour l'utiliser avec JPA ?
merci à vous
A+JYT