Expand my Community achievements bar.

SOLVED

DataSourcePool service injection failure

Avatar

Level 2

I'm trying to use the OOB DataSourcePool service to connect to a MySQL from AEM in order to run a basic query  Nothing fancy.  After installing my driver and setting up the connection parameters through the system console, I'm able to fetch data from the DB without any problem when accessing the DataSourcePool from a jsp page as follows:

DataSourcePool service = sling.getService(DataSourcePool.class);

However, I'm unable to fetch data from the DB when I try to access the DataSourcePool from another OSGI service through service injection as follows:

@Reference

private DataSourcePool service;

The latter throws an exception at the time the system is attempting to inject the service (I believe), which says the following:

The bindSource method has thrown an exception (java.lang.IllegalArgumentException: argument type mismatch)

Subsequently the service variable is null when I attempt to de-reference it.

Any ideas?

Thanks!

1 Accepted Solution

Avatar

Correct answer by
Level 2

After some more digging, I found the solution to my problem in the second to last reply of this thread: http://forums.adobe.com/message/4862409

Exact same stack trace and all!

Thanks so much for your help smacdonald2008!

View solution in original post

6 Replies

Avatar

Correct answer by
Level 2

After some more digging, I found the solution to my problem in the second to last reply of this thread: http://forums.adobe.com/message/4862409

Exact same stack trace and all!

Thanks so much for your help smacdonald2008!

Avatar

Level 2

MyService.java (interface)

public interface MyService {
    public String helloWorld();
}

MyServiceImpl.java (implementation)

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;

import com.day.commons.datasource.poolservice.DataSourcePool;

@Component(metatype = true , label = "My Service")
@Service(value = MyService.class)
public class MyServiceImpl implements MyService {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(MyServiceImpl.class);
    
    @Reference
   private DataSourcePool source;
    
    public String helloWorld() {
        LOGGER.info("saying Hello!");
        
        String result = "Hello World!";

        DataSource dataSource = null;
        Connection con = null;
        try
        {
            dataSource = (DataSource) source.getDataSource("myDB");
            con = dataSource.getConnection();
            PreparedStatement ps = con.prepareStatement("select email from users where username=?");
            ps.setString(1, "admin");
            ResultSet rs = ps.executeQuery();
            rs.next();
            result = rs.getString("email");
            ps.close();
        }catch(Exception e)
        {
            LOGGER.error("Error creating connection from {}.", "myDB", e);
        }
        finally
        {
           if(con != null)
           {
               try
               {
                   con.close();
               }
               catch(SQLException se)
               {
                   LOGGER.error("Error closing connection from data source 'myDB'");
               }
           }
        }
        
        return result;    
    }
        
    @Activate
    protected void activate() {
        LOGGER.info("service activated" );
    }
    
    @Deactivate
    protected void deactivate() {
        LOGGER.info ("service deactivated");
    }
    
}

Avatar

Level 10

See if this thread helps: http://stackoverflow.com/questions/8595279/getting-osgi-services-from-a-bundle-in-sling-cq/

I have experienced similar issue with DataSourcePool. A work around is to write your own JDBC ConnectionHelper class as discussed here:

http://scottsdigitalcommunity.blogspot.ca/2013/08/querying-and-persisting-adobe-aem-data.html

Avatar

Level 10

To make sure that no community member has an issue with this subject again -- a new community article is being produced. See http://scottsdigitalcommunity.blogspot.ca/2013/09/injecting-datasourcepool-service-into.html.