Connecting MS SQL Database from 5.6 using OSGi Service | Community
Skip to main content
Krishna_C
New Participant
October 16, 2015
Solved

Connecting MS SQL Database from 5.6 using OSGi Service

  • October 16, 2015
  • 8 replies
  • 2541 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

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. 

8 replies

edubey
New Participant
October 16, 2015

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. 

Krishna_C
Krishna_CAuthor
New Participant
October 16, 2015

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

edubey
New Participant
October 16, 2015

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.

Krishna_C
Krishna_CAuthor
New Participant
October 16, 2015

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

smacdonald2008
smacdonald2008Accepted solution
New Participant
October 16, 2015

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. 

Krishna_C
Krishna_CAuthor
New Participant
October 16, 2015

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

smacdonald2008
New Participant
October 16, 2015

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. 

Krishna_C
Krishna_CAuthor
New Participant
October 16, 2015

Thanks Scott