Expand my Community achievements bar.

SOLVED

Connecting MS SQL Database from 5.6 using OSGi Service

Avatar

Level 4

Hi Everyone,

I am trying to connect to MS SQL database in AEM 5.6 . Below is my service class which will provide the connection.

@Component
@Service
public class JDBCConnectionServiceImpl implements JDBCConnectionService{
    
    private static final Logger LOGGER =
            LoggerFactory.getLogger(JDBCConnectionServiceImpl.class);    
    private static String DATA_SOURCE_NAME="sqljdbc";    
    @Reference
    private DataSourcePool source;

    public Connection getConnection() {

        Connection con=null;

        try {
             DataSource ds = (DataSource) this.source.getDataSource(DATA_SOURCE_NAME);  
             con= ds.getConnection();
        }catch(Exception e){
            LOGGER.error("Exception when getting the setting data source ",e);
        }
        return con;
    }

    public void closeConnection(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOGGER.error("Exception when trying to close the connection {} ",e);
        }
    }
}

When I try to access this service it throws a class not found exception as shown

java.lang.ClassNotFoundException: mypackage.JDBCConnectionServiceImpl not found by mypackage [377]
    at java.lang.Throwable.<init>(Throwable.java:80)
    at java.lang.ClassNotFoundException.<init>(ClassNotFoundException.java:76)
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1420)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1923)

But when I access the same data source in component JSP as shown below it is working fine 

DataSourcePool dspService = sling.getService(DataSourcePool.class);
DataSource ds = (DataSource) dspService.getDataSource("sqljdbc");

Not sure what is the problem with OSGi Service. 

Help me on this please.

Regards,

Krishna

1 Accepted Solution

Avatar

Correct answer by
Level 10

When creating the Maven project - try placing the interface and imlp class in the same AEM package. Then delete all other generated Maven Java files. 

View solution in original post

8 Replies

Avatar

Level 10

Hi,

first,Here is the complete article on Adobe site: https://helpx.adobe.com/experience-manager/using/persisting-cq-data-relational-database.html

second, did your build got successful when you deployed in AEM 5.6.1?

Ensured that your bundle STARTED once it is deployed in AEM 5.6.1. 

Avatar

Level 4

Hi ,

Thanks for the reply.

I have went through the article already. 

I have different bundles deployed for my project and ms sql drivers. Both are build successfully.D

When I deploy the project bundle it throws Impl class not found exception as mentioned in the post. Still both the bundles are active in felix. 

Thanks,

Krishna

Avatar

Level 10

Its seems to be the simple case where AEM is not able to find you class although as you mentioned build is successful,

Can you check in your pom.xml that the packages in which this class is present is in <export-package> section.

Avatar

Level 4

Export package details are fine.  Even though the build is successful when I deploy it throws a exception as mentioned.

I think there might be problem with OSGi configuration which I am not aware like binding datasourcepool before the service starts (Just a thought)

Thanks.

Krishna

Avatar

Correct answer by
Level 10

When creating the Maven project - try placing the interface and imlp class in the same AEM package. Then delete all other generated Maven Java files. 

Avatar

Level 4

Thanks Scott,

It is working fine now .But the other services works fine without having the implementation and interface in the same package .

Is this a restriction while using  DataSourcePool ?

Is there any way to encrypt the password  available in DataSourcePool OSGi Configuration ?

Thanks,

Krishna

Avatar

Level 10

From your error - it looked like there was an issue finding the Imlp class. I find this happens sometimes and this is the fix i use.