I'm not using JDBC datasources personally, but can you clarify in what
context your client code is running?

In general the @Reference annotations (both the older felix.scr ones and
the newer standardardized annotations from OSGi R6 specification) are only
going to work when they are within a declarative service component and that
client class is accessed as an osgi service.

If the client code is running in some other context or lookup of the client
class happens some other way, then you would likely need a different way to
get the reference to the DataSource component.

For example, sling models can use the @Inject annotation for getting a
reference to an osgi service, or you can get the DataSource service
reference from a ServiceTracker or a direct service reference lookup from
some BundleContext.

Regards,
Eric

On Fri, Feb 26, 2021 at 7:04 AM JCR <j...@proxymit.net> wrote:

> Adding a detail: The @Reference annotation is deprecated.
>
> ****************************
>
> For creating a JDBC connection pool (to mariadb) in Sling 11, I followed
> the instructions given at
> https://sling.apache.org/documentation/bundles/datasource-providers.html
> . However, I get a runtime exception on the datasource without further
> details.
>
> As documented on that webpage, I declared the following in the consuming
> class:
>
> import org.apache.felix.scr.annotations.Reference;
>
> public class Database {
>
>      @Reference(target =
> "(&(objectclass=javax.sql.DataSource)(datasource.name=dbdb))")
>      DataSource dataSource;
>
>      public String test() {
>
>          try {
>
>              Connection con = dataSource.getConnection();
>
>              ... Some SQL instructions...
>
>      }
>
> The config of the datasource was done via the Felix console and looks like:
>
>      in JNDI DataSource:
>
>          Datasource name(*): dbdb
>          JNDI name(*): dbdb
>
>      in Apache Sling Connection Pooled DataSource:
>
>          Datasource name(*): dbdb
>          JDBC driver class: org.mariadb.jdbc.Driver
>          JDBC connection URI:
> jdbc:mysql://localhost:3306/someDB?useUnicode=yes&characterEncoding=UTF-8
>          Username: xyz
>          .... many more
>
> The POM:
>      <dependency>
>              <groupId>org.apache.felix</groupId>
> <artifactId>org.apache.felix.scr.annotations</artifactId>
>              <version>1.12.0</version>
>          </dependency>
>
>
> Now, the datasource, at the moment of calling test() is null. I have
> absolutely no hint what the problem is. The config? The annotation? What
> is obvious, however, is that the /@Reference /as suggested on the
> bundles page, is deprecated... What would be the most current approach?
>
> Thanks,
> -Juerg
>
>
>
>

Reply via email to