20.8. DataSource
Typical Enterprise OSGi JPA usage includes a DataSource
installed in the container.
Your bundle’s persistence.xml
calls out the DataSource
through JNDI.
For example, you could install the following H2 DataSource
.
You can deploy the DataSource
manually (Karaf has a deploy
dir), or through a "blueprint bundle" (blueprint:file:/[PATH]/datasource-h2.xml
).
</div>
`<?xml version="1.0" encoding="UTF-8"?> <!-- First install the H2 driver using: > install -s mvn:com.h2database/h2/1.3.163
Then copy this file to the deploy folder --> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="dataSource" class="org.h2.jdbcx.JdbcDataSource"> <property name="URL" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"/> <property name="user" value="sa"/> <property name="password" value=""/> </bean>
<service interface="javax.sql.DataSource" ref="dataSource"> <service-properties> <entry key="osgi.jndi.service.name" value="jdbc/h2ds"/> </service-properties> </service> </blueprint>`</pre> </div> </div> </div> </div>
That DataSource
is then used by your persistence.xml
persistence-unit. The following works in Karaf, but the names may need tweaked in alternative containers.
</div>
`<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/h2ds)</jta-data-source>`</div> </div> </div> </div> </div>